Skip to content

Commit 6b2b0a3

Browse files
committed
Fixed broken Vary header handling
1 parent f3d761f commit 6b2b0a3

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/Psr6Store.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function lookup(Request $request)
167167
// Otherwise we have to see if Vary headers match
168168
$varyKeyRequest = $this->getVaryKey(
169169
$responseData['vary'],
170-
$request->headers
170+
$request
171171
);
172172

173173
if ($varyKeyRequest === $varyKeyResponse) {
@@ -218,7 +218,7 @@ public function write(Request $request, Response $response)
218218
}
219219

220220
// Add or replace entry with current Vary header key
221-
$entries[$this->getVaryKey($response->getVary(), $response->headers)] = [
221+
$entries[$this->getVaryKey($response->getVary(), $request)] = [
222222
'vary' => $response->getVary(),
223223
'headers' => $headers,
224224
'status' => $response->getStatusCode(),
@@ -400,12 +400,12 @@ public function getCacheKey(Request $request)
400400
}
401401

402402
/**
403-
* @param array $vary
404-
* @param HeaderBag $headerBag
403+
* @param array $vary
404+
* @param Request $request
405405
*
406406
* @return string
407407
*/
408-
public function getVaryKey(array $vary, HeaderBag $headerBag)
408+
public function getVaryKey(array $vary, Request $request)
409409
{
410410
if (0 === \count($vary)) {
411411
return self::NON_VARYING_KEY;
@@ -416,7 +416,7 @@ public function getVaryKey(array $vary, HeaderBag $headerBag)
416416
$hashData = '';
417417

418418
foreach ($vary as $headerName) {
419-
$hashData .= $headerName.':'.$headerBag->get($headerName);
419+
$hashData .= $headerName.':'.$request->headers->get($headerName);
420420
}
421421

422422
return hash('sha256', $hashData);

tests/Psr6StoreTest.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -332,34 +332,36 @@ public function testLookupWithEmptyCache()
332332
public function testLookupWithVaryResponse()
333333
{
334334
$request = Request::create('https://foobar.com/');
335-
$response = new Response('hello world', 200, ['Vary' => 'Foobar', 'Foobar' => 'whatever']);
335+
$request->headers->set('Foobar', 'whatever');
336+
$response = new Response('hello world', 200, ['Vary' => 'Foobar']);
336337

337338
$this->store->write($request, $response);
338339

340+
$request = Request::create('https://foobar.com/');
339341
$result = $this->store->lookup($request);
340-
341342
$this->assertNull($result);
342343

343344
$request = Request::create('https://foobar.com/');
344345
$request->headers->set('Foobar', 'whatever');
345-
346346
$result = $this->store->lookup($request);
347-
348347
$this->assertSame(200, $result->getStatusCode());
349348
$this->assertSame('hello world', $result->getContent());
350349
$this->assertSame('Foobar', $result->headers->get('Vary'));
351-
$this->assertSame('whatever', $result->headers->get('Foobar'));
352350
}
353351

354352
public function testLookupWithMultipleVaryResponse()
355353
{
356-
$request = Request::create('https://foobar.com/');
357-
$response1 = new Response('should be whatever 1', 200, ['Vary' => 'Foobar', 'Foobar' => 'whatever1']);
358-
$response2 = new Response('should be whatever 2', 200, ['Vary' => 'Foobar', 'Foobar' => 'whatever2']);
354+
$jsonRequest = Request::create('https://foobar.com/');
355+
$jsonRequest->headers->set('Accept', 'application/json');
356+
$htmlRequest = Request::create('https://foobar.com/');
357+
$htmlRequest->headers->set('Accept', 'text/html');
358+
359+
$jsonResponse = new Response('{}', 200, ['Vary' => 'Accept', 'Content-Type' => 'application/json']);
360+
$htmlResponse = new Response('<html></html>', 200, ['Vary' => 'Accept', 'Content-Type' => 'text/html']);
359361

360362
// Fill cache
361-
$this->store->write($request, $response1);
362-
$this->store->write($request, $response2);
363+
$this->store->write($jsonRequest, $jsonResponse);
364+
$this->store->write($htmlRequest, $htmlResponse);
363365

364366
// Should return null because no header provided
365367
$request = Request::create('https://foobar.com/');
@@ -368,27 +370,27 @@ public function testLookupWithMultipleVaryResponse()
368370

369371
// Should return null because header provided but non matching content
370372
$request = Request::create('https://foobar.com/');
371-
$request->headers->set('Foobar', 'whatever3');
373+
$request->headers->set('Accept', 'application/xml');
372374
$result = $this->store->lookup($request);
373375
$this->assertNull($result);
374376

375-
// Should return $response1
377+
// Should return a JSON response
376378
$request = Request::create('https://foobar.com/');
377-
$request->headers->set('Foobar', 'whatever1');
379+
$request->headers->set('Accept', 'application/json');
378380
$result = $this->store->lookup($request);
379381
$this->assertSame(200, $result->getStatusCode());
380-
$this->assertSame('should be whatever 1', $result->getContent());
381-
$this->assertSame('Foobar', $result->headers->get('Vary'));
382-
$this->assertSame('whatever1', $result->headers->get('Foobar'));
382+
$this->assertSame('{}', $result->getContent());
383+
$this->assertSame('Accept', $result->headers->get('Vary'));
384+
$this->assertSame('application/json', $result->headers->get('Content-Type'));
383385

384-
// Should return $response2
386+
// Should return an HTML response
385387
$request = Request::create('https://foobar.com/');
386-
$request->headers->set('Foobar', 'whatever2');
388+
$request->headers->set('Accept', 'text/html');
387389
$result = $this->store->lookup($request);
388390
$this->assertSame(200, $result->getStatusCode());
389-
$this->assertSame('should be whatever 2', $result->getContent());
390-
$this->assertSame('Foobar', $result->headers->get('Vary'));
391-
$this->assertSame('whatever2', $result->headers->get('Foobar'));
391+
$this->assertSame('<html></html>', $result->getContent());
392+
$this->assertSame('Accept', $result->headers->get('Vary'));
393+
$this->assertSame('text/html', $result->headers->get('Content-Type'));
392394
}
393395

394396
public function testInvalidate()

0 commit comments

Comments
 (0)