| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests; |
| 3 |
|
| 4 |
use GuzzleHttp\Client; |
| 5 |
use GuzzleHttp\Cookie\CookieJar; |
| 6 |
use GuzzleHttp\Handler\MockHandler; |
| 7 |
use GuzzleHttp\HandlerStack; |
| 8 |
use GuzzleHttp\Promise\PromiseInterface; |
| 9 |
use GuzzleHttp\Psr7; |
| 10 |
use GuzzleHttp\Psr7\Request; |
| 11 |
use GuzzleHttp\Psr7\Response; |
| 12 |
use GuzzleHttp\Psr7\Uri; |
| 13 |
use PHPUnit\Framework\TestCase; |
| 14 |
use Psr\Http\Message\ResponseInterface; |
| 15 |
|
| 16 |
class ClientTest extends TestCase |
| 17 |
{ |
| 18 |
public function testUsesDefaultHandler() |
| 19 |
{ |
| 20 |
$client = new Client(); |
| 21 |
Server::enqueue([new Response(200, ['Content-Length' => 0])]); |
| 22 |
$response = $client->get(Server::$url); |
| 23 |
self::assertSame(200, $response->getStatusCode()); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @expectedException \InvalidArgumentException |
| 28 |
* @expectedExceptionMessage Magic request methods require a URI and optional options array |
| 29 |
*/ |
| 30 |
public function testValidatesArgsForMagicMethods() |
| 31 |
{ |
| 32 |
$client = new Client(); |
| 33 |
$client->get(); |
| 34 |
} |
| 35 |
|
| 36 |
public function testCanSendMagicAsyncRequests() |
| 37 |
{ |
| 38 |
$client = new Client(); |
| 39 |
Server::flush(); |
| 40 |
Server::enqueue([new Response(200, ['Content-Length' => 2], 'hi')]); |
| 41 |
$p = $client->getAsync(Server::$url, ['query' => ['test' => 'foo']]); |
| 42 |
self::assertInstanceOf(PromiseInterface::class, $p); |
| 43 |
self::assertSame(200, $p->wait()->getStatusCode()); |
| 44 |
$received = Server::received(true); |
| 45 |
self::assertCount(1, $received); |
| 46 |
self::assertSame('test=foo', $received[0]->getUri()->getQuery()); |
| 47 |
} |
| 48 |
|
| 49 |
public function testCanSendSynchronously() |
| 50 |
{ |
| 51 |
$client = new Client(['handler' => new MockHandler([new Response()])]); |
| 52 |
$request = new Request('GET', 'http://example.com'); |
| 53 |
$r = $client->send($request); |
| 54 |
self::assertInstanceOf(ResponseInterface::class, $r); |
| 55 |
self::assertSame(200, $r->getStatusCode()); |
| 56 |
} |
| 57 |
|
| 58 |
public function testClientHasOptions() |
| 59 |
{ |
| 60 |
$client = new Client([ |
| 61 |
'base_uri' => 'http://foo.com', |
| 62 |
'timeout' => 2, |
| 63 |
'headers' => ['bar' => 'baz'], |
| 64 |
'handler' => new MockHandler() |
| 65 |
]); |
| 66 |
$base = $client->getConfig('base_uri'); |
| 67 |
self::assertSame('http://foo.com', (string) $base); |
| 68 |
self::assertInstanceOf(Uri::class, $base); |
| 69 |
self::assertNotNull($client->getConfig('handler')); |
| 70 |
self::assertSame(2, $client->getConfig('timeout')); |
| 71 |
self::assertArrayHasKey('timeout', $client->getConfig()); |
| 72 |
self::assertArrayHasKey('headers', $client->getConfig()); |
| 73 |
} |
| 74 |
|
| 75 |
public function testCanMergeOnBaseUri() |
| 76 |
{ |
| 77 |
$mock = new MockHandler([new Response()]); |
| 78 |
$client = new Client([ |
| 79 |
'base_uri' => 'http://foo.com/bar/', |
| 80 |
'handler' => $mock |
| 81 |
]); |
| 82 |
$client->get('baz'); |
| 83 |
self::assertSame( |
| 84 |
'http://foo.com/bar/baz', |
| 85 |
(string)$mock->getLastRequest()->getUri() |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
public function testCanMergeOnBaseUriWithRequest() |
| 90 |
{ |
| 91 |
$mock = new MockHandler([new Response(), new Response()]); |
| 92 |
$client = new Client([ |
| 93 |
'handler' => $mock, |
| 94 |
'base_uri' => 'http://foo.com/bar/' |
| 95 |
]); |
| 96 |
$client->request('GET', new Uri('baz')); |
| 97 |
self::assertSame( |
| 98 |
'http://foo.com/bar/baz', |
| 99 |
(string) $mock->getLastRequest()->getUri() |
| 100 |
); |
| 101 |
|
| 102 |
$client->request('GET', new Uri('baz'), ['base_uri' => 'http://example.com/foo/']); |
| 103 |
self::assertSame( |
| 104 |
'http://example.com/foo/baz', |
| 105 |
(string) $mock->getLastRequest()->getUri(), |
| 106 |
'Can overwrite the base_uri through the request options' |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
public function testCanUseRelativeUriWithSend() |
| 111 |
{ |
| 112 |
$mock = new MockHandler([new Response()]); |
| 113 |
$client = new Client([ |
| 114 |
'handler' => $mock, |
| 115 |
'base_uri' => 'http://bar.com' |
| 116 |
]); |
| 117 |
self::assertSame('http://bar.com', (string) $client->getConfig('base_uri')); |
| 118 |
$request = new Request('GET', '/baz'); |
| 119 |
$client->send($request); |
| 120 |
self::assertSame( |
| 121 |
'http://bar.com/baz', |
| 122 |
(string) $mock->getLastRequest()->getUri() |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
public function testMergesDefaultOptionsAndDoesNotOverwriteUa() |
| 127 |
{ |
| 128 |
$c = new Client(['headers' => ['User-agent' => 'foo']]); |
| 129 |
self::assertSame(['User-agent' => 'foo'], $c->getConfig('headers')); |
| 130 |
self::assertInternalType('array', $c->getConfig('allow_redirects')); |
| 131 |
self::assertTrue($c->getConfig('http_errors')); |
| 132 |
self::assertTrue($c->getConfig('decode_content')); |
| 133 |
self::assertTrue($c->getConfig('verify')); |
| 134 |
} |
| 135 |
|
| 136 |
public function testDoesNotOverwriteHeaderWithDefault() |
| 137 |
{ |
| 138 |
$mock = new MockHandler([new Response()]); |
| 139 |
$c = new Client([ |
| 140 |
'headers' => ['User-agent' => 'foo'], |
| 141 |
'handler' => $mock |
| 142 |
]); |
| 143 |
$c->get('http://example.com', ['headers' => ['User-Agent' => 'bar']]); |
| 144 |
self::assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent')); |
| 145 |
} |
| 146 |
|
| 147 |
public function testDoesNotOverwriteHeaderWithDefaultInRequest() |
| 148 |
{ |
| 149 |
$mock = new MockHandler([new Response()]); |
| 150 |
$c = new Client([ |
| 151 |
'headers' => ['User-agent' => 'foo'], |
| 152 |
'handler' => $mock |
| 153 |
]); |
| 154 |
$request = new Request('GET', Server::$url, ['User-Agent' => 'bar']); |
| 155 |
$c->send($request); |
| 156 |
self::assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent')); |
| 157 |
} |
| 158 |
|
| 159 |
public function testDoesOverwriteHeaderWithSetRequestOption() |
| 160 |
{ |
| 161 |
$mock = new MockHandler([new Response()]); |
| 162 |
$c = new Client([ |
| 163 |
'headers' => ['User-agent' => 'foo'], |
| 164 |
'handler' => $mock |
| 165 |
]); |
| 166 |
$request = new Request('GET', Server::$url, ['User-Agent' => 'bar']); |
| 167 |
$c->send($request, ['headers' => ['User-Agent' => 'YO']]); |
| 168 |
self::assertSame('YO', $mock->getLastRequest()->getHeaderLine('User-Agent')); |
| 169 |
} |
| 170 |
|
| 171 |
public function testCanUnsetRequestOptionWithNull() |
| 172 |
{ |
| 173 |
$mock = new MockHandler([new Response()]); |
| 174 |
$c = new Client([ |
| 175 |
'headers' => ['foo' => 'bar'], |
| 176 |
'handler' => $mock |
| 177 |
]); |
| 178 |
$c->get('http://example.com', ['headers' => null]); |
| 179 |
self::assertFalse($mock->getLastRequest()->hasHeader('foo')); |
| 180 |
} |
| 181 |
|
| 182 |
public function testRewriteExceptionsToHttpErrors() |
| 183 |
{ |
| 184 |
$client = new Client(['handler' => new MockHandler([new Response(404)])]); |
| 185 |
$res = $client->get('http://foo.com', ['exceptions' => false]); |
| 186 |
self::assertSame(404, $res->getStatusCode()); |
| 187 |
} |
| 188 |
|
| 189 |
public function testRewriteSaveToToSink() |
| 190 |
{ |
| 191 |
$r = Psr7\stream_for(fopen('php://temp', 'r+')); |
| 192 |
$mock = new MockHandler([new Response(200, [], 'foo')]); |
| 193 |
$client = new Client(['handler' => $mock]); |
| 194 |
$client->get('http://foo.com', ['save_to' => $r]); |
| 195 |
self::assertSame($r, $mock->getLastOptions()['sink']); |
| 196 |
} |
| 197 |
|
| 198 |
public function testAllowRedirectsCanBeTrue() |
| 199 |
{ |
| 200 |
$mock = new MockHandler([new Response(200, [], 'foo')]); |
| 201 |
$handler = HandlerStack::create($mock); |
| 202 |
$client = new Client(['handler' => $handler]); |
| 203 |
$client->get('http://foo.com', ['allow_redirects' => true]); |
| 204 |
self::assertInternalType('array', $mock->getLastOptions()['allow_redirects']); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @expectedException \InvalidArgumentException |
| 209 |
* @expectedExceptionMessage allow_redirects must be true, false, or array |
| 210 |
*/ |
| 211 |
public function testValidatesAllowRedirects() |
| 212 |
{ |
| 213 |
$mock = new MockHandler([new Response(200, [], 'foo')]); |
| 214 |
$handler = HandlerStack::create($mock); |
| 215 |
$client = new Client(['handler' => $handler]); |
| 216 |
$client->get('http://foo.com', ['allow_redirects' => 'foo']); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* @expectedException \GuzzleHttp\Exception\ClientException |
| 221 |
*/ |
| 222 |
public function testThrowsHttpErrorsByDefault() |
| 223 |
{ |
| 224 |
$mock = new MockHandler([new Response(404)]); |
| 225 |
$handler = HandlerStack::create($mock); |
| 226 |
$client = new Client(['handler' => $handler]); |
| 227 |
$client->get('http://foo.com'); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @expectedException \InvalidArgumentException |
| 232 |
* @expectedExceptionMessage cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface |
| 233 |
*/ |
| 234 |
public function testValidatesCookies() |
| 235 |
{ |
| 236 |
$mock = new MockHandler([new Response(200, [], 'foo')]); |
| 237 |
$handler = HandlerStack::create($mock); |
| 238 |
$client = new Client(['handler' => $handler]); |
| 239 |
$client->get('http://foo.com', ['cookies' => 'foo']); |
| 240 |
} |
| 241 |
|
| 242 |
public function testSetCookieToTrueUsesSharedJar() |
| 243 |
{ |
| 244 |
$mock = new MockHandler([ |
| 245 |
new Response(200, ['Set-Cookie' => 'foo=bar']), |
| 246 |
new Response() |
| 247 |
]); |
| 248 |
$handler = HandlerStack::create($mock); |
| 249 |
$client = new Client(['handler' => $handler, 'cookies' => true]); |
| 250 |
$client->get('http://foo.com'); |
| 251 |
$client->get('http://foo.com'); |
| 252 |
self::assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie')); |
| 253 |
} |
| 254 |
|
| 255 |
public function testSetCookieToJar() |
| 256 |
{ |
| 257 |
$mock = new MockHandler([ |
| 258 |
new Response(200, ['Set-Cookie' => 'foo=bar']), |
| 259 |
new Response() |
| 260 |
]); |
| 261 |
$handler = HandlerStack::create($mock); |
| 262 |
$client = new Client(['handler' => $handler]); |
| 263 |
$jar = new CookieJar(); |
| 264 |
$client->get('http://foo.com', ['cookies' => $jar]); |
| 265 |
$client->get('http://foo.com', ['cookies' => $jar]); |
| 266 |
self::assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie')); |
| 267 |
} |
| 268 |
|
| 269 |
public function testCanDisableContentDecoding() |
| 270 |
{ |
| 271 |
$mock = new MockHandler([new Response()]); |
| 272 |
$client = new Client(['handler' => $mock]); |
| 273 |
$client->get('http://foo.com', ['decode_content' => false]); |
| 274 |
$last = $mock->getLastRequest(); |
| 275 |
self::assertFalse($last->hasHeader('Accept-Encoding')); |
| 276 |
self::assertFalse($mock->getLastOptions()['decode_content']); |
| 277 |
} |
| 278 |
|
| 279 |
public function testCanSetContentDecodingToValue() |
| 280 |
{ |
| 281 |
$mock = new MockHandler([new Response()]); |
| 282 |
$client = new Client(['handler' => $mock]); |
| 283 |
$client->get('http://foo.com', ['decode_content' => 'gzip']); |
| 284 |
$last = $mock->getLastRequest(); |
| 285 |
self::assertSame('gzip', $last->getHeaderLine('Accept-Encoding')); |
| 286 |
self::assertSame('gzip', $mock->getLastOptions()['decode_content']); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* @expectedException \InvalidArgumentException |
| 291 |
*/ |
| 292 |
public function testValidatesHeaders() |
| 293 |
{ |
| 294 |
$mock = new MockHandler(); |
| 295 |
$client = new Client(['handler' => $mock]); |
| 296 |
$client->get('http://foo.com', ['headers' => 'foo']); |
| 297 |
} |
| 298 |
|
| 299 |
public function testAddsBody() |
| 300 |
{ |
| 301 |
$mock = new MockHandler([new Response()]); |
| 302 |
$client = new Client(['handler' => $mock]); |
| 303 |
$request = new Request('PUT', 'http://foo.com'); |
| 304 |
$client->send($request, ['body' => 'foo']); |
| 305 |
$last = $mock->getLastRequest(); |
| 306 |
self::assertSame('foo', (string) $last->getBody()); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* @expectedException \InvalidArgumentException |
| 311 |
*/ |
| 312 |
public function testValidatesQuery() |
| 313 |
{ |
| 314 |
$mock = new MockHandler(); |
| 315 |
$client = new Client(['handler' => $mock]); |
| 316 |
$request = new Request('PUT', 'http://foo.com'); |
| 317 |
$client->send($request, ['query' => false]); |
| 318 |
} |
| 319 |
|
| 320 |
public function testQueryCanBeString() |
| 321 |
{ |
| 322 |
$mock = new MockHandler([new Response()]); |
| 323 |
$client = new Client(['handler' => $mock]); |
| 324 |
$request = new Request('PUT', 'http://foo.com'); |
| 325 |
$client->send($request, ['query' => 'foo']); |
| 326 |
self::assertSame('foo', $mock->getLastRequest()->getUri()->getQuery()); |
| 327 |
} |
| 328 |
|
| 329 |
public function testQueryCanBeArray() |
| 330 |
{ |
| 331 |
$mock = new MockHandler([new Response()]); |
| 332 |
$client = new Client(['handler' => $mock]); |
| 333 |
$request = new Request('PUT', 'http://foo.com'); |
| 334 |
$client->send($request, ['query' => ['foo' => 'bar baz']]); |
| 335 |
self::assertSame('foo=bar%20baz', $mock->getLastRequest()->getUri()->getQuery()); |
| 336 |
} |
| 337 |
|
| 338 |
public function testCanAddJsonData() |
| 339 |
{ |
| 340 |
$mock = new MockHandler([new Response()]); |
| 341 |
$client = new Client(['handler' => $mock]); |
| 342 |
$request = new Request('PUT', 'http://foo.com'); |
| 343 |
$client->send($request, ['json' => ['foo' => 'bar']]); |
| 344 |
$last = $mock->getLastRequest(); |
| 345 |
self::assertSame('{"foo":"bar"}', (string) $mock->getLastRequest()->getBody()); |
| 346 |
self::assertSame('application/json', $last->getHeaderLine('Content-Type')); |
| 347 |
} |
| 348 |
|
| 349 |
public function testCanAddJsonDataWithoutOverwritingContentType() |
| 350 |
{ |
| 351 |
$mock = new MockHandler([new Response()]); |
| 352 |
$client = new Client(['handler' => $mock]); |
| 353 |
$request = new Request('PUT', 'http://foo.com'); |
| 354 |
$client->send($request, [ |
| 355 |
'headers' => ['content-type' => 'foo'], |
| 356 |
'json' => 'a' |
| 357 |
]); |
| 358 |
$last = $mock->getLastRequest(); |
| 359 |
self::assertSame('"a"', (string) $mock->getLastRequest()->getBody()); |
| 360 |
self::assertSame('foo', $last->getHeaderLine('Content-Type')); |
| 361 |
} |
| 362 |
|
| 363 |
public function testCanAddJsonDataWithNullHeader() |
| 364 |
{ |
| 365 |
$mock = new MockHandler([new Response()]); |
| 366 |
$client = new Client(['handler' => $mock]); |
| 367 |
$request = new Request('PUT', 'http://foo.com'); |
| 368 |
$client->send($request, [ |
| 369 |
'headers' => null, |
| 370 |
'json' => 'a' |
| 371 |
]); |
| 372 |
$last = $mock->getLastRequest(); |
| 373 |
self::assertSame('"a"', (string) $mock->getLastRequest()->getBody()); |
| 374 |
self::assertSame('application/json', $last->getHeaderLine('Content-Type')); |
| 375 |
} |
| 376 |
|
| 377 |
public function testAuthCanBeTrue() |
| 378 |
{ |
| 379 |
$mock = new MockHandler([new Response()]); |
| 380 |
$client = new Client(['handler' => $mock]); |
| 381 |
$client->get('http://foo.com', ['auth' => false]); |
| 382 |
$last = $mock->getLastRequest(); |
| 383 |
self::assertFalse($last->hasHeader('Authorization')); |
| 384 |
} |
| 385 |
|
| 386 |
public function testAuthCanBeArrayForBasicAuth() |
| 387 |
{ |
| 388 |
$mock = new MockHandler([new Response()]); |
| 389 |
$client = new Client(['handler' => $mock]); |
| 390 |
$client->get('http://foo.com', ['auth' => ['a', 'b']]); |
| 391 |
$last = $mock->getLastRequest(); |
| 392 |
self::assertSame('Basic YTpi', $last->getHeaderLine('Authorization')); |
| 393 |
} |
| 394 |
|
| 395 |
public function testAuthCanBeArrayForDigestAuth() |
| 396 |
{ |
| 397 |
$mock = new MockHandler([new Response()]); |
| 398 |
$client = new Client(['handler' => $mock]); |
| 399 |
$client->get('http://foo.com', ['auth' => ['a', 'b', 'digest']]); |
| 400 |
$last = $mock->getLastOptions(); |
| 401 |
self::assertSame([ |
| 402 |
CURLOPT_HTTPAUTH => 2, |
| 403 |
CURLOPT_USERPWD => 'a:b' |
| 404 |
], $last['curl']); |
| 405 |
} |
| 406 |
|
| 407 |
public function testAuthCanBeArrayForNtlmAuth() |
| 408 |
{ |
| 409 |
$mock = new MockHandler([new Response()]); |
| 410 |
$client = new Client(['handler' => $mock]); |
| 411 |
$client->get('http://foo.com', ['auth' => ['a', 'b', 'ntlm']]); |
| 412 |
$last = $mock->getLastOptions(); |
| 413 |
self::assertSame([ |
| 414 |
CURLOPT_HTTPAUTH => 8, |
| 415 |
CURLOPT_USERPWD => 'a:b' |
| 416 |
], $last['curl']); |
| 417 |
} |
| 418 |
|
| 419 |
public function testAuthCanBeCustomType() |
| 420 |
{ |
| 421 |
$mock = new MockHandler([new Response()]); |
| 422 |
$client = new Client(['handler' => $mock]); |
| 423 |
$client->get('http://foo.com', ['auth' => 'foo']); |
| 424 |
$last = $mock->getLastOptions(); |
| 425 |
self::assertSame('foo', $last['auth']); |
| 426 |
} |
| 427 |
|
| 428 |
public function testCanAddFormParams() |
| 429 |
{ |
| 430 |
$mock = new MockHandler([new Response()]); |
| 431 |
$client = new Client(['handler' => $mock]); |
| 432 |
$client->post('http://foo.com', [ |
| 433 |
'form_params' => [ |
| 434 |
'foo' => 'bar bam', |
| 435 |
'baz' => ['boo' => 'qux'] |
| 436 |
] |
| 437 |
]); |
| 438 |
$last = $mock->getLastRequest(); |
| 439 |
self::assertSame( |
| 440 |
'application/x-www-form-urlencoded', |
| 441 |
$last->getHeaderLine('Content-Type') |
| 442 |
); |
| 443 |
self::assertSame( |
| 444 |
'foo=bar+bam&baz%5Bboo%5D=qux', |
| 445 |
(string) $last->getBody() |
| 446 |
); |
| 447 |
} |
| 448 |
|
| 449 |
public function testFormParamsEncodedProperly() |
| 450 |
{ |
| 451 |
$separator = ini_get('arg_separator.output'); |
| 452 |
ini_set('arg_separator.output', '&'); |
| 453 |
$mock = new MockHandler([new Response()]); |
| 454 |
$client = new Client(['handler' => $mock]); |
| 455 |
$client->post('http://foo.com', [ |
| 456 |
'form_params' => [ |
| 457 |
'foo' => 'bar bam', |
| 458 |
'baz' => ['boo' => 'qux'] |
| 459 |
] |
| 460 |
]); |
| 461 |
$last = $mock->getLastRequest(); |
| 462 |
self::assertSame( |
| 463 |
'foo=bar+bam&baz%5Bboo%5D=qux', |
| 464 |
(string) $last->getBody() |
| 465 |
); |
| 466 |
|
| 467 |
ini_set('arg_separator.output', $separator); |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* @expectedException \InvalidArgumentException |
| 472 |
*/ |
| 473 |
public function testEnsuresThatFormParamsAndMultipartAreExclusive() |
| 474 |
{ |
| 475 |
$client = new Client(['handler' => function () { |
| 476 |
}]); |
| 477 |
$client->post('http://foo.com', [ |
| 478 |
'form_params' => ['foo' => 'bar bam'], |
| 479 |
'multipart' => [] |
| 480 |
]); |
| 481 |
} |
| 482 |
|
| 483 |
public function testCanSendMultipart() |
| 484 |
{ |
| 485 |
$mock = new MockHandler([new Response()]); |
| 486 |
$client = new Client(['handler' => $mock]); |
| 487 |
$client->post('http://foo.com', [ |
| 488 |
'multipart' => [ |
| 489 |
[ |
| 490 |
'name' => 'foo', |
| 491 |
'contents' => 'bar' |
| 492 |
], |
| 493 |
[ |
| 494 |
'name' => 'test', |
| 495 |
'contents' => fopen(__FILE__, 'r') |
| 496 |
] |
| 497 |
] |
| 498 |
]); |
| 499 |
|
| 500 |
$last = $mock->getLastRequest(); |
| 501 |
self::assertContains( |
| 502 |
'multipart/form-data; boundary=', |
| 503 |
$last->getHeaderLine('Content-Type') |
| 504 |
); |
| 505 |
|
| 506 |
self::assertContains( |
| 507 |
'Content-Disposition: form-data; name="foo"', |
| 508 |
(string) $last->getBody() |
| 509 |
); |
| 510 |
|
| 511 |
self::assertContains('bar', (string) $last->getBody()); |
| 512 |
self::assertContains( |
| 513 |
'Content-Disposition: form-data; name="foo"' . "\r\n", |
| 514 |
(string) $last->getBody() |
| 515 |
); |
| 516 |
self::assertContains( |
| 517 |
'Content-Disposition: form-data; name="test"; filename="ClientTest.php"', |
| 518 |
(string) $last->getBody() |
| 519 |
); |
| 520 |
} |
| 521 |
|
| 522 |
public function testCanSendMultipartWithExplicitBody() |
| 523 |
{ |
| 524 |
$mock = new MockHandler([new Response()]); |
| 525 |
$client = new Client(['handler' => $mock]); |
| 526 |
$client->send( |
| 527 |
new Request( |
| 528 |
'POST', |
| 529 |
'http://foo.com', |
| 530 |
[], |
| 531 |
new Psr7\MultipartStream( |
| 532 |
[ |
| 533 |
[ |
| 534 |
'name' => 'foo', |
| 535 |
'contents' => 'bar', |
| 536 |
], |
| 537 |
[ |
| 538 |
'name' => 'test', |
| 539 |
'contents' => fopen(__FILE__, 'r'), |
| 540 |
], |
| 541 |
] |
| 542 |
) |
| 543 |
) |
| 544 |
); |
| 545 |
|
| 546 |
$last = $mock->getLastRequest(); |
| 547 |
self::assertContains( |
| 548 |
'multipart/form-data; boundary=', |
| 549 |
$last->getHeaderLine('Content-Type') |
| 550 |
); |
| 551 |
|
| 552 |
self::assertContains( |
| 553 |
'Content-Disposition: form-data; name="foo"', |
| 554 |
(string) $last->getBody() |
| 555 |
); |
| 556 |
|
| 557 |
self::assertContains('bar', (string) $last->getBody()); |
| 558 |
self::assertContains( |
| 559 |
'Content-Disposition: form-data; name="foo"' . "\r\n", |
| 560 |
(string) $last->getBody() |
| 561 |
); |
| 562 |
self::assertContains( |
| 563 |
'Content-Disposition: form-data; name="test"; filename="ClientTest.php"', |
| 564 |
(string) $last->getBody() |
| 565 |
); |
| 566 |
} |
| 567 |
|
| 568 |
public function testUsesProxyEnvironmentVariables() |
| 569 |
{ |
| 570 |
$http = getenv('HTTP_PROXY'); |
| 571 |
$https = getenv('HTTPS_PROXY'); |
| 572 |
$no = getenv('NO_PROXY'); |
| 573 |
$client = new Client(); |
| 574 |
self::assertNull($client->getConfig('proxy')); |
| 575 |
putenv('HTTP_PROXY=127.0.0.1'); |
| 576 |
$client = new Client(); |
| 577 |
self::assertSame( |
| 578 |
['http' => '127.0.0.1'], |
| 579 |
$client->getConfig('proxy') |
| 580 |
); |
| 581 |
putenv('HTTPS_PROXY=127.0.0.2'); |
| 582 |
putenv('NO_PROXY=127.0.0.3, 127.0.0.4'); |
| 583 |
$client = new Client(); |
| 584 |
self::assertSame( |
| 585 |
['http' => '127.0.0.1', 'https' => '127.0.0.2', 'no' => ['127.0.0.3','127.0.0.4']], |
| 586 |
$client->getConfig('proxy') |
| 587 |
); |
| 588 |
putenv("HTTP_PROXY=$http"); |
| 589 |
putenv("HTTPS_PROXY=$https"); |
| 590 |
putenv("NO_PROXY=$no"); |
| 591 |
} |
| 592 |
|
| 593 |
public function testRequestSendsWithSync() |
| 594 |
{ |
| 595 |
$mock = new MockHandler([new Response()]); |
| 596 |
$client = new Client(['handler' => $mock]); |
| 597 |
$client->request('GET', 'http://foo.com'); |
| 598 |
self::assertTrue($mock->getLastOptions()['synchronous']); |
| 599 |
} |
| 600 |
|
| 601 |
public function testSendSendsWithSync() |
| 602 |
{ |
| 603 |
$mock = new MockHandler([new Response()]); |
| 604 |
$client = new Client(['handler' => $mock]); |
| 605 |
$client->send(new Request('GET', 'http://foo.com')); |
| 606 |
self::assertTrue($mock->getLastOptions()['synchronous']); |
| 607 |
} |
| 608 |
|
| 609 |
public function testCanSetCustomHandler() |
| 610 |
{ |
| 611 |
$mock = new MockHandler([new Response(500)]); |
| 612 |
$client = new Client(['handler' => $mock]); |
| 613 |
$mock2 = new MockHandler([new Response(200)]); |
| 614 |
self::assertSame( |
| 615 |
200, |
| 616 |
$client->send(new Request('GET', 'http://foo.com'), [ |
| 617 |
'handler' => $mock2 |
| 618 |
])->getStatusCode() |
| 619 |
); |
| 620 |
} |
| 621 |
|
| 622 |
public function testProperlyBuildsQuery() |
| 623 |
{ |
| 624 |
$mock = new MockHandler([new Response()]); |
| 625 |
$client = new Client(['handler' => $mock]); |
| 626 |
$request = new Request('PUT', 'http://foo.com'); |
| 627 |
$client->send($request, ['query' => ['foo' => 'bar', 'john' => 'doe']]); |
| 628 |
self::assertSame('foo=bar&john=doe', $mock->getLastRequest()->getUri()->getQuery()); |
| 629 |
} |
| 630 |
|
| 631 |
public function testSendSendsWithIpAddressAndPortAndHostHeaderInRequestTheHostShouldBePreserved() |
| 632 |
{ |
| 633 |
$mockHandler = new MockHandler([new Response()]); |
| 634 |
$client = new Client(['base_uri' => 'http://127.0.0.1:8585', 'handler' => $mockHandler]); |
| 635 |
$request = new Request('GET', '/test', ['Host'=>'foo.com']); |
| 636 |
|
| 637 |
$client->send($request); |
| 638 |
|
| 639 |
self::assertSame('foo.com', $mockHandler->getLastRequest()->getHeader('Host')[0]); |
| 640 |
} |
| 641 |
|
| 642 |
public function testSendSendsWithDomainAndHostHeaderInRequestTheHostShouldBePreserved() |
| 643 |
{ |
| 644 |
$mockHandler = new MockHandler([new Response()]); |
| 645 |
$client = new Client(['base_uri' => 'http://foo2.com', 'handler' => $mockHandler]); |
| 646 |
$request = new Request('GET', '/test', ['Host'=>'foo.com']); |
| 647 |
|
| 648 |
$client->send($request); |
| 649 |
|
| 650 |
self::assertSame('foo.com', $mockHandler->getLastRequest()->getHeader('Host')[0]); |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* @expectedException \InvalidArgumentException |
| 655 |
*/ |
| 656 |
public function testValidatesSink() |
| 657 |
{ |
| 658 |
$mockHandler = new MockHandler([new Response()]); |
| 659 |
$client = new Client(['handler' => $mockHandler]); |
| 660 |
$client->get('http://test.com', ['sink' => true]); |
| 661 |
} |
| 662 |
|
| 663 |
public function testHttpDefaultSchemeIfUriHasNone() |
| 664 |
{ |
| 665 |
$mockHandler = new MockHandler([new Response()]); |
| 666 |
$client = new Client(['handler' => $mockHandler]); |
| 667 |
|
| 668 |
$client->request('GET', '//example.org/test'); |
| 669 |
|
| 670 |
self::assertSame('http://example.org/test', (string) $mockHandler->getLastRequest()->getUri()); |
| 671 |
} |
| 672 |
|
| 673 |
public function testOnlyAddSchemeWhenHostIsPresent() |
| 674 |
{ |
| 675 |
$mockHandler = new MockHandler([new Response()]); |
| 676 |
$client = new Client(['handler' => $mockHandler]); |
| 677 |
|
| 678 |
$client->request('GET', 'baz'); |
| 679 |
self::assertSame( |
| 680 |
'baz', |
| 681 |
(string) $mockHandler->getLastRequest()->getUri() |
| 682 |
); |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* @expectedException InvalidArgumentException |
| 687 |
*/ |
| 688 |
public function testHandlerIsCallable() |
| 689 |
{ |
| 690 |
new Client(['handler' => 'not_cllable']); |
| 691 |
} |
| 692 |
|
| 693 |
public function testResponseBodyAsString() |
| 694 |
{ |
| 695 |
$responseBody = '{ "package": "guzzle" }'; |
| 696 |
$mock = new MockHandler([new Response(200, ['Content-Type' => 'application/json'], $responseBody)]); |
| 697 |
$client = new Client(['handler' => $mock]); |
| 698 |
$request = new Request('GET', 'http://foo.com'); |
| 699 |
$response = $client->send($request, ['json' => ['a' => 'b']]); |
| 700 |
|
| 701 |
self::assertSame($responseBody, (string) $response->getBody()); |
| 702 |
} |
| 703 |
|
| 704 |
public function testResponseContent() |
| 705 |
{ |
| 706 |
$responseBody = '{ "package": "guzzle" }'; |
| 707 |
$mock = new MockHandler([new Response(200, ['Content-Type' => 'application/json'], $responseBody)]); |
| 708 |
$client = new Client(['handler' => $mock]); |
| 709 |
$request = new Request('POST', 'http://foo.com'); |
| 710 |
$response = $client->send($request, ['json' => ['a' => 'b']]); |
| 711 |
|
| 712 |
self::assertSame($responseBody, $response->getBody()->getContents()); |
| 713 |
} |
| 714 |
} |
| 715 |
|