Skip to content

[HttpFoundation] Constraint ResponseHeaderSame now shows the actual header value #49227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function assertResponseHeaderSame(string $headerName, string $expe

public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThatForResponse(new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message);
self::assertThatForResponse(new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue, true)), $message);
}

public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function testAssertResponseHeaderSame()
{
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'no-cache, private');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public".');
$this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public", value of header "Cache-Control" is "no-cache, private".');
$this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'public');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,39 @@ final class ResponseHeaderSame extends Constraint
{
private string $headerName;
private string $expectedValue;
private bool $logicalNot;
private ?string $actualValue = null;

public function __construct(string $headerName, string $expectedValue)
public function __construct(string $headerName, string $expectedValue, bool $logicalNot = false)
Copy link
Contributor Author

@jschaedl jschaedl Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Let me try to explain:

The test case for the assertResponseHeaderNotSame

public function testAssertResponseHeaderNotSame()
{
    $this->getResponseTester(new Response())->assertResponseHeaderNotSame('Cache-Control', 'public');
    $this->expectException(AssertionFailedError::class);
    $this->expectExceptionMessage('Failed asserting that the Response does not have header "Cache-Control" with value "no-cache, private", value of header "Cache-Control" is "no-cache, private".');
    $this->getResponseTester(new Response())->assertResponseHeaderNotSame('Cache-Control', 'no-cache, private');
}

failed with:

Failed asserting that exception message

'Failed asserting that the Response does not have header "Cache-Control" with value "no-cache, private", value of header "Cache-Control" is not "no-cache, private".'

contains

'Failed asserting that the Response does not have header "Cache-Control" with value "no-cache, private", value of header "Cache-Control" is "no-cache, private".'.

  • Please notice the not in the actual exception message.

This is happening because the assertResponseHeaderNotSame assertion uses a ResponseHeaderSame constraint internally. This constraint is wrapped by a LogicalNot which is in fact the reason all messages for this constraint are negated.

So the best idea I had so far is to "ignore" the logical not case.

{
$this->headerName = $headerName;
$this->expectedValue = $expectedValue;
$this->logicalNot = $logicalNot;
}

public function toString(): string
{
return sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue);
$output = sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue);

if (null === $this->actualValue) {
$output .= sprintf(', header "%s" is not set', $this->headerName);
}

if (null !== $this->actualValue && !$this->logicalNot) {
$output .= sprintf(', value of header "%s" is "%s"', $this->headerName, $this->actualValue);
}

return $output;
}

/**
* @param Response $response
*/
protected function matches($response): bool
{
return $this->expectedValue === $response->headers->get($this->headerName, null);
$this->actualValue = $response->headers->get($this->headerName);

return $this->expectedValue === $this->actualValue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,48 @@

class ResponseHeaderSameTest extends TestCase
{
public function testConstraint()
public function testResponseHeaderSameWithExpectedHeaderValueIsSame()
{
$constraint = new ResponseHeaderSame('Cache-Control', 'no-cache, private');
$this->assertTrue($constraint->evaluate(new Response(), '', true));
$constraint = new ResponseHeaderSame('Cache-Control', 'public');
$this->assertFalse($constraint->evaluate(new Response(), '', true));
$constraint = new ResponseHeaderSame('X-Token', 'custom-token');

$response = new Response();
$response->headers->set('X-Token', 'custom-token');

$this->assertTrue($constraint->evaluate($response, '', true));
}

public function testResponseHeaderSameWithExpectedHeaderValueIsDifferent()
{
$constraint = new ResponseHeaderSame('X-Token', 'custom-token');

$response = new Response();
$response->headers->set('X-Token', 'default-token');

$this->assertFalse($constraint->evaluate($response, '', true));

try {
$constraint->evaluate($response);
} catch (ExpectationFailedException $e) {
$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));

return;
}

$this->fail();
}

public function testResponseHeaderSameWithExpectedHeaderIsNotPresent()
{
$constraint = new ResponseHeaderSame('X-Token', 'custom-token');

$response = new Response();

$this->assertFalse($constraint->evaluate($response, '', true));

try {
$constraint->evaluate(new Response());
$constraint->evaluate($response);
} catch (ExpectationFailedException $e) {
$this->assertEquals("Failed asserting that the Response has header \"Cache-Control\" with value \"public\".\n", TestFailure::exceptionToString($e));
$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));

return;
}
Expand Down