Skip to content

Commit 82363f3

Browse files
smatyasdunglas
authored andcommitted
Ability to modify the Vary header (#2758)
* added ability to modify the Vary header * override Vary header from ApiResource metadata
1 parent 125dec9 commit 82363f3

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

src/HttpCache/EventListener/AddHeadersListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public function onKernelResponse(FilterResponseEvent $event): void
7070
$response->setMaxAge($maxAge);
7171
}
7272

73-
if (null !== $this->vary) {
73+
if (isset($resourceCacheHeaders['vary'])) {
74+
$response->setVary($resourceCacheHeaders['vary']);
75+
} elseif (null !== $this->vary) {
7476
$response->setVary(array_diff($this->vary, $response->getVary()), false);
7577
}
7678

tests/Annotation/ApiResourceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testConstruct()
2929
$resource = new ApiResource([
3030
'accessControl' => 'has_role("ROLE_FOO")',
3131
'accessControlMessage' => 'You are not foo.',
32-
'attributes' => ['foo' => 'bar', 'validation_groups' => ['baz', 'qux'], 'cache_headers' => ['max_age' => 0, 'shared_max_age' => 0]],
32+
'attributes' => ['foo' => 'bar', 'validation_groups' => ['baz', 'qux'], 'cache_headers' => ['max_age' => 0, 'shared_max_age' => 0, 'vary' => ['Custom-Vary-1', 'Custom-Vary-2']]],
3333
'collectionOperations' => ['bar' => ['foo']],
3434
'denormalizationContext' => ['groups' => ['foo']],
3535
'description' => 'description',
@@ -97,7 +97,7 @@ public function testConstruct()
9797
'route_prefix' => '/foo',
9898
'swagger_context' => ['description' => 'bar'],
9999
'validation_groups' => ['baz', 'qux'],
100-
'cache_headers' => ['max_age' => 0, 'shared_max_age' => 0],
100+
'cache_headers' => ['max_age' => 0, 'shared_max_age' => 0, 'vary' => ['Custom-Vary-1', 'Custom-Vary-2']],
101101
'sunset' => 'Thu, 11 Oct 2018 00:00:00 +0200',
102102
], $resource->attributes);
103103
}
@@ -120,7 +120,7 @@ public function testApiResourceAnnotation()
120120
'route_prefix' => '/whatever',
121121
'access_control' => "has_role('ROLE_FOO')",
122122
'access_control_message' => 'You are not foo.',
123-
'cache_headers' => ['max_age' => 0, 'shared_max_age' => 0],
123+
'cache_headers' => ['max_age' => 0, 'shared_max_age' => 0, 'vary' => ['Custom-Vary-1', 'Custom-Vary-2']],
124124
], $resource->attributes);
125125
}
126126

tests/Fixtures/AnnotatedClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* itemOperations={"foo"={"bar"}},
2424
* collectionOperations={"bar"={"foo"}},
2525
* graphql={"query"={"normalization_context"={"groups"={"foo", "bar"}}}},
26-
* attributes={"foo"="bar", "route_prefix"="/whatever", "cache_headers"={"max_age"=0, "shared_max_age"=0}},
26+
* attributes={"foo"="bar", "route_prefix"="/whatever", "cache_headers"={"max_age"=0, "shared_max_age"=0, "vary"={"Custom-Vary-1", "Custom-Vary-2"}}},
2727
* routePrefix="/foo",
2828
* accessControl="has_role('ROLE_FOO')",
2929
* accessControlMessage="You are not foo."

tests/HttpCache/EventListener/AddHeadersListenerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ public function testDoNotSetHeaderWhenAlreadySet()
126126
$factory = $this->prophesize(ResourceMetadataFactoryInterface::class);
127127
$factory->create(Dummy::class)->willReturn(new ResourceMetadata())->shouldBeCalled();
128128

129-
$listener = new AddHeadersListener(true, 100, 200, [], true, $factory->reveal());
129+
$listener = new AddHeadersListener(true, 100, 200, ['Accept', 'Accept-Encoding'], true, $factory->reveal());
130130
$listener->onKernelResponse($event->reveal());
131131

132132
$this->assertSame('"etag"', $response->getEtag());
133133
$this->assertSame('max-age=300, public, s-maxage=400', $response->headers->get('Cache-Control'));
134+
$this->assertSame(['Accept', 'Cookie', 'Accept-Encoding'], $response->getVary());
134135
}
135136

136137
public function testSetHeadersFromResourceMetadata()
@@ -142,13 +143,14 @@ public function testSetHeadersFromResourceMetadata()
142143
$event->getRequest()->willReturn($request)->shouldBeCalled();
143144
$event->getResponse()->willReturn($response)->shouldBeCalled();
144145

145-
$metadata = new ResourceMetadata(null, null, null, null, null, ['cache_headers' => ['max_age' => 123, 'shared_max_age' => 456]]);
146+
$metadata = new ResourceMetadata(null, null, null, null, null, ['cache_headers' => ['max_age' => 123, 'shared_max_age' => 456, 'vary' => ['Vary-1', 'Vary-2']]]);
146147
$factory = $this->prophesize(ResourceMetadataFactoryInterface::class);
147148
$factory->create(Dummy::class)->willReturn($metadata)->shouldBeCalled();
148149

149150
$listener = new AddHeadersListener(true, 100, 200, ['Accept', 'Accept-Encoding'], true, $factory->reveal());
150151
$listener->onKernelResponse($event->reveal());
151152

152153
$this->assertSame('max-age=123, public, s-maxage=456', $response->headers->get('Cache-Control'));
154+
$this->assertSame(['Vary-1', 'Vary-2'], $response->getVary());
153155
}
154156
}

0 commit comments

Comments
 (0)