22
22
23
23
class UserContextSubscriberTest extends \PHPUnit_Framework_TestCase
24
24
{
25
+ /**
26
+ * @expectedException \InvalidArgumentException
27
+ */
28
+ public function testMisconfiguration ()
29
+ {
30
+ new UserContextSubscriber (
31
+ \Mockery::mock ('\Symfony\Component\HttpFoundation\RequestMatcherInterface ' ),
32
+ \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' ),
33
+ array ()
34
+ );
35
+ }
36
+
25
37
public function testOnKernelRequest ()
26
38
{
27
39
$ request = new Request ();
@@ -45,6 +57,23 @@ public function testOnKernelRequest()
45
57
$ this ->assertEquals ('max-age=0, no-cache, private ' , $ response ->headers ->get ('Cache-Control ' ));
46
58
}
47
59
60
+ public function testOnKernelRequestNonMaster ()
61
+ {
62
+ $ request = new Request ();
63
+ $ request ->setMethod ('HEAD ' );
64
+
65
+ $ requestMatcher = $ this ->getRequestMatcher ($ request , true );
66
+ $ hashGenerator = \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' );
67
+ $ hashGenerator ->shouldReceive ('generateHash ' )->never ();
68
+
69
+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ('X-SessionId ' ), 'X-Hash ' );
70
+ $ event = $ this ->getKernelRequestEvent ($ request , HttpKernelInterface::SUB_REQUEST );
71
+
72
+ $ userContextSubscriber ->onKernelRequest ($ event );
73
+
74
+ $ this ->assertNull ($ event ->getResponse ());
75
+ }
76
+
48
77
public function testOnKernelRequestCached ()
49
78
{
50
79
$ request = new Request ();
@@ -103,6 +132,23 @@ public function testOnKernelResponse()
103
132
$ this ->assertContains ('X-Hash ' , $ event ->getResponse ()->headers ->get ('Vary ' ));
104
133
}
105
134
135
+ public function testOnKernelResponseNotMaster ()
136
+ {
137
+ $ request = new Request ();
138
+ $ request ->setMethod ('HEAD ' );
139
+ $ request ->headers ->set ('X-Hash ' , 'hash ' );
140
+
141
+ $ requestMatcher = $ this ->getRequestMatcher ($ request , false );
142
+ $ hashGenerator = \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' );
143
+
144
+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ('X-SessionId ' ), 'X-Hash ' );
145
+ $ event = $ this ->getKernelResponseEvent ($ request , null , HttpKernelInterface::SUB_REQUEST );
146
+
147
+ $ userContextSubscriber ->onKernelResponse ($ event );
148
+
149
+ $ this ->assertFalse ($ event ->getResponse ()->headers ->has ('Vary ' ));
150
+ }
151
+
106
152
/**
107
153
* If there is no hash in the request, vary on the user identifier.
108
154
*/
@@ -122,21 +168,21 @@ public function testOnKernelResponseNotCached()
122
168
$ this ->assertEquals ('X-SessionId ' , $ event ->getResponse ()->headers ->get ('Vary ' ));
123
169
}
124
170
125
- protected function getKernelRequestEvent (Request $ request )
171
+ protected function getKernelRequestEvent (Request $ request, $ type = HttpKernelInterface:: MASTER_REQUEST )
126
172
{
127
173
return new GetResponseEvent (
128
174
\Mockery::mock ('\Symfony\Component\HttpKernel\HttpKernelInterface ' ),
129
175
$ request ,
130
- HttpKernelInterface:: MASTER_REQUEST
176
+ $ type
131
177
);
132
178
}
133
179
134
- protected function getKernelResponseEvent (Request $ request , Response $ response = null )
180
+ protected function getKernelResponseEvent (Request $ request , Response $ response = null , $ type = HttpKernelInterface:: MASTER_REQUEST )
135
181
{
136
182
return new FilterResponseEvent (
137
183
\Mockery::mock ('\Symfony\Component\HttpKernel\HttpKernelInterface ' ),
138
184
$ request ,
139
- HttpKernelInterface:: MASTER_REQUEST ,
185
+ $ type ,
140
186
$ response ?: new Response ()
141
187
);
142
188
}
0 commit comments