Skip to content

Commit f9660cc

Browse files
committed
Constraint ResponseHeaderSame now shows the actual header value
1 parent bce5492 commit f9660cc

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function testAssertResponseHeaderSame()
121121
{
122122
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'no-cache, private');
123123
$this->expectException(AssertionFailedError::class);
124-
$this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public".');
124+
$this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public", value of header "Cache-Control" is "no-cache, private".');
125125
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'public');
126126
}
127127

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

Lines changed: 15 additions & 2 deletions
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);
50+
51+
return $this->expectedValue === $this->actualValue;
3952
}
4053

4154
/**

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

Lines changed: 38 additions & 7 deletions
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)