1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 10:03:27 +01:00
guzzle/tests/ClientTest.php

481 lines
17 KiB
PHP
Raw Normal View History

2015-03-03 14:02:24 -08:00
<?php
namespace GuzzleHttp\Tests;
use GuzzleHttp\Client;
2015-03-22 19:32:07 -07:00
use GuzzleHttp\Cookie\CookieJar;
2015-03-21 23:33:02 -07:00
use GuzzleHttp\Handler\MockHandler;
2015-03-22 19:32:07 -07:00
use GuzzleHttp\HandlerStack;
2015-03-22 17:42:47 -07:00
use GuzzleHttp\Psr7;
2015-03-21 23:33:02 -07:00
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
2015-03-03 14:02:24 -08:00
class ClientTest extends \PHPUnit_Framework_TestCase
{
2015-03-21 15:53:38 -07:00
public function testUsesDefaultHandler()
{
$client = new Client();
Server::enqueue([new Response(200, ['Content-Length' => 0])]);
$response = $client->get(Server::$url);
$this->assertEquals(200, $response->getStatusCode());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Magic request methods require a URI and optional options array
*/
public function testValidatesArgsForMagicMethods()
{
$client = new Client();
$client->get();
}
2015-03-03 14:02:24 -08:00
public function testCanSendMagicAsyncRequests()
{
$client = new Client();
Server::flush();
Server::enqueue([new Response(200, ['Content-Length' => 2], 'hi')]);
$p = $client->getAsync(Server::$url, ['query' => ['test' => 'foo']]);
$this->assertInstanceOf('GuzzleHttp\Promise\PromiseInterface', $p);
$this->assertEquals(200, $p->wait()->getStatusCode());
$received = Server::received(true);
$this->assertCount(1, $received);
$this->assertEquals('test=foo', $received[0]->getUri()->getQuery());
2015-03-21 15:53:38 -07:00
}
2015-03-21 23:33:02 -07:00
public function testCanSendSynchronously()
{
2015-03-22 20:19:33 -07:00
$client = new Client(['handler' => new MockHandler([new Response()])]);
2015-03-21 23:33:02 -07:00
$request = new Request('GET', 'http://example.com');
$r = $client->send($request);
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $r);
$this->assertEquals(200, $r->getStatusCode());
}
public function testClientHasOptions()
{
$client = new Client([
'base_uri' => 'http://foo.com',
'timeout' => 2,
'headers' => ['bar' => 'baz'],
'handler' => new MockHandler()
]);
$base = $client->getDefaultOption('base_uri');
$this->assertEquals('http://foo.com', (string) $base);
$this->assertInstanceOf('GuzzleHttp\Psr7\Uri', $base);
2015-03-21 23:33:02 -07:00
$this->assertNull($client->getDefaultOption('handler'));
$this->assertEquals(2, $client->getDefaultOption('timeout'));
$this->assertArrayHasKey('timeout', $client->getDefaultOption());
$this->assertArrayHasKey('headers', $client->getDefaultOption());
}
public function testCanMergeOnBaseUri()
{
2015-03-22 20:19:33 -07:00
$mock = new MockHandler([new Response()]);
2015-03-21 23:33:02 -07:00
$client = new Client([
'base_uri' => 'http://foo.com/bar/',
'handler' => $mock
]);
$client->get('baz');
$this->assertEquals(
'http://foo.com/bar/baz',
$mock->getLastRequest()->getUri()
);
}
public function testCanUseRelativeUriWithSend()
{
2015-03-22 20:19:33 -07:00
$mock = new MockHandler([new Response()]);
2015-03-21 23:33:02 -07:00
$client = new Client([
'handler' => $mock,
'base_uri' => 'http://bar.com'
]);
$this->assertEquals('http://bar.com', (string) $client->getDefaultOption('base_uri'));
2015-03-21 23:33:02 -07:00
$request = new Request('GET', '/baz');
$client->send($request);
$this->assertEquals(
'http://bar.com/baz',
(string) $mock->getLastRequest()->getUri()
2015-03-21 23:33:02 -07:00
);
}
public function testMergesDefaultOptionsAndDoesNotOverwriteUa()
{
$c = new Client(['headers' => ['User-agent' => 'foo']]);
$this->assertEquals(['User-agent' => 'foo'], $c->getDefaultOption('headers'));
$this->assertInternalType('array', $c->getDefaultOption('allow_redirects'));
$this->assertTrue($c->getDefaultOption('http_errors'));
$this->assertTrue($c->getDefaultOption('decode_content'));
$this->assertTrue($c->getDefaultOption('verify'));
}
public function testDoesNotOverwriteHeaderWithDefault()
{
2015-03-22 20:19:33 -07:00
$mock = new MockHandler([new Response()]);
2015-03-21 23:33:02 -07:00
$c = new Client([
'headers' => ['User-agent' => 'foo'],
'handler' => $mock
]);
$c->get('http://example.com', ['headers' => ['User-Agent' => 'bar']]);
2015-04-19 20:39:53 -07:00
$this->assertEquals('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
2015-03-21 23:33:02 -07:00
}
public function testDoesNotOverwriteHeaderWithDefaultInRequest()
{
$mock = new MockHandler([new Response()]);
$c = new Client([
'headers' => ['User-agent' => 'foo'],
'handler' => $mock
]);
$request = new Request('GET', Server::$url, ['User-Agent' => 'bar']);
$c->send($request);
$this->assertEquals('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
}
public function testDoesOverwriteHeaderWithSetRequestOption()
{
$mock = new MockHandler([new Response()]);
$c = new Client([
'headers' => ['User-agent' => 'foo'],
'handler' => $mock
]);
$request = new Request('GET', Server::$url, ['User-Agent' => 'bar']);
$c->send($request, ['headers' => ['User-Agent' => 'YO']]);
$this->assertEquals('YO', $mock->getLastRequest()->getHeaderLine('User-Agent'));
}
2015-03-21 23:33:02 -07:00
public function testCanUnsetRequestOptionWithNull()
{
2015-03-22 20:19:33 -07:00
$mock = new MockHandler([new Response()]);
2015-03-21 23:33:02 -07:00
$c = new Client([
'headers' => ['foo' => 'bar'],
'handler' => $mock
]);
$c->get('http://example.com', ['headers' => null]);
$this->assertFalse($mock->getLastRequest()->hasHeader('foo'));
}
2015-03-22 17:42:47 -07:00
public function testRewriteExceptionsToHttpErrors()
{
$client = new Client(['handler' => new MockHandler([new Response(404)])]);
$res = $client->get('http://foo.com', ['exceptions' => false]);
$this->assertEquals(404, $res->getStatusCode());
}
public function testRewriteSaveToToSink()
{
$r = Psr7\stream_for(fopen('php://temp', 'r+'));
$mock = new MockHandler([new Response(200, [], 'foo')]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['save_to' => $r]);
$this->assertSame($r, $mock->getLastOptions()['sink']);
}
2015-03-22 19:32:07 -07:00
public function testAllowRedirectsCanBeTrue()
{
$mock = new MockHandler([new Response(200, [], 'foo')]);
$handler = HandlerStack::create($mock);
2015-04-17 19:59:11 -07:00
$client = new Client(['handler' => $handler]);
2015-03-22 19:32:07 -07:00
$client->get('http://foo.com', ['allow_redirects' => true]);
$this->assertInternalType('array', $mock->getLastOptions()['allow_redirects']);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage allow_redirects must be true, false, or array
*/
public function testValidatesAllowRedirects()
{
$mock = new MockHandler([new Response(200, [], 'foo')]);
$handler = HandlerStack::create($mock);
2015-04-17 19:59:11 -07:00
$client = new Client(['handler' => $handler]);
2015-03-22 19:32:07 -07:00
$client->get('http://foo.com', ['allow_redirects' => 'foo']);
}
/**
* @expectedException \GuzzleHttp\Exception\ClientException
*/
public function testThrowsHttpErrorsByDefault()
{
2015-04-17 19:59:11 -07:00
$mock = new MockHandler([new Response(404)]);
$handler = HandlerStack::create($mock);
2015-04-17 19:59:11 -07:00
$client = new Client(['handler' => $handler]);
2015-03-22 19:32:07 -07:00
$client->get('http://foo.com');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface
2015-03-22 19:32:07 -07:00
*/
public function testValidatesCookies()
{
$mock = new MockHandler([new Response(200, [], 'foo')]);
$handler = HandlerStack::create($mock);
2015-04-17 19:59:11 -07:00
$client = new Client(['handler' => $handler]);
$client->get('http://foo.com', ['cookies' => 'foo']);
2015-03-22 19:32:07 -07:00
}
public function testSetCookieToTrueUsesSharedJar()
{
$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'foo=bar']),
new Response()
]);
$handler = HandlerStack::create($mock);
2015-04-20 10:06:38 -07:00
$client = new Client(['handler' => $handler, 'cookies' => true]);
$client->get('http://foo.com');
$client->get('http://foo.com');
2015-04-19 20:39:53 -07:00
$this->assertEquals('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
2015-03-22 19:32:07 -07:00
}
public function testSetCookieToJar()
{
$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'foo=bar']),
new Response()
]);
$handler = HandlerStack::create($mock);
2015-04-17 19:59:11 -07:00
$client = new Client(['handler' => $handler]);
2015-03-22 19:32:07 -07:00
$jar = new CookieJar();
$client->get('http://foo.com', ['cookies' => $jar]);
$client->get('http://foo.com', ['cookies' => $jar]);
2015-04-19 20:39:53 -07:00
$this->assertEquals('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
2015-03-22 19:32:07 -07:00
}
public function testCanDisableContentDecoding()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['decode_content' => false]);
$last = $mock->getLastRequest();
$this->assertFalse($last->hasHeader('Accept-Encoding'));
$this->assertFalse($mock->getLastOptions()['decode_content']);
}
public function testCanSetContentDecodingToValue()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['decode_content' => 'gzip']);
$last = $mock->getLastRequest();
2015-04-19 20:39:53 -07:00
$this->assertEquals('gzip', $last->getHeaderLine('Accept-Encoding'));
2015-03-22 19:32:07 -07:00
$this->assertEquals('gzip', $mock->getLastOptions()['decode_content']);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesHeaders()
{
$mock = new MockHandler();
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['headers' => 'foo']);
}
public function testAddsBody()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$request = new Request('PUT', 'http://foo.com');
$client->send($request, ['body' => 'foo']);
$last = $mock->getLastRequest();
$this->assertEquals('foo', (string) $last->getBody());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesQuery()
{
$mock = new MockHandler();
$client = new Client(['handler' => $mock]);
$request = new Request('PUT', 'http://foo.com');
$client->send($request, ['query' => false]);
}
public function testQueryCanBeString()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$request = new Request('PUT', 'http://foo.com');
$client->send($request, ['query' => 'foo']);
$this->assertEquals('foo', $mock->getLastRequest()->getUri()->getQuery());
}
public function testQueryCanBeArray()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$request = new Request('PUT', 'http://foo.com');
$client->send($request, ['query' => ['foo' => 'bar baz']]);
$this->assertEquals('foo=bar%20baz', $mock->getLastRequest()->getUri()->getQuery());
}
public function testCanAddJsonData()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$request = new Request('PUT', 'http://foo.com');
$client->send($request, ['json' => ['foo' => 'bar']]);
$last = $mock->getLastRequest();
$this->assertEquals('{"foo":"bar"}', (string) $mock->getLastRequest()->getBody());
2015-04-19 20:39:53 -07:00
$this->assertEquals('application/json', $last->getHeaderLine('Content-Type'));
2015-03-22 19:32:07 -07:00
}
public function testCanAddJsonDataWithoutOverwritingContentType()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$request = new Request('PUT', 'http://foo.com');
$client->send($request, [
'headers' => ['content-type' => 'foo'],
'json' => 'a'
]);
$last = $mock->getLastRequest();
$this->assertEquals('"a"', (string) $mock->getLastRequest()->getBody());
2015-04-19 20:39:53 -07:00
$this->assertEquals('foo', $last->getHeaderLine('Content-Type'));
2015-03-22 19:32:07 -07:00
}
public function testAuthCanBeTrue()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['auth' => false]);
$last = $mock->getLastRequest();
$this->assertFalse($last->hasHeader('Authorization'));
}
public function testAuthCanBeArrayForBasicAuth()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['auth' => ['a', 'b']]);
$last = $mock->getLastRequest();
2015-04-19 20:39:53 -07:00
$this->assertEquals('Basic YTpi', $last->getHeaderLine('Authorization'));
2015-03-22 19:32:07 -07:00
}
public function testAuthCanBeArrayForDigestAuth()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['auth' => ['a', 'b', 'digest']]);
$last = $mock->getLastOptions();
$this->assertEquals([
CURLOPT_HTTPAUTH => 2,
CURLOPT_USERPWD => 'a:b'
2015-03-28 15:33:35 -07:00
], $last['curl']);
2015-03-22 19:32:07 -07:00
}
public function testAuthCanBeCustomType()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['auth' => 'foo']);
$last = $mock->getLastOptions();
$this->assertEquals('foo', $last['auth']);
}
public function testCanAddFormFields()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->post('http://foo.com', [
'form_fields' => [
'foo' => 'bar bam',
'baz' => ['boo' => 'qux']
]
]);
$last = $mock->getLastRequest();
$this->assertEquals(
'application/x-www-form-urlencoded',
2015-04-19 20:39:53 -07:00
$last->getHeaderLine('Content-Type')
2015-03-22 19:32:07 -07:00
);
$this->assertEquals(
'foo=bar+bam&baz%5Bboo%5D=qux',
(string) $last->getBody()
);
}
public function testCanAddFormFieldsAndFiles()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->post('http://foo.com', [
'form_fields' => ['foo' => 'bar'],
'form_files' => [
[
'name' => 'test',
'contents' => fopen(__FILE__, 'r')
]
]
]);
$last = $mock->getLastRequest();
$this->assertContains(
'multipart/form-data; boundary=',
2015-04-19 20:39:53 -07:00
$last->getHeaderLine('Content-Type')
2015-03-22 19:32:07 -07:00
);
$this->assertContains(
'Content-Disposition: form-data; name="foo"',
(string) $last->getBody()
);
$this->assertContains('bar', (string) $last->getBody());
$this->assertContains(
'Content-Disposition: form-data; name="test"; filename="ClientTest.php"',
(string) $last->getBody()
);
}
public function testUsesProxyEnvironmentVariables()
{
$http = getenv('HTTP_PROXY');
$https = getenv('HTTPS_PROXY');
$client = new Client();
$this->assertNull($client->getDefaultOption('proxy'));
putenv('HTTP_PROXY=127.0.0.1');
$client = new Client();
$this->assertEquals(
['http' => '127.0.0.1'],
$client->getDefaultOption('proxy')
);
putenv('HTTPS_PROXY=127.0.0.2');
$client = new Client();
$this->assertEquals(
['http' => '127.0.0.1', 'https' => '127.0.0.2'],
$client->getDefaultOption('proxy')
);
putenv("HTTP_PROXY=$http");
putenv("HTTPS_PROXY=$https");
}
public function testRequestSendsWithSync()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->request('GET', 'http://foo.com');
$this->assertTrue($mock->getLastOptions()['sync']);
}
public function testSendSendsWithSync()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->send(new Request('GET', 'http://foo.com'));
$this->assertTrue($mock->getLastOptions()['sync']);
}
public function testCanDisableDefaultMiddleware()
{
$mock = new MockHandler([new Response(500)]);
$client = new Client([
'handler' => $mock,
'disable_default_middleware' => true
]);
$this->assertEquals(
500,
$client->send(new Request('GET', 'http://foo.com'))->getStatusCode()
);
}
2015-03-03 14:02:24 -08:00
}