Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 39bfb97

Browse filesBrowse files
committed
Constraint ResponseHeaderSame now shows the actual header value
1 parent 5f77c3f commit 39bfb97
Copy full SHA for 39bfb97

File tree

3 files changed

+54
-10
lines changed
Filter options

3 files changed

+54
-10
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function testAssertResponseHeaderSame()
130130
{
131131
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'no-cache, private');
132132
$this->expectException(AssertionFailedError::class);
133-
$this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public".');
133+
$this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public", value of header "Cache-Control" is "no-cache, private".');
134134
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'public');
135135
}
136136

‎src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseHeaderSame.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseHeaderSame.php
+15-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class ResponseHeaderSame extends Constraint
1818
{
1919
private string $headerName;
2020
private string $expectedValue;
21+
private ?string $actualValue = null;
2122

2223
public function __construct(string $headerName, string $expectedValue)
2324
{
@@ -27,15 +28,27 @@ public function __construct(string $headerName, string $expectedValue)
2728

2829
public function toString(): string
2930
{
30-
return sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue);
31+
$output = sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue);
32+
33+
if (null === $this->actualValue) {
34+
$output .= sprintf(', header "%s" is not set', $this->headerName);
35+
}
36+
37+
if (null !== $this->actualValue) {
38+
$output .= sprintf(', value of header "%s" is "%s"', $this->headerName, $this->actualValue);
39+
}
40+
41+
return $output;
3142
}
3243

3344
/**
3445
* @param Response $response
3546
*/
3647
protected function matches($response): bool
3748
{
38-
return $this->expectedValue === $response->headers->get($this->headerName, null);
49+
$this->actualValue = $response->headers->get($this->headerName, null);
50+
51+
return $this->expectedValue === $this->actualValue;
3952
}
4053

4154
/**

‎src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseHeaderSameTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseHeaderSameTest.php
+38-7
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,48 @@
1919

2020
class ResponseHeaderSameTest extends TestCase
2121
{
22-
public function testConstraint()
22+
public function testResponseHeaderSameWithExpectedHeaderValueIsSame()
2323
{
24-
$constraint = new ResponseHeaderSame('Cache-Control', 'no-cache, private');
25-
$this->assertTrue($constraint->evaluate(new Response(), '', true));
26-
$constraint = new ResponseHeaderSame('Cache-Control', 'public');
27-
$this->assertFalse($constraint->evaluate(new Response(), '', true));
24+
$constraint = new ResponseHeaderSame('X-Token', 'custom-token');
25+
26+
$response = new Response();
27+
$response->headers->set('X-Token', 'custom-token');
28+
29+
$this->assertTrue($constraint->evaluate($response, '', true));
30+
}
31+
32+
public function testResponseHeaderSameWithExpectedHeaderValueIsDifferent()
33+
{
34+
$constraint = new ResponseHeaderSame('X-Token', 'custom-token');
35+
36+
$response = new Response();
37+
$response->headers->set('X-Token', 'default-token');
38+
39+
$this->assertFalse($constraint->evaluate($response, '', true));
40+
41+
try {
42+
$constraint->evaluate($response);
43+
} catch (ExpectationFailedException $e) {
44+
$this->assertEquals("Failed asserting that the Response has header \"X-Token\" with value \"custom-token\", value of header \"X-Token\" is \"default-token\".\n", TestFailure::exceptionToString($e));
45+
46+
return;
47+
}
48+
49+
$this->fail();
50+
}
51+
52+
public function testResponseHeaderSameWithExpectedHeaderIsNotPresent()
53+
{
54+
$constraint = new ResponseHeaderSame('X-Token', 'custom-token');
55+
56+
$response = new Response();
57+
58+
$this->assertFalse($constraint->evaluate($response, '', true));
2859

2960
try {
30-
$constraint->evaluate(new Response());
61+
$constraint->evaluate($response);
3162
} catch (ExpectationFailedException $e) {
32-
$this->assertEquals("Failed asserting that the Response has header \"Cache-Control\" with value \"public\".\n", TestFailure::exceptionToString($e));
63+
$this->assertEquals("Failed asserting that the Response has header \"X-Token\" with value \"custom-token\", header \"X-Token\" is not set.\n", TestFailure::exceptionToString($e));
3364

3465
return;
3566
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.