@@ -104,6 +104,7 @@ public function testOnKernelRequestNotMatched()
104104
105105 $ requestMatcher = $ this ->getRequestMatcher ($ request , false );
106106 $ hashGenerator = \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' );
107+ $ hashGenerator ->shouldReceive ('generateHash ' )->andReturn ('hash ' );
107108
108109 $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ('X-SessionId ' ), 'X-Hash ' );
109110 $ event = $ this ->getKernelRequestEvent ($ request );
@@ -168,6 +169,67 @@ public function testOnKernelResponseNotCached()
168169 $ this ->assertEquals ('X-SessionId ' , $ event ->getResponse ()->headers ->get ('Vary ' ));
169170 }
170171
172+ /**
173+ * If there is no hash in the request, vary on the user identifier.
174+ */
175+ public function testFullRequestHashOk ()
176+ {
177+ $ request = new Request ();
178+ $ request ->setMethod ('GET ' );
179+ $ request ->headers ->set ('X-Hash ' , 'hash ' );
180+
181+ $ requestMatcher = $ this ->getRequestMatcher ($ request , false );
182+ $ hashGenerator = \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' );
183+ $ hashGenerator ->shouldReceive ('generateHash ' )->andReturn ('hash ' );
184+
185+ // onKernelRequest
186+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ('X-SessionId ' ), 'X-Hash ' );
187+ $ event = $ this ->getKernelRequestEvent ($ request );
188+
189+ $ userContextSubscriber ->onKernelRequest ($ event );
190+
191+ $ response = $ event ->getResponse ();
192+
193+ $ this ->assertNull ($ response );
194+
195+ // onKernelResponse
196+ $ event = $ this ->getKernelResponseEvent ($ request );
197+ $ userContextSubscriber ->onKernelResponse ($ event );
198+
199+ $ this ->assertContains ('X-Hash ' , $ event ->getResponse ()->headers ->get ('Vary ' ));
200+ }
201+
202+ /**
203+ * If there is no hash in the requests but session changed, prevent setting bad cache
204+ */
205+ public function testFullRequestHashChanged ()
206+ {
207+ $ request = new Request ();
208+ $ request ->setMethod ('GET ' );
209+ $ request ->headers ->set ('X-Hash ' , 'hash ' );
210+
211+ $ requestMatcher = $ this ->getRequestMatcher ($ request , false );
212+ $ hashGenerator = \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' );
213+ $ hashGenerator ->shouldReceive ('generateHash ' )->andReturn ('hash-changed ' );
214+
215+ // onKernelRequest
216+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ('X-SessionId ' ), 'X-Hash ' );
217+ $ event = $ this ->getKernelRequestEvent ($ request );
218+
219+ $ userContextSubscriber ->onKernelRequest ($ event );
220+
221+ $ response = $ event ->getResponse ();
222+
223+ $ this ->assertNull ($ response );
224+
225+ // onKernelResponse
226+ $ event = $ this ->getKernelResponseEvent ($ request );
227+ $ userContextSubscriber ->onKernelResponse ($ event );
228+
229+ $ this ->assertFalse ($ event ->getResponse ()->headers ->has ('Vary ' ));
230+ $ this ->assertEquals ('max-age=0, no-cache, private ' , $ event ->getResponse ()->headers ->get ('Cache-Control ' ));
231+ }
232+
171233 protected function getKernelRequestEvent (Request $ request , $ type = HttpKernelInterface::MASTER_REQUEST )
172234 {
173235 return new GetResponseEvent (
0 commit comments