3
namespace GuzzleHttp\Tests;
5
use GuzzleHttp\Adapter\FakeParallelAdapter;
6
use GuzzleHttp\Adapter\MockAdapter;
8
use GuzzleHttp\Event\BeforeEvent;
9
use GuzzleHttp\Exception\RequestException;
10
use GuzzleHttp\Message\MessageFactory;
11
use GuzzleHttp\Message\Response;
12
use GuzzleHttp\Subscriber\History;
13
use GuzzleHttp\Subscriber\Mock;
16
* @covers GuzzleHttp\Client
18
class ClientTest extends \PHPUnit_Framework_TestCase
20
public function testProvidesDefaultUserAgent()
22
$this->assertEquals(1, preg_match('#^Guzzle/.+ curl/.+ PHP/.+$#', Client::getDefaultUserAgent()));
25
public function testUsesDefaultDefaultOptions()
27
$client = new Client();
28
$this->assertTrue($client->getDefaultOption('allow_redirects'));
29
$this->assertTrue($client->getDefaultOption('exceptions'));
30
$this->assertContains('cacert.pem', $client->getDefaultOption('verify'));
33
public function testUsesProvidedDefaultOptions()
35
$client = new Client([
37
'allow_redirects' => false,
38
'query' => ['foo' => 'bar']
41
$this->assertFalse($client->getDefaultOption('allow_redirects'));
42
$this->assertTrue($client->getDefaultOption('exceptions'));
43
$this->assertContains('cacert.pem', $client->getDefaultOption('verify'));
44
$this->assertEquals(['foo' => 'bar'], $client->getDefaultOption('query'));
47
public function testCanSpecifyBaseUrl()
49
$this->assertSame('', (new Client())->getBaseUrl());
50
$this->assertEquals('http://foo', (new Client([
51
'base_url' => 'http://foo'
55
public function testCanSpecifyBaseUrlUriTemplate()
57
$client = new Client(['base_url' => ['http://foo.com/{var}/', ['var' => 'baz']]]);
58
$this->assertEquals('http://foo.com/baz/', $client->getBaseUrl());
61
public function testClientUsesDefaultAdapterWhenNoneIsSet()
63
$client = new Client();
64
if (!extension_loaded('curl')) {
65
$adapter = 'GuzzleHttp\Adapter\StreamAdapter';
66
} elseif (ini_get('allow_url_fopen')) {
67
$adapter = 'GuzzleHttp\Adapter\StreamingProxyAdapter';
69
$adapter = 'GuzzleHttp\Adapter\Curl\CurlAdapter';
71
$this->assertInstanceOf($adapter, $this->readAttribute($client, 'adapter'));
75
* @expectedException \Exception
76
* @expectedExceptionMessage Foo
78
public function testCanSpecifyAdapter()
80
$adapter = $this->getMockBuilder('GuzzleHttp\Adapter\AdapterInterface')
81
->setMethods(['send'])
82
->getMockForAbstractClass();
83
$adapter->expects($this->once())
85
->will($this->throwException(new \Exception('Foo')));
86
$client = new Client(['adapter' => $adapter]);
87
$client->get('http://httpbin.org');
91
* @expectedException \Exception
92
* @expectedExceptionMessage Foo
94
public function testCanSpecifyMessageFactory()
96
$factory = $this->getMockBuilder('GuzzleHttp\Message\MessageFactoryInterface')
97
->setMethods(['createRequest'])
98
->getMockForAbstractClass();
99
$factory->expects($this->once())
100
->method('createRequest')
101
->will($this->throwException(new \Exception('Foo')));
102
$client = new Client(['message_factory' => $factory]);
106
public function testCanSpecifyEmitter()
108
$emitter = $this->getMockBuilder('GuzzleHttp\Event\EmitterInterface')
109
->setMethods(['listeners'])
110
->getMockForAbstractClass();
111
$emitter->expects($this->once())
112
->method('listeners')
113
->will($this->returnValue('foo'));
115
$client = new Client(['emitter' => $emitter]);
116
$this->assertEquals('foo', $client->getEmitter()->listeners());
119
public function testAddsDefaultUserAgentHeaderWithDefaultOptions()
121
$client = new Client(['defaults' => ['allow_redirects' => false]]);
122
$this->assertFalse($client->getDefaultOption('allow_redirects'));
124
['User-Agent' => Client::getDefaultUserAgent()],
125
$client->getDefaultOption('headers')
129
public function testAddsDefaultUserAgentHeaderWithoutDefaultOptions()
131
$client = new Client();
133
['User-Agent' => Client::getDefaultUserAgent()],
134
$client->getDefaultOption('headers')
138
private function getRequestClient()
140
$client = $this->getMockBuilder('GuzzleHttp\Client')
141
->setMethods(['send'])
143
$client->expects($this->once())
145
->will($this->returnArgument(0));
150
public function requestMethodProvider()
164
* @dataProvider requestMethodProvider
166
public function testClientProvidesMethodShortcut($method, $body)
168
$client = $this->getRequestClient();
170
$request = $client->{$method}('http://foo.com', [
171
'headers' => ['X-Baz' => 'Bar'],
173
'query' => ['a' => 'b']
176
$request = $client->{$method}('http://foo.com', [
177
'headers' => ['X-Baz' => 'Bar'],
178
'query' => ['a' => 'b']
181
$this->assertEquals($method, $request->getMethod());
182
$this->assertEquals('Bar', $request->getHeader('X-Baz'));
183
$this->assertEquals('a=b', $request->getQuery());
185
$this->assertEquals($body, $request->getBody());
189
public function testClientMergesDefaultOptionsWithRequestOptions()
191
$f = $this->getMockBuilder('GuzzleHttp\Message\MessageFactoryInterface')
192
->setMethods(array('createRequest'))
193
->getMockForAbstractClass();
196
// Intercept the creation
197
$f->expects($this->once())
198
->method('createRequest')
199
->will($this->returnCallback(
200
function ($method, $url, array $options = []) use (&$o) {
202
return (new MessageFactory())->createRequest($method, $url, $options);
206
$client = new Client([
207
'message_factory' => $f,
209
'headers' => ['Foo' => 'Bar'],
210
'query' => ['baz' => 'bam'],
211
'exceptions' => false
215
$request = $client->createRequest('GET', 'http://foo.com?a=b', [
216
'headers' => ['Hi' => 'there', '1' => 'one'],
217
'allow_redirects' => false,
218
'query' => ['t' => 1]
221
$this->assertFalse($o['allow_redirects']);
222
$this->assertFalse($o['exceptions']);
223
$this->assertEquals('Bar', $request->getHeader('Foo'));
224
$this->assertEquals('there', $request->getHeader('Hi'));
225
$this->assertEquals('one', $request->getHeader('1'));
226
$this->assertEquals('a=b&baz=bam&t=1', $request->getQuery());
229
public function testClientMergesDefaultHeadersCaseInsensitively()
231
$client = new Client(['defaults' => ['headers' => ['Foo' => 'Bar']]]);
232
$request = $client->createRequest('GET', 'http://foo.com?a=b', [
233
'headers' => ['foo' => 'custom', 'user-agent' => 'test']
235
$this->assertEquals('test', $request->getHeader('User-Agent'));
236
$this->assertEquals('custom', $request->getHeader('Foo'));
239
public function testUsesBaseUrlWhenNoUrlIsSet()
241
$client = new Client(['base_url' => 'http://www.foo.com/baz?bam=bar']);
243
'http://www.foo.com/baz?bam=bar',
244
$client->createRequest('GET')->getUrl()
248
public function testUsesBaseUrlCombinedWithProvidedUrl()
250
$client = new Client(['base_url' => 'http://www.foo.com/baz?bam=bar']);
252
'http://www.foo.com/bar/bam',
253
$client->createRequest('GET', 'bar/bam')->getUrl()
257
public function testUsesBaseUrlCombinedWithProvidedUrlViaUriTemplate()
259
$client = new Client(['base_url' => 'http://www.foo.com/baz?bam=bar']);
261
'http://www.foo.com/bar/123',
262
$client->createRequest('GET', ['bar/{bam}', ['bam' => '123']])->getUrl()
266
public function testSettingAbsoluteUrlOverridesBaseUrl()
268
$client = new Client(['base_url' => 'http://www.foo.com/baz?bam=bar']);
270
'http://www.foo.com/foo',
271
$client->createRequest('GET', '/foo')->getUrl()
275
public function testSettingAbsoluteUriTemplateOverridesBaseUrl()
277
$client = new Client(['base_url' => 'http://www.foo.com/baz?bam=bar']);
280
$client->createRequest(
282
['http://goo.com/{bar}', ['bar' => '1']]
287
public function testCanSetRelativeUrlStartingWithHttp()
289
$client = new Client(['base_url' => 'http://www.foo.com']);
291
'http://www.foo.com/httpfoo',
292
$client->createRequest('GET', 'httpfoo')->getUrl()
296
public function testClientSendsRequests()
298
$response = new Response(200);
299
$adapter = new MockAdapter();
300
$adapter->setResponse($response);
301
$client = new Client(['adapter' => $adapter]);
302
$this->assertSame($response, $client->get('http://test.com'));
303
$this->assertEquals('http://test.com', $response->getEffectiveUrl());
306
public function testSendingRequestCanBeIntercepted()
308
$response = new Response(200);
309
$response2 = new Response(200);
310
$adapter = new MockAdapter();
311
$adapter->setResponse($response);
312
$client = new Client(['adapter' => $adapter]);
313
$client->getEmitter()->on(
315
function (BeforeEvent $e) use ($response2) {
316
$e->intercept($response2);
319
$this->assertSame($response2, $client->get('http://test.com'));
320
$this->assertEquals('http://test.com', $response2->getEffectiveUrl());
324
* @expectedException \GuzzleHttp\Exception\RequestException
325
* @expectedExceptionMessage No response
327
public function testEnsuresResponseIsPresentAfterSending()
329
$adapter = $this->getMockBuilder('GuzzleHttp\Adapter\MockAdapter')
330
->setMethods(['send'])
332
$adapter->expects($this->once())
334
$client = new Client(['adapter' => $adapter]);
335
$client->get('http://httpbin.org');
338
public function testClientHandlesErrorsDuringBeforeSend()
340
$client = new Client();
341
$client->getEmitter()->on('before', function ($e) {
342
throw new \Exception('foo');
344
$client->getEmitter()->on('error', function ($e) {
345
$e->intercept(new Response(200));
347
$this->assertEquals(200, $client->get('http://test.com')->getStatusCode());
351
* @expectedException \GuzzleHttp\Exception\RequestException
352
* @expectedExceptionMessage foo
354
public function testClientHandlesErrorsDuringBeforeSendAndThrowsIfUnhandled()
356
$client = new Client();
357
$client->getEmitter()->on('before', function ($e) {
358
throw new RequestException('foo', $e->getRequest());
360
$client->get('http://httpbin.org');
364
* @expectedException \GuzzleHttp\Exception\RequestException
365
* @expectedExceptionMessage foo
367
public function testClientWrapsExceptions()
369
$client = new Client();
370
$client->getEmitter()->on('before', function ($e) {
371
throw new \Exception('foo');
373
$client->get('http://httpbin.org');
376
public function testCanSetDefaultValues()
378
$client = new Client(['foo' => 'bar']);
379
$client->setDefaultOption('headers/foo', 'bar');
380
$this->assertNull($client->getDefaultOption('foo'));
381
$this->assertEquals('bar', $client->getDefaultOption('headers/foo'));
384
public function testSendsAllInParallel()
386
$client = new Client();
387
$client->getEmitter()->attach(new Mock([
392
$history = new History();
393
$client->getEmitter()->attach($history);
396
$client->createRequest('GET', 'http://test.com'),
397
$client->createRequest('POST', 'http://test.com'),
398
$client->createRequest('PUT', 'http://test.com')
401
$client->sendAll($requests);
402
$requests = array_map(function ($r) { return $r->getMethod(); }, $history->getRequests());
403
$this->assertContains('GET', $requests);
404
$this->assertContains('POST', $requests);
405
$this->assertContains('PUT', $requests);
408
public function testCanSetCustomParallelAdapter()
411
$pa = new FakeParallelAdapter(new MockAdapter(function () use (&$called) {
413
return new Response(203);
415
$client = new Client(['parallel_adapter' => $pa]);
416
$client->sendAll([$client->createRequest('GET', 'http://www.foo.com')]);
417
$this->assertTrue($called);
420
public function testCanDisableAuthPerRequest()
422
$client = new Client(['defaults' => ['auth' => 'foo']]);
423
$request = $client->createRequest('GET', 'http://test.com');
424
$this->assertEquals('foo', $request->getConfig()['auth']);
425
$request = $client->createRequest('GET', 'http://test.com', ['auth' => null]);
426
$this->assertFalse($request->getConfig()->hasKey('auth'));
430
* @expectedException \PHPUnit_Framework_Error_Deprecated
432
public function testHasDeprecatedGetEmitter()
434
$client = new Client();
435
$client->getEventDispatcher();
438
public function testUsesProxyEnvironmentVariables()
440
$client = new Client();
441
$this->assertNull($client->getDefaultOption('proxy'));
443
putenv('HTTP_PROXY=127.0.0.1');
444
$client = new Client();
446
['http' => '127.0.0.1'],
447
$client->getDefaultOption('proxy')
450
putenv('HTTPS_PROXY=127.0.0.2');
451
$client = new Client();
453
['http' => '127.0.0.1', 'https' => '127.0.0.2'],
454
$client->getDefaultOption('proxy')
457
putenv('HTTP_PROXY=');
458
putenv('HTTPS_PROXY=');