bug #19651 [HttpKernel] Fix HttpCache validation HTTP method (tgalopin)

This PR was merged into the 2.7 branch.

Discussion
----------

[HttpKernel] Fix HttpCache validation HTTP method

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19582
| License       | MIT
| Doc PR        | -

Commits
-------

1a8a8af [HttpKernel] Fix HttpCache validation HTTP method
This commit is contained in:
Fabien Potencier 2016-08-17 11:31:09 -07:00
commit c5ca5f3ed7
2 changed files with 21 additions and 2 deletions

View File

@ -374,7 +374,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
$subRequest = clone $request;
// send no head requests because we want content
$subRequest->setMethod('GET');
if ('HEAD' === $request->getMethod()) {
$subRequest->setMethod('GET');
}
// add our cached last-modified validator
$subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified'));
@ -435,7 +437,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
$subRequest = clone $request;
// send no head requests because we want content
$subRequest->setMethod('GET');
if ('HEAD' === $request->getMethod()) {
$subRequest->setMethod('GET');
}
// avoid that the backend sends no content
$subRequest->headers->remove('if_modified_since');

View File

@ -799,6 +799,21 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertTraceNotContains('miss');
}
public function testValidatesCachedResponsesUseSameHttpMethod()
{
$test = $this;
$this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($test) {
$test->assertSame('OPTIONS', $request->getMethod());
});
// build initial request
$this->request('OPTIONS', '/');
// build subsequent request
$this->request('OPTIONS', '/');
}
public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
{
$this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {