@@ -41,6 +41,8 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
41
41
42
42
$ request ->getMethod ()->willReturn ('GET ' );
43
43
$ request ->getUri ()->willReturn ('/ ' );
44
+ $ request ->getBody ()->shouldBeCalled ();
45
+
44
46
$ response ->getStatusCode ()->willReturn (200 );
45
47
$ response ->getBody ()->willReturn ($ stream );
46
48
$ response ->getHeader ('Cache-Control ' )->willReturn (array ())->shouldBeCalled ();
@@ -71,6 +73,8 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte
71
73
{
72
74
$ request ->getMethod ()->willReturn ('GET ' );
73
75
$ request ->getUri ()->willReturn ('/ ' );
76
+ $ request ->getBody ()->shouldBeCalled ();
77
+
74
78
$ response ->getStatusCode ()->willReturn (400 );
75
79
$ response ->getHeader ('Cache-Control ' )->willReturn (array ());
76
80
$ response ->getHeader ('Expires ' )->willReturn (array ());
@@ -85,7 +89,7 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte
85
89
$ this ->handleRequest ($ request , $ next , function () {});
86
90
}
87
91
88
- function it_doesnt_store_post_requests (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response )
92
+ function it_doesnt_store_post_requests_by_default (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response )
89
93
{
90
94
$ request ->getMethod ()->willReturn ('POST ' );
91
95
$ request ->getUri ()->willReturn ('/ ' );
@@ -97,6 +101,74 @@ function it_doesnt_store_post_requests(CacheItemPoolInterface $pool, CacheItemIn
97
101
$ this ->handleRequest ($ request , $ next , function () {});
98
102
}
99
103
104
+ function it_stores_post_requests_when_allowed (
105
+ CacheItemPoolInterface $ pool ,
106
+ CacheItemInterface $ item ,
107
+ RequestInterface $ request ,
108
+ ResponseInterface $ response ,
109
+ StreamFactory $ streamFactory ,
110
+ StreamInterface $ stream
111
+ ) {
112
+ $ this ->beConstructedWith ($ pool , $ streamFactory , [
113
+ 'default_ttl ' => 60 ,
114
+ 'cache_lifetime ' => 1000 ,
115
+ 'methods ' => ['GET ' , 'HEAD ' , 'POST ' ]
116
+ ]);
117
+
118
+ $ httpBody = 'hello=world ' ;
119
+ $ stream ->__toString ()->willReturn ($ httpBody );
120
+ $ stream ->isSeekable ()->willReturn (true );
121
+ $ stream ->rewind ()->shouldBeCalled ();
122
+
123
+ $ request ->getMethod ()->willReturn ('POST ' );
124
+ $ request ->getUri ()->willReturn ('/post ' );
125
+ $ request ->getBody ()->willReturn ($ stream );
126
+
127
+ $ response ->getStatusCode ()->willReturn (200 );
128
+ $ response ->getBody ()->willReturn ($ stream );
129
+ $ response ->getHeader ('Cache-Control ' )->willReturn ([])->shouldBeCalled ();
130
+ $ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
131
+ $ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
132
+
133
+ $ pool ->getItem ('e4311a9af932c603b400a54efab21b6d7dea7a90 ' )->shouldBeCalled ()->willReturn ($ item );
134
+ $ item ->isHit ()->willReturn (false );
135
+ $ item ->expiresAfter (1060 )->willReturn ($ item )->shouldBeCalled ();
136
+
137
+ $ item ->set ($ this ->getCacheItemMatcher ([
138
+ 'response ' => $ response ->getWrappedObject (),
139
+ 'body ' => $ httpBody ,
140
+ 'expiresAt ' => 0 ,
141
+ 'createdAt ' => 0 ,
142
+ 'etag ' => []
143
+ ]))->willReturn ($ item )->shouldBeCalled ();
144
+
145
+ $ pool ->save (Argument::any ())->shouldBeCalled ();
146
+
147
+ $ next = function (RequestInterface $ request ) use ($ response ) {
148
+ return new FulfilledPromise ($ response ->getWrappedObject ());
149
+ };
150
+
151
+ $ this ->handleRequest ($ request , $ next , function () {});
152
+ }
153
+
154
+ function it_does_not_allow_invalid_request_methods (
155
+ CacheItemPoolInterface $ pool ,
156
+ CacheItemInterface $ item ,
157
+ RequestInterface $ request ,
158
+ ResponseInterface $ response ,
159
+ StreamFactory $ streamFactory ,
160
+ StreamInterface $ stream
161
+ ) {
162
+ $ this
163
+ ->shouldThrow ("Symfony\Component\OptionsResolver\Exception\InvalidOptionsException " )
164
+ ->during ('__construct ' , [$ pool , $ streamFactory , ['methods ' => ['GET ' , 'HEAD ' , 'POST ' ]]]);
165
+ $ this
166
+ ->shouldThrow ("Symfony\Component\OptionsResolver\Exception\InvalidOptionsException " )
167
+ ->during ('__construct ' , [$ pool , $ streamFactory , ['methods ' => ['GET ' , 'HEAD" ' , 'POST ' ]]]);
168
+ $ this
169
+ ->shouldThrow ("Symfony\Component\OptionsResolver\Exception\InvalidOptionsException " )
170
+ ->during ('__construct ' , [$ pool , $ streamFactory , ['methods ' => ['GET ' , 'head ' , 'POST ' ]]]);
171
+ }
100
172
101
173
function it_calculate_age_from_response (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response , StreamInterface $ stream )
102
174
{
@@ -107,6 +179,8 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
107
179
108
180
$ request ->getMethod ()->willReturn ('GET ' );
109
181
$ request ->getUri ()->willReturn ('/ ' );
182
+ $ request ->getBody ()->shouldBeCalled ();
183
+
110
184
$ response ->getStatusCode ()->willReturn (200 );
111
185
$ response ->getBody ()->willReturn ($ stream );
112
186
$ response ->getHeader ('Cache-Control ' )->willReturn (array ('max-age=40 ' ));
@@ -141,6 +215,7 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R
141
215
$ stream ->__toString ()->willReturn ($ httpBody );
142
216
$ stream ->isSeekable ()->willReturn (true );
143
217
$ stream ->rewind ()->shouldBeCalled ();
218
+ $ request ->getBody ()->shouldBeCalled ();
144
219
145
220
$ request ->getMethod ()->willReturn ('GET ' );
146
221
$ request ->getUri ()->willReturn ('/ ' );
@@ -176,6 +251,7 @@ function it_adds_etag_and_modfied_since_to_request(CacheItemPoolInterface $pool,
176
251
177
252
$ request ->getMethod ()->willReturn ('GET ' );
178
253
$ request ->getUri ()->willReturn ('/ ' );
254
+ $ request ->getBody ()->shouldBeCalled ();
179
255
180
256
$ request ->withHeader ('If-Modified-Since ' , 'Thursday, 01-Jan-70 01:18:31 GMT ' )->shouldBeCalled ()->willReturn ($ request );
181
257
$ request ->withHeader ('If-None-Match ' , 'foo_etag ' )->shouldBeCalled ()->willReturn ($ request );
@@ -205,6 +281,7 @@ function it_servces_a_cached_response(CacheItemPoolInterface $pool, CacheItemInt
205
281
206
282
$ request ->getMethod ()->willReturn ('GET ' );
207
283
$ request ->getUri ()->willReturn ('/ ' );
284
+ $ request ->getBody ()->shouldBeCalled ();
208
285
209
286
$ pool ->getItem ('d20f64acc6e70b6079845f2fe357732929550ae1 ' )->shouldBeCalled ()->willReturn ($ item );
210
287
$ item ->isHit ()->willReturn (true );
@@ -233,6 +310,7 @@ function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, Ca
233
310
234
311
$ request ->getMethod ()->willReturn ('GET ' );
235
312
$ request ->getUri ()->willReturn ('/ ' );
313
+ $ request ->getBody ()->shouldBeCalled ();
236
314
237
315
$ request ->withHeader (Argument::any (), Argument::any ())->willReturn ($ request );
238
316
$ request ->withHeader (Argument::any (), Argument::any ())->willReturn ($ request );
0 commit comments