Skip to content

Commit aa57d9a

Browse files
Merge branch '6.4' into 7.3
* 6.4: [Serializer] Fix unknown type in denormalization errors when union type used in constructor [HttpKernel] Handle an array vary header in the http cache store for write [Console] Fix handling of `\E` in Bash completion
2 parents f136c4d + 5be8659 commit aa57d9a

File tree

2 files changed

+124
-7
lines changed

2 files changed

+124
-7
lines changed

HttpCache/Store.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,9 @@ public function write(Request $request, Response $response): string
206206

207207
// read existing cache entries, remove non-varying, and add this one to the list
208208
$entries = [];
209-
$vary = $response->headers->get('vary');
209+
$vary = implode(', ', $response->headers->all('vary'));
210210
foreach ($this->getMetadata($key) as $entry) {
211-
if (!isset($entry[1]['vary'][0])) {
212-
$entry[1]['vary'] = [''];
213-
}
214-
215-
if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
211+
if (!$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
216212
$entries[] = $entry;
217213
}
218214
}
@@ -278,7 +274,7 @@ public function invalidate(Request $request): void
278274
*/
279275
private function requestsMatch(?string $vary, array $env1, array $env2): bool
280276
{
281-
if (!$vary) {
277+
if ('' === ($vary ?? '')) {
282278
return true;
283279
}
284280

Tests/HttpCache/StoreTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,127 @@ public function testLoadsBodyEval()
348348
$this->assertSame($content, $response->getContent());
349349
}
350350

351+
/**
352+
* Basic case when the second header has a different value.
353+
* Both responses should be cached
354+
*/
355+
public function testWriteWithMultipleVaryAndCachedAllResponse()
356+
{
357+
$req1 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_BAR' => 'bar']);
358+
$content = str_repeat('a', 24).'b'.str_repeat('a', 24);
359+
$res1 = new Response($content, 200, ['vary' => ['Foo', 'Bar'], 'X-Body-Eval' => 'SSI']);
360+
$this->store->write($req1, $res1);
361+
362+
$responseLook = $this->store->lookup($req1);
363+
$this->assertSame($content, $responseLook->getContent());
364+
365+
$req2 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_BAR' => 'foobar']);
366+
$content2 = str_repeat('b', 24).'a'.str_repeat('b', 24);
367+
$res2 = new Response($content2, 200, ['vary' => ['Foo', 'Bar'], 'X-Body-Eval' => 'SSI']);
368+
$this->store->write($req2, $res2);
369+
370+
$responseLook = $this->store->lookup($req2);
371+
$this->assertSame($content2, $responseLook->getContent());
372+
373+
$responseLook = $this->store->lookup($req1);
374+
$this->assertSame($content, $responseLook->getContent());
375+
}
376+
377+
/**
378+
* Basic case when the second header has the same value on both requests.
379+
* The last response should be cached
380+
*/
381+
public function testWriteWithMultipleVaryAndCachedLastResponse()
382+
{
383+
$req1 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_BAR' => 'bar']);
384+
$content = str_repeat('a', 24).'b'.str_repeat('a', 24);
385+
$res1 = new Response($content, 200, ['vary' => ['Foo', 'Bar'], 'X-Body-Eval' => 'SSI']);
386+
$this->store->write($req1, $res1);
387+
388+
$responseLook = $this->store->lookup($req1);
389+
$this->assertSame($content, $responseLook->getContent());
390+
391+
$req2 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_BAR' => 'bar']);
392+
$content2 = str_repeat('b', 24).'a'.str_repeat('b', 24);
393+
$res2 = new Response($content2, 200, ['vary' => ['Foo', 'Bar'], 'X-Body-Eval' => 'SSI']);
394+
$this->store->write($req2, $res2);
395+
396+
$responseLook = $this->store->lookup($req2);
397+
$this->assertSame($content2, $responseLook->getContent());
398+
399+
$responseLook = $this->store->lookup($req1);
400+
$this->assertSame($content2, $responseLook->getContent());
401+
}
402+
403+
/**
404+
* Case when a vary value has been removed.
405+
* Both responses should be cached
406+
*/
407+
public function testWriteWithChangingVary()
408+
{
409+
$req1 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_BAR' => 'bar']);
410+
$content = str_repeat('a', 24).'b'.str_repeat('a', 24);
411+
$res1 = new Response($content, 200, ['vary' => ['Foo', 'bar', 'foobar'], 'X-Body-Eval' => 'SSI']);
412+
$this->store->write($req1, $res1);
413+
414+
$req2 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_FOOBAR' => 'bar']);
415+
$content2 = str_repeat('b', 24).'a'.str_repeat('b', 24);
416+
$res2 = new Response($content2, 200, ['vary' => ['Foo', 'foobar'], 'X-Body-Eval' => 'SSI']);
417+
$this->store->write($req2, $res2);
418+
419+
$responseLook = $this->store->lookup($req2);
420+
$this->assertSame($content2, $responseLook->getContent());
421+
422+
$responseLook = $this->store->lookup($req1);
423+
$this->assertSame($content, $responseLook->getContent());
424+
}
425+
426+
/**
427+
* Case when a vary value has been removed and headers of the new vary list are the same.
428+
* The last response should be cached
429+
*/
430+
public function testWriteWithRemoveVaryAndAllHeadersOnTheList()
431+
{
432+
$req1 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_FOOBAR' => 'bar',]);
433+
$content = str_repeat('a', 24).'b'.str_repeat('a', 24);
434+
$res1 = new Response($content, 200, ['vary' => ['Foo', 'bar', 'foobar'], 'X-Body-Eval' => 'SSI']);
435+
$this->store->write($req1, $res1);
436+
437+
$req2 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_FOOBAR' => 'bar']);
438+
$content2 = str_repeat('b', 24).'a'.str_repeat('b', 24);
439+
$res2 = new Response($content2, 200, ['vary' => ['Foo', 'foobar'], 'X-Body-Eval' => 'SSI']);
440+
$this->store->write($req2, $res2);
441+
442+
$responseLook = $this->store->lookup($req2);
443+
$this->assertSame($content2, $responseLook->getContent());
444+
445+
$responseLook = $this->store->lookup($req1);
446+
$this->assertSame($content2, $responseLook->getContent());
447+
}
448+
449+
/**
450+
* Case when a vary value has been added and headers of the new vary list are the same.
451+
* The last response should be cached
452+
*/
453+
public function testWriteWithAddingVaryAndAllHeadersOnTheList()
454+
{
455+
$req1 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_FOOBAR' => 'bar']);
456+
$content = str_repeat('a', 24).'b'.str_repeat('a', 24);
457+
$res1 = new Response($content, 200, ['vary' => ['Foo', 'foobar'], 'X-Body-Eval' => 'SSI']);
458+
$this->store->write($req1, $res1);
459+
460+
$req2 = Request::create('/foo', 'get', [], [], [], ['HTTP_FOO' => 'foo', 'HTTP_BAR' => 'foobar', 'HTTP_FOOBAR' => 'bar']);
461+
$content2 = str_repeat('b', 24).'a'.str_repeat('b', 24);
462+
$res2 = new Response($content2, 200, ['vary' => ['Foo', 'bar', 'foobar'], 'X-Body-Eval' => 'SSI']);
463+
$this->store->write($req2, $res2);
464+
465+
$responseLook = $this->store->lookup($req2);
466+
$this->assertSame($content2, $responseLook->getContent());
467+
468+
$responseLook = $this->store->lookup($req1);
469+
$this->assertSame($content, $responseLook->getContent());
470+
}
471+
351472
protected function storeSimpleEntry($path = null, $headers = [])
352473
{
353474
$path ??= '/test';

0 commit comments

Comments
 (0)