PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.7
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.7
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / psr7 / tests / ResponseTest.php

ResponseTest.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.5.7, at vendor/guzzlehttp/psr7/tests/ResponseTest.php

362 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace GuzzleHttp\Tests\Psr7;
3
4 use GuzzleHttp\Psr7;
5 use GuzzleHttp\Psr7\Response;
6
7 /**
8 * @covers GuzzleHttp\Psr7\MessageTrait
9 * @covers GuzzleHttp\Psr7\Response
10 */
11 class ResponseTest extends BaseTest
12 {
13 public function testDefaultConstructor()
14 {
15 $r = new Response();
16 $this->assertSame(200, $r->getStatusCode());
17 $this->assertSame('1.1', $r->getProtocolVersion());
18 $this->assertSame('OK', $r->getReasonPhrase());
19 $this->assertSame([], $r->getHeaders());
20 $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
21 $this->assertSame('', (string) $r->getBody());
22 }
23
24 public function testCanConstructWithStatusCode()
25 {
26 $r = new Response(404);
27 $this->assertSame(404, $r->getStatusCode());
28 $this->assertSame('Not Found', $r->getReasonPhrase());
29 }
30
31 public function testConstructorDoesNotReadStreamBody()
32 {
33 $streamIsRead = false;
34 $body = Psr7\FnStream::decorate(Psr7\stream_for(''), [
35 '__toString' => function () use (&$streamIsRead) {
36 $streamIsRead = true;
37 return '';
38 }
39 ]);
40
41 $r = new Response(200, [], $body);
42 $this->assertFalse($streamIsRead);
43 $this->assertSame($body, $r->getBody());
44 }
45
46 public function testStatusCanBeNumericString()
47 {
48 $r = new Response('404');
49 $r2 = $r->withStatus('201');
50 $this->assertSame(404, $r->getStatusCode());
51 $this->assertSame('Not Found', $r->getReasonPhrase());
52 $this->assertSame(201, $r2->getStatusCode());
53 $this->assertSame('Created', $r2->getReasonPhrase());
54 }
55
56 public function testCanConstructWithHeaders()
57 {
58 $r = new Response(200, ['Foo' => 'Bar']);
59 $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
60 $this->assertSame('Bar', $r->getHeaderLine('Foo'));
61 $this->assertSame(['Bar'], $r->getHeader('Foo'));
62 }
63
64 public function testCanConstructWithHeadersAsArray()
65 {
66 $r = new Response(200, [
67 'Foo' => ['baz', 'bar']
68 ]);
69 $this->assertSame(['Foo' => ['baz', 'bar']], $r->getHeaders());
70 $this->assertSame('baz, bar', $r->getHeaderLine('Foo'));
71 $this->assertSame(['baz', 'bar'], $r->getHeader('Foo'));
72 }
73
74 public function testCanConstructWithBody()
75 {
76 $r = new Response(200, [], 'baz');
77 $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
78 $this->assertSame('baz', (string) $r->getBody());
79 }
80
81 public function testNullBody()
82 {
83 $r = new Response(200, [], null);
84 $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
85 $this->assertSame('', (string) $r->getBody());
86 }
87
88 public function testFalseyBody()
89 {
90 $r = new Response(200, [], '0');
91 $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
92 $this->assertSame('0', (string) $r->getBody());
93 }
94
95 public function testCanConstructWithReason()
96 {
97 $r = new Response(200, [], null, '1.1', 'bar');
98 $this->assertSame('bar', $r->getReasonPhrase());
99
100 $r = new Response(200, [], null, '1.1', '0');
101 $this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works');
102 }
103
104 public function testCanConstructWithProtocolVersion()
105 {
106 $r = new Response(200, [], null, '1000');
107 $this->assertSame('1000', $r->getProtocolVersion());
108 }
109
110 public function testWithStatusCodeAndNoReason()
111 {
112 $r = (new Response())->withStatus(201);
113 $this->assertSame(201, $r->getStatusCode());
114 $this->assertSame('Created', $r->getReasonPhrase());
115 }
116
117 public function testWithStatusCodeAndReason()
118 {
119 $r = (new Response())->withStatus(201, 'Foo');
120 $this->assertSame(201, $r->getStatusCode());
121 $this->assertSame('Foo', $r->getReasonPhrase());
122
123 $r = (new Response())->withStatus(201, '0');
124 $this->assertSame(201, $r->getStatusCode());
125 $this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works');
126 }
127
128 public function testWithProtocolVersion()
129 {
130 $r = (new Response())->withProtocolVersion('1000');
131 $this->assertSame('1000', $r->getProtocolVersion());
132 }
133
134 public function testSameInstanceWhenSameProtocol()
135 {
136 $r = new Response();
137 $this->assertSame($r, $r->withProtocolVersion('1.1'));
138 }
139
140 public function testWithBody()
141 {
142 $b = Psr7\stream_for('0');
143 $r = (new Response())->withBody($b);
144 $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
145 $this->assertSame('0', (string) $r->getBody());
146 }
147
148 public function testSameInstanceWhenSameBody()
149 {
150 $r = new Response();
151 $b = $r->getBody();
152 $this->assertSame($r, $r->withBody($b));
153 }
154
155 public function testWithHeader()
156 {
157 $r = new Response(200, ['Foo' => 'Bar']);
158 $r2 = $r->withHeader('baZ', 'Bam');
159 $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
160 $this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam']], $r2->getHeaders());
161 $this->assertSame('Bam', $r2->getHeaderLine('baz'));
162 $this->assertSame(['Bam'], $r2->getHeader('baz'));
163 }
164
165 public function testNumericHeaderValue()
166 {
167 $r = (new Response())->withHeader('Api-Version', 1);
168 $this->assertSame(['Api-Version' => ['1']], $r->getHeaders());
169 }
170
171 public function testWithHeaderAsArray()
172 {
173 $r = new Response(200, ['Foo' => 'Bar']);
174 $r2 = $r->withHeader('baZ', ['Bam', 'Bar']);
175 $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
176 $this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam', 'Bar']], $r2->getHeaders());
177 $this->assertSame('Bam, Bar', $r2->getHeaderLine('baz'));
178 $this->assertSame(['Bam', 'Bar'], $r2->getHeader('baz'));
179 }
180
181 public function testWithHeaderReplacesDifferentCase()
182 {
183 $r = new Response(200, ['Foo' => 'Bar']);
184 $r2 = $r->withHeader('foO', 'Bam');
185 $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
186 $this->assertSame(['foO' => ['Bam']], $r2->getHeaders());
187 $this->assertSame('Bam', $r2->getHeaderLine('foo'));
188 $this->assertSame(['Bam'], $r2->getHeader('foo'));
189 }
190
191 public function testWithAddedHeader()
192 {
193 $r = new Response(200, ['Foo' => 'Bar']);
194 $r2 = $r->withAddedHeader('foO', 'Baz');
195 $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
196 $this->assertSame(['Foo' => ['Bar', 'Baz']], $r2->getHeaders());
197 $this->assertSame('Bar, Baz', $r2->getHeaderLine('foo'));
198 $this->assertSame(['Bar', 'Baz'], $r2->getHeader('foo'));
199 }
200
201 public function testWithAddedHeaderAsArray()
202 {
203 $r = new Response(200, ['Foo' => 'Bar']);
204 $r2 = $r->withAddedHeader('foO', ['Baz', 'Bam']);
205 $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
206 $this->assertSame(['Foo' => ['Bar', 'Baz', 'Bam']], $r2->getHeaders());
207 $this->assertSame('Bar, Baz, Bam', $r2->getHeaderLine('foo'));
208 $this->assertSame(['Bar', 'Baz', 'Bam'], $r2->getHeader('foo'));
209 }
210
211 public function testWithAddedHeaderThatDoesNotExist()
212 {
213 $r = new Response(200, ['Foo' => 'Bar']);
214 $r2 = $r->withAddedHeader('nEw', 'Baz');
215 $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
216 $this->assertSame(['Foo' => ['Bar'], 'nEw' => ['Baz']], $r2->getHeaders());
217 $this->assertSame('Baz', $r2->getHeaderLine('new'));
218 $this->assertSame(['Baz'], $r2->getHeader('new'));
219 }
220
221 public function testWithoutHeaderThatExists()
222 {
223 $r = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']);
224 $r2 = $r->withoutHeader('foO');
225 $this->assertTrue($r->hasHeader('foo'));
226 $this->assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $r->getHeaders());
227 $this->assertFalse($r2->hasHeader('foo'));
228 $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders());
229 }
230
231 public function testWithoutHeaderThatDoesNotExist()
232 {
233 $r = new Response(200, ['Baz' => 'Bam']);
234 $r2 = $r->withoutHeader('foO');
235 $this->assertSame($r, $r2);
236 $this->assertFalse($r2->hasHeader('foo'));
237 $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders());
238 }
239
240 public function testSameInstanceWhenRemovingMissingHeader()
241 {
242 $r = new Response();
243 $this->assertSame($r, $r->withoutHeader('foo'));
244 }
245
246 public function testPassNumericHeaderNameInConstructor()
247 {
248 $r = new Response(200, ['Location' => 'foo', '123' => 'bar']);
249 $this->assertSame('bar', $r->getHeaderLine('123'));
250 }
251
252 /**
253 * @dataProvider invalidHeaderProvider
254 */
255 public function testConstructResponseInvalidHeader($header, $headerValue, $expectedMessage)
256 {
257 $this->expectException('InvalidArgumentException', $expectedMessage);
258 new Response(200, [$header => $headerValue]);
259 }
260
261 public function invalidHeaderProvider()
262 {
263 return [
264 ['foo', [], 'Header value can not be an empty array.'],
265 ['', '', 'Header name can not be empty.'],
266 ['foo', new \stdClass(), 'Header value must be scalar or null but stdClass provided.'],
267 ];
268 }
269
270 /**
271 * @dataProvider invalidWithHeaderProvider
272 */
273 public function testWithInvalidHeader($header, $headerValue, $expectedMessage)
274 {
275 $r = new Response();
276 $this->expectException('InvalidArgumentException', $expectedMessage);
277 $r->withHeader($header, $headerValue);
278 }
279
280 public function invalidWithHeaderProvider()
281 {
282 return array_merge($this->invalidHeaderProvider(), [
283 [[], 'foo', 'Header name must be a string but array provided.'],
284 [false, 'foo', 'Header name must be a string but boolean provided.'],
285 [new \stdClass(), 'foo', 'Header name must be a string but stdClass provided.'],
286 ]);
287 }
288
289 public function testHeaderValuesAreTrimmed()
290 {
291 $r1 = new Response(200, ['OWS' => " \t \tFoo\t \t "]);
292 $r2 = (new Response())->withHeader('OWS', " \t \tFoo\t \t ");
293 $r3 = (new Response())->withAddedHeader('OWS', " \t \tFoo\t \t ");;
294
295 foreach ([$r1, $r2, $r3] as $r) {
296 $this->assertSame(['OWS' => ['Foo']], $r->getHeaders());
297 $this->assertSame('Foo', $r->getHeaderLine('OWS'));
298 $this->assertSame(['Foo'], $r->getHeader('OWS'));
299 }
300 }
301
302 /**
303 * @dataProvider nonIntegerStatusCodeProvider
304 * @param mixed $invalidValues
305 */
306 public function testConstructResponseWithNonIntegerStatusCode($invalidValues)
307 {
308 $this->expectException('InvalidArgumentException', 'Status code must be an integer value.');
309 new Response($invalidValues);
310 }
311
312 /**
313 * @dataProvider nonIntegerStatusCodeProvider
314 * @param mixed $invalidValues
315 */
316 public function testResponseChangeStatusCodeWithNonInteger($invalidValues)
317 {
318 $response = new Response();
319 $this->expectException('InvalidArgumentException', 'Status code must be an integer value.');
320 $response->withStatus($invalidValues);
321 }
322
323 public function nonIntegerStatusCodeProvider()
324 {
325 return [
326 ['whatever'],
327 ['1.01'],
328 [1.01],
329 [new \stdClass()],
330 ];
331 }
332
333 /**
334 * @dataProvider invalidStatusCodeRangeProvider
335 * @param mixed $invalidValues
336 */
337 public function testConstructResponseWithInvalidRangeStatusCode($invalidValues)
338 {
339 $this->expectException('InvalidArgumentException', 'Status code must be an integer value between 1xx and 5xx.');
340 new Response($invalidValues);
341 }
342
343 /**
344 * @dataProvider invalidStatusCodeRangeProvider
345 * @param mixed $invalidValues
346 */
347 public function testResponseChangeStatusCodeWithWithInvalidRange($invalidValues)
348 {
349 $response = new Response();
350 $this->expectException('InvalidArgumentException', 'Status code must be an integer value between 1xx and 5xx.');
351 $response->withStatus($invalidValues);
352 }
353
354 public function invalidStatusCodeRangeProvider()
355 {
356 return [
357 [600],
358 [99],
359 ];
360 }
361 }
362