scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Message; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use GuzzleHttp\Cookie\CookieJar; |
||
7 | use GuzzleHttp\Message\MessageFactory; |
||
8 | use GuzzleHttp\Message\Response; |
||
9 | use GuzzleHttp\Post\PostFile; |
||
10 | use GuzzleHttp\Query; |
||
11 | use GuzzleHttp\Stream\Stream; |
||
12 | use GuzzleHttp\Subscriber\Cookie; |
||
13 | use GuzzleHttp\Subscriber\History; |
||
14 | use GuzzleHttp\Subscriber\Mock; |
||
15 | |||
16 | /** |
||
17 | * @covers GuzzleHttp\Message\MessageFactory |
||
18 | */ |
||
19 | class MessageFactoryTest extends \PHPUnit_Framework_TestCase |
||
20 | { |
||
21 | public function testCreatesResponses() |
||
22 | { |
||
23 | $f = new MessageFactory(); |
||
24 | $response = $f->createResponse(200, ['foo' => 'bar'], 'test', [ |
||
25 | 'protocol_version' => 1.0 |
||
26 | ]); |
||
27 | $this->assertEquals(200, $response->getStatusCode()); |
||
28 | $this->assertEquals(['foo' => ['bar']], $response->getHeaders()); |
||
29 | $this->assertEquals('test', $response->getBody()); |
||
30 | $this->assertEquals(1.0, $response->getProtocolVersion()); |
||
31 | } |
||
32 | |||
33 | public function testCreatesRequestFromMessage() |
||
34 | { |
||
35 | $f = new MessageFactory(); |
||
36 | $req = $f->fromMessage("GET / HTTP/1.1\r\nBaz: foo\r\n\r\n"); |
||
37 | $this->assertEquals('GET', $req->getMethod()); |
||
38 | $this->assertEquals('/', $req->getPath()); |
||
39 | $this->assertEquals('foo', $req->getHeader('Baz')); |
||
40 | $this->assertNull($req->getBody()); |
||
41 | } |
||
42 | |||
43 | public function testCreatesRequestFromMessageWithBody() |
||
44 | { |
||
45 | $req = (new MessageFactory())->fromMessage("GET / HTTP/1.1\r\nBaz: foo\r\n\r\ntest"); |
||
46 | $this->assertEquals('test', $req->getBody()); |
||
47 | } |
||
48 | |||
49 | public function testCreatesRequestWithPostBody() |
||
50 | { |
||
51 | $req = (new MessageFactory())->createRequest('GET', 'http://www.foo.com', ['body' => ['abc' => '123']]); |
||
52 | $this->assertEquals('abc=123', $req->getBody()); |
||
53 | } |
||
54 | |||
55 | public function testCreatesRequestWithPostBodyScalars() |
||
56 | { |
||
57 | $req = (new MessageFactory())->createRequest( |
||
58 | 'GET', |
||
59 | 'http://www.foo.com', |
||
60 | ['body' => [ |
||
61 | 'abc' => true, |
||
62 | '123' => false, |
||
63 | 'foo' => null, |
||
64 | 'baz' => 10, |
||
65 | 'bam' => 1.5, |
||
66 | 'boo' => [1]] |
||
67 | ] |
||
68 | ); |
||
69 | $this->assertEquals( |
||
70 | 'abc=1&123=&foo&baz=10&bam=1.5&boo%5B0%5D=1', |
||
71 | (string) $req->getBody() |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | public function testCreatesRequestWithPostBodyAndPostFiles() |
||
76 | { |
||
77 | $pf = fopen(__FILE__, 'r'); |
||
78 | $pfi = new PostFile('ghi', 'abc', __FILE__); |
||
79 | $req = (new MessageFactory())->createRequest('GET', 'http://www.foo.com', [ |
||
80 | 'body' => [ |
||
81 | 'abc' => '123', |
||
82 | 'def' => $pf, |
||
83 | 'ghi' => $pfi |
||
84 | ] |
||
85 | ]); |
||
86 | $this->assertInstanceOf('GuzzleHttp\Post\PostBody', $req->getBody()); |
||
87 | $s = (string) $req; |
||
88 | $this->assertContains('testCreatesRequestWithPostBodyAndPostFiles', $s); |
||
89 | $this->assertContains('multipart/form-data', $s); |
||
90 | $this->assertTrue(in_array($pfi, $req->getBody()->getFiles(), true)); |
||
91 | } |
||
92 | |||
93 | public function testCreatesResponseFromMessage() |
||
94 | { |
||
95 | $response = (new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest"); |
||
96 | $this->assertEquals(200, $response->getStatusCode()); |
||
97 | $this->assertEquals('OK', $response->getReasonPhrase()); |
||
98 | $this->assertEquals('4', $response->getHeader('Content-Length')); |
||
99 | $this->assertEquals('test', $response->getBody(true)); |
||
100 | } |
||
101 | |||
102 | public function testCanCreateHeadResponses() |
||
103 | { |
||
104 | $response = (new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\n"); |
||
105 | $this->assertEquals(200, $response->getStatusCode()); |
||
106 | $this->assertEquals('OK', $response->getReasonPhrase()); |
||
107 | $this->assertEquals(null, $response->getBody()); |
||
108 | $this->assertEquals('4', $response->getHeader('Content-Length')); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @expectedException \InvalidArgumentException |
||
113 | */ |
||
114 | public function testFactoryRequiresMessageForRequest() |
||
115 | { |
||
116 | (new MessageFactory())->fromMessage(''); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @expectedException \InvalidArgumentException |
||
121 | * @expectedExceptionMessage foo |
||
122 | */ |
||
123 | public function testValidatesOptionsAreImplemented() |
||
124 | { |
||
125 | (new MessageFactory())->createRequest('GET', 'http://test.com', ['foo' => 'bar']); |
||
126 | } |
||
127 | |||
128 | public function testOptionsAddsRequestOptions() |
||
129 | { |
||
130 | $request = (new MessageFactory())->createRequest( |
||
131 | 'GET', 'http://test.com', ['config' => ['baz' => 'bar']] |
||
132 | ); |
||
133 | $this->assertEquals('bar', $request->getConfig()->get('baz')); |
||
134 | } |
||
135 | |||
136 | public function testCanDisableRedirects() |
||
137 | { |
||
138 | $request = (new MessageFactory())->createRequest('GET', '/', ['allow_redirects' => false]); |
||
139 | $this->assertEmpty($request->getEmitter()->listeners('complete')); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @expectedException \InvalidArgumentException |
||
144 | */ |
||
145 | public function testValidatesRedirects() |
||
146 | { |
||
147 | (new MessageFactory())->createRequest('GET', '/', ['allow_redirects' => []]); |
||
148 | } |
||
149 | |||
150 | public function testCanEnableStrictRedirectsAndSpecifyMax() |
||
151 | { |
||
152 | $request = (new MessageFactory())->createRequest('GET', '/', [ |
||
153 | 'allow_redirects' => ['max' => 10, 'strict' => true] |
||
154 | ]); |
||
155 | $this->assertTrue($request->getConfig()['redirect']['strict']); |
||
156 | $this->assertEquals(10, $request->getConfig()['redirect']['max']); |
||
157 | } |
||
158 | |||
159 | public function testCanAddCookiesFromHash() |
||
160 | { |
||
161 | $request = (new MessageFactory())->createRequest('GET', 'http://www.test.com/', [ |
||
162 | 'cookies' => ['Foo' => 'Bar'] |
||
163 | ]); |
||
164 | $cookies = null; |
||
165 | foreach ($request->getEmitter()->listeners('before') as $l) { |
||
166 | if ($l[0] instanceof Cookie) { |
||
167 | $cookies = $l[0]; |
||
168 | break; |
||
169 | } |
||
170 | } |
||
171 | if (!$cookies) { |
||
172 | $this->fail('Did not add cookie listener'); |
||
173 | } else { |
||
174 | $this->assertCount(1, $cookies->getCookieJar()); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | public function testAddsCookieUsingTrue() |
||
179 | { |
||
180 | $factory = new MessageFactory(); |
||
181 | $request1 = $factory->createRequest('GET', '/', ['cookies' => true]); |
||
182 | $request2 = $factory->createRequest('GET', '/', ['cookies' => true]); |
||
183 | $listeners = function ($r) { |
||
184 | return array_filter($r->getEmitter()->listeners('before'), function ($l) { |
||
185 | return $l[0] instanceof Cookie; |
||
186 | }); |
||
187 | }; |
||
188 | $this->assertSame($listeners($request1), $listeners($request2)); |
||
189 | } |
||
190 | |||
191 | public function testAddsCookieFromCookieJar() |
||
192 | { |
||
193 | $jar = new CookieJar(); |
||
194 | $request = (new MessageFactory())->createRequest('GET', '/', ['cookies' => $jar]); |
||
195 | foreach ($request->getEmitter()->listeners('before') as $l) { |
||
196 | if ($l[0] instanceof Cookie) { |
||
197 | $this->assertSame($jar, $l[0]->getCookieJar()); |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @expectedException \InvalidArgumentException |
||
204 | */ |
||
205 | public function testValidatesCookies() |
||
206 | { |
||
207 | (new MessageFactory())->createRequest('GET', '/', ['cookies' => 'baz']); |
||
208 | } |
||
209 | |||
210 | public function testCanAddQuery() |
||
211 | { |
||
212 | $request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [ |
||
213 | 'query' => ['Foo' => 'Bar'] |
||
214 | ]); |
||
215 | $this->assertEquals('Bar', $request->getQuery()->get('Foo')); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @expectedException \InvalidArgumentException |
||
220 | */ |
||
221 | public function testValidatesQuery() |
||
222 | { |
||
223 | (new MessageFactory())->createRequest('GET', 'http://foo.com', [ |
||
224 | 'query' => 'foo' |
||
225 | ]); |
||
226 | } |
||
227 | |||
228 | public function testCanSetDefaultQuery() |
||
229 | { |
||
230 | $request = (new MessageFactory())->createRequest('GET', 'http://foo.com?test=abc', [ |
||
231 | 'query' => ['Foo' => 'Bar', 'test' => 'def'] |
||
232 | ]); |
||
233 | $this->assertEquals('Bar', $request->getQuery()->get('Foo')); |
||
234 | $this->assertEquals('abc', $request->getQuery()->get('test')); |
||
235 | } |
||
236 | |||
237 | public function testCanSetDefaultQueryWithObject() |
||
238 | { |
||
239 | $request = (new MessageFactory())->createRequest('GET', 'http://foo.com?test=abc', [ |
||
240 | 'query' => new Query(['Foo' => 'Bar', 'test' => 'def']) |
||
241 | ]); |
||
242 | $this->assertEquals('Bar', $request->getQuery()->get('Foo')); |
||
243 | $this->assertEquals('abc', $request->getQuery()->get('test')); |
||
244 | } |
||
245 | |||
246 | public function testCanAddBasicAuth() |
||
247 | { |
||
248 | $request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [ |
||
249 | 'auth' => ['michael', 'test'] |
||
250 | ]); |
||
251 | $this->assertTrue($request->hasHeader('Authorization')); |
||
252 | } |
||
253 | |||
254 | public function testCanAddDigestAuth() |
||
255 | { |
||
256 | $request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [ |
||
257 | 'auth' => ['michael', 'test', 'digest'] |
||
258 | ]); |
||
259 | $this->assertEquals('michael:test', $request->getConfig()->getPath('curl/' . CURLOPT_USERPWD)); |
||
260 | $this->assertEquals(CURLAUTH_DIGEST, $request->getConfig()->getPath('curl/' . CURLOPT_HTTPAUTH)); |
||
261 | } |
||
262 | |||
263 | public function testCanDisableAuth() |
||
264 | { |
||
265 | $request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [ |
||
266 | 'auth' => false |
||
267 | ]); |
||
268 | $this->assertFalse($request->hasHeader('Authorization')); |
||
269 | } |
||
270 | |||
271 | public function testCanSetCustomAuth() |
||
272 | { |
||
273 | $request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [ |
||
274 | 'auth' => 'foo' |
||
275 | ]); |
||
276 | $this->assertEquals('foo', $request->getConfig()['auth']); |
||
277 | } |
||
278 | |||
279 | public function testCanAddEvents() |
||
280 | { |
||
281 | $foo = null; |
||
282 | $client = new Client(); |
||
283 | $client->getEmitter()->attach(new Mock([new Response(200)])); |
||
284 | $client->get('http://test.com', [ |
||
285 | 'events' => [ |
||
286 | 'before' => function () use (&$foo) { $foo = true; } |
||
287 | ] |
||
288 | ]); |
||
289 | $this->assertTrue($foo); |
||
290 | } |
||
291 | |||
292 | public function testCanAddEventsWithPriority() |
||
293 | { |
||
294 | $foo = null; |
||
295 | $client = new Client(); |
||
296 | $client->getEmitter()->attach(new Mock(array(new Response(200)))); |
||
297 | $request = $client->createRequest('GET', 'http://test.com', [ |
||
298 | 'events' => [ |
||
299 | 'before' => [ |
||
300 | 'fn' => function () use (&$foo) { $foo = true; }, |
||
301 | 'priority' => 123 |
||
302 | ] |
||
303 | ] |
||
304 | ]); |
||
305 | $client->send($request); |
||
306 | $this->assertTrue($foo); |
||
307 | $l = $this->readAttribute($request->getEmitter(), 'listeners'); |
||
308 | $this->assertArrayHasKey(123, $l['before']); |
||
309 | } |
||
310 | |||
311 | public function testCanAddEventsOnce() |
||
312 | { |
||
313 | $foo = 0; |
||
314 | $client = new Client(); |
||
315 | $client->getEmitter()->attach(new Mock([ |
||
316 | new Response(200), |
||
317 | new Response(200), |
||
318 | ])); |
||
319 | $fn = function () use (&$foo) { ++$foo; }; |
||
320 | $request = $client->createRequest('GET', 'http://test.com', [ |
||
321 | 'events' => ['before' => ['fn' => $fn, 'once' => true]] |
||
322 | ]); |
||
323 | $client->send($request); |
||
324 | $this->assertEquals(1, $foo); |
||
325 | $client->send($request); |
||
326 | $this->assertEquals(1, $foo); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @expectedException \InvalidArgumentException |
||
331 | */ |
||
332 | public function testValidatesEventContainsFn() |
||
333 | { |
||
334 | $client = new Client(['base_url' => 'http://test.com']); |
||
335 | $client->createRequest('GET', '/', ['events' => ['before' => ['foo' => 'bar']]]); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @expectedException \InvalidArgumentException |
||
340 | */ |
||
341 | public function testValidatesEventIsArray() |
||
342 | { |
||
343 | $client = new Client(['base_url' => 'http://test.com']); |
||
344 | $client->createRequest('GET', '/', ['events' => ['before' => '123']]); |
||
345 | } |
||
346 | |||
347 | public function testCanAddSubscribers() |
||
348 | { |
||
349 | $mock = new Mock([new Response(200)]); |
||
350 | $client = new Client(); |
||
351 | $client->getEmitter()->attach($mock); |
||
352 | $request = $client->get('http://test.com', ['subscribers' => [$mock]]); |
||
353 | } |
||
354 | |||
355 | public function testCanDisableExceptions() |
||
356 | { |
||
357 | $client = new Client(); |
||
358 | $this->assertEquals(500, $client->get('http://test.com', [ |
||
359 | 'subscribers' => [new Mock([new Response(500)])], |
||
360 | 'exceptions' => false |
||
361 | ])->getStatusCode()); |
||
362 | } |
||
363 | |||
364 | public function testCanChangeSaveToLocation() |
||
365 | { |
||
366 | $saveTo = Stream::factory(); |
||
367 | $request = (new MessageFactory())->createRequest('GET', '/', ['save_to' => $saveTo]); |
||
368 | $this->assertSame($saveTo, $request->getConfig()->get('save_to')); |
||
369 | } |
||
370 | |||
371 | public function testCanSetProxy() |
||
372 | { |
||
373 | $request = (new MessageFactory())->createRequest('GET', '/', ['proxy' => '192.168.16.121']); |
||
374 | $this->assertEquals('192.168.16.121', $request->getConfig()->get('proxy')); |
||
375 | } |
||
376 | |||
377 | public function testCanSetHeadersOption() |
||
378 | { |
||
379 | $request = (new MessageFactory())->createRequest('GET', '/', ['headers' => ['Foo' => 'Bar']]); |
||
380 | $this->assertEquals('Bar', (string) $request->getHeader('Foo')); |
||
381 | } |
||
382 | |||
383 | public function testCanSetHeaders() |
||
384 | { |
||
385 | $request = (new MessageFactory())->createRequest('GET', '/', [ |
||
386 | 'headers' => ['Foo' => ['Baz', 'Bar'], 'Test' => '123'] |
||
387 | ]); |
||
388 | $this->assertEquals('Baz, Bar', $request->getHeader('Foo')); |
||
389 | $this->assertEquals('123', $request->getHeader('Test')); |
||
390 | } |
||
391 | |||
392 | public function testCanSetTimeoutOption() |
||
393 | { |
||
394 | $request = (new MessageFactory())->createRequest('GET', '/', ['timeout' => 1.5]); |
||
395 | $this->assertEquals(1.5, $request->getConfig()->get('timeout')); |
||
396 | } |
||
397 | |||
398 | public function testCanSetConnectTimeoutOption() |
||
399 | { |
||
400 | $request = (new MessageFactory())->createRequest('GET', '/', ['connect_timeout' => 1.5]); |
||
401 | $this->assertEquals(1.5, $request->getConfig()->get('connect_timeout')); |
||
402 | } |
||
403 | |||
404 | public function testCanSetDebug() |
||
405 | { |
||
406 | $request = (new MessageFactory())->createRequest('GET', '/', ['debug' => true]); |
||
407 | $this->assertTrue($request->getConfig()->get('debug')); |
||
408 | } |
||
409 | |||
410 | public function testCanSetVerifyToOff() |
||
411 | { |
||
412 | $request = (new MessageFactory())->createRequest('GET', '/', ['verify' => false]); |
||
413 | $this->assertFalse($request->getConfig()->get('verify')); |
||
414 | } |
||
415 | |||
416 | public function testCanSetVerifyToOn() |
||
417 | { |
||
418 | $request = (new MessageFactory())->createRequest('GET', '/', ['verify' => true]); |
||
419 | $this->assertTrue($request->getConfig()->get('verify')); |
||
420 | } |
||
421 | |||
422 | public function testCanSetVerifyToPath() |
||
423 | { |
||
424 | $request = (new MessageFactory())->createRequest('GET', '/', ['verify' => '/foo.pem']); |
||
425 | $this->assertEquals('/foo.pem', $request->getConfig()->get('verify')); |
||
426 | } |
||
427 | |||
428 | public function inputValidation() |
||
429 | { |
||
430 | return array_map(function ($option) { return array($option); }, array( |
||
431 | 'headers', 'events', 'subscribers', 'params' |
||
432 | )); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @dataProvider inputValidation |
||
437 | * @expectedException \InvalidArgumentException |
||
438 | */ |
||
439 | public function testValidatesInput($option) |
||
440 | { |
||
441 | (new MessageFactory())->createRequest('GET', '/', [$option => 'foo']); |
||
442 | } |
||
443 | |||
444 | public function testCanAddSslKey() |
||
445 | { |
||
446 | $request = (new MessageFactory())->createRequest('GET', '/', ['ssl_key' => '/foo.pem']); |
||
447 | $this->assertEquals('/foo.pem', $request->getConfig()->get('ssl_key')); |
||
448 | } |
||
449 | |||
450 | public function testCanAddSslKeyPassword() |
||
451 | { |
||
452 | $request = (new MessageFactory())->createRequest('GET', '/', ['ssl_key' => ['/foo.pem', 'bar']]); |
||
453 | $this->assertEquals(['/foo.pem', 'bar'], $request->getConfig()->get('ssl_key')); |
||
454 | } |
||
455 | |||
456 | public function testCanAddSslCert() |
||
457 | { |
||
458 | $request = (new MessageFactory())->createRequest('GET', '/', ['cert' => '/foo.pem']); |
||
459 | $this->assertEquals('/foo.pem', $request->getConfig()->get('cert')); |
||
460 | } |
||
461 | |||
462 | public function testCanAddSslCertPassword() |
||
463 | { |
||
464 | $request = (new MessageFactory())->createRequest('GET', '/', ['cert' => ['/foo.pem', 'bar']]); |
||
465 | $this->assertEquals(['/foo.pem', 'bar'], $request->getConfig()->get('cert')); |
||
466 | } |
||
467 | |||
468 | public function testCreatesBodyWithoutZeroString() |
||
469 | { |
||
470 | $request = (new MessageFactory())->createRequest('PUT', 'http://test.com', ['body' => '0']); |
||
471 | $this->assertSame('0', (string) $request->getBody()); |
||
472 | } |
||
473 | |||
474 | public function testCanSetProtocolVersion() |
||
475 | { |
||
476 | $request = (new MessageFactory())->createRequest('GET', 'http://t.com', ['version' => 1.0]); |
||
477 | $this->assertEquals(1.0, $request->getProtocolVersion()); |
||
478 | } |
||
479 | |||
480 | public function testCanAddJsonData() |
||
481 | { |
||
482 | $request = (new MessageFactory())->createRequest('PUT', 'http://f.com', [ |
||
483 | 'json' => ['foo' => 'bar'] |
||
484 | ]); |
||
485 | $this->assertEquals( |
||
486 | 'application/json', |
||
487 | $request->getHeader('Content-Type') |
||
488 | ); |
||
489 | $this->assertEquals('{"foo":"bar"}', (string) $request->getBody()); |
||
490 | } |
||
491 | |||
492 | public function testCanAddJsonDataToAPostRequest() |
||
493 | { |
||
494 | $request = (new MessageFactory())->createRequest('POST', 'http://f.com', [ |
||
495 | 'json' => ['foo' => 'bar'] |
||
496 | ]); |
||
497 | $this->assertEquals( |
||
498 | 'application/json', |
||
499 | $request->getHeader('Content-Type') |
||
500 | ); |
||
501 | $this->assertEquals('{"foo":"bar"}', (string) $request->getBody()); |
||
502 | } |
||
503 | |||
504 | public function testCanAddJsonDataAndNotOverwriteContentType() |
||
505 | { |
||
506 | $request = (new MessageFactory())->createRequest('PUT', 'http://f.com', [ |
||
507 | 'headers' => ['Content-Type' => 'foo'], |
||
508 | 'json' => null |
||
509 | ]); |
||
510 | $this->assertEquals('foo', $request->getHeader('Content-Type')); |
||
511 | $this->assertEquals('null', (string) $request->getBody()); |
||
512 | } |
||
513 | |||
514 | public function testCanUseCustomSubclassesWithMethods() |
||
515 | { |
||
516 | (new ExtendedFactory())->createRequest('PUT', 'http://f.com', [ |
||
517 | 'headers' => ['Content-Type' => 'foo'], |
||
518 | 'foo' => 'bar' |
||
519 | ]); |
||
520 | try { |
||
521 | $f = new MessageFactory(); |
||
522 | $f->createRequest('PUT', 'http://f.com', [ |
||
523 | 'headers' => ['Content-Type' => 'foo'], |
||
524 | 'foo' => 'bar' |
||
525 | ]); |
||
526 | } catch (\InvalidArgumentException $e) { |
||
527 | $this->assertContains('foo config', $e->getMessage()); |
||
528 | } |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * @ticket https://github.com/guzzle/guzzle/issues/706 |
||
533 | */ |
||
534 | public function testDoesNotApplyPostBodyRightAway() |
||
535 | { |
||
536 | $request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [ |
||
537 | 'body' => ['foo' => ['bar', 'baz']] |
||
538 | ]); |
||
539 | $this->assertEquals('', $request->getHeader('Content-Type')); |
||
540 | $this->assertEquals('', $request->getHeader('Content-Length')); |
||
541 | $request->getBody()->setAggregator(Query::duplicateAggregator()); |
||
542 | $request->getBody()->applyRequestHeaders($request); |
||
543 | $this->assertEquals('foo=bar&foo=baz', $request->getBody()); |
||
544 | } |
||
545 | |||
546 | public function testCanForceMultipartUploadWithContentType() |
||
547 | { |
||
548 | $client = new Client(); |
||
549 | $client->getEmitter()->attach(new Mock([new Response(200)])); |
||
550 | $history = new History(); |
||
551 | $client->getEmitter()->attach($history); |
||
552 | $client->post('http://foo.com', [ |
||
553 | 'headers' => ['Content-Type' => 'multipart/form-data'], |
||
554 | 'body' => ['foo' => 'bar'] |
||
555 | ]); |
||
556 | $this->assertContains( |
||
557 | 'multipart/form-data; boundary=', |
||
558 | $history->getLastRequest()->getHeader('Content-Type') |
||
559 | ); |
||
560 | $this->assertContains( |
||
561 | "Content-Disposition: form-data; name=\"foo\"\r\n\r\nbar", |
||
562 | (string) $history->getLastRequest()->getBody() |
||
563 | ); |
||
564 | } |
||
565 | |||
566 | public function testDecodeDoesNotForceAcceptHeader() |
||
567 | { |
||
568 | $request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [ |
||
569 | 'decode_content' => true |
||
570 | ]); |
||
571 | $this->assertEquals('', $request->getHeader('Accept-Encoding')); |
||
572 | $this->assertTrue($request->getConfig()->get('decode_content')); |
||
573 | } |
||
574 | |||
575 | public function testDecodeCanAddAcceptHeader() |
||
576 | { |
||
577 | $request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [ |
||
578 | 'decode_content' => 'gzip' |
||
579 | ]); |
||
580 | $this->assertEquals('gzip', $request->getHeader('Accept-Encoding')); |
||
581 | $this->assertTrue($request->getConfig()->get('decode_content')); |
||
582 | } |
||
583 | |||
584 | public function testCanDisableDecoding() |
||
585 | { |
||
586 | $request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [ |
||
587 | 'decode_content' => false |
||
588 | ]); |
||
589 | $this->assertEquals('', $request->getHeader('Accept-Encoding')); |
||
590 | $this->assertNull($request->getConfig()->get('decode_content')); |
||
591 | } |
||
592 | } |
||
593 | |||
594 | class ExtendedFactory extends MessageFactory |
||
595 | { |
||
596 | protected function add_foo() {} |
||
597 | } |