123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- <?php
- namespace GuzzleHttp\Tests;
- use GuzzleHttp\Client;
- use GuzzleHttp\Cookie\CookieJar;
- use GuzzleHttp\Handler\MockHandler;
- use GuzzleHttp\HandlerStack;
- use GuzzleHttp\Promise\PromiseInterface;
- use GuzzleHttp\Psr7;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Psr7\Response;
- use GuzzleHttp\Psr7\Uri;
- use Psr\Http\Message\ResponseInterface;
- use PHPUnit\Framework\TestCase;
- class ClientTest extends TestCase
- {
- public function testUsesDefaultHandler()
- {
- $client = new Client();
- Server::enqueue([new Response(200, ['Content-Length' => 0])]);
- $response = $client->get(Server::$url);
- $this->assertSame(200, $response->getStatusCode());
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Magic request methods require a URI and optional options array
- */
- public function testValidatesArgsForMagicMethods()
- {
- $client = new Client();
- $client->get();
- }
- 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(PromiseInterface::class, $p);
- $this->assertSame(200, $p->wait()->getStatusCode());
- $received = Server::received(true);
- $this->assertCount(1, $received);
- $this->assertSame('test=foo', $received[0]->getUri()->getQuery());
- }
- public function testCanSendSynchronously()
- {
- $client = new Client(['handler' => new MockHandler([new Response()])]);
- $request = new Request('GET', 'http://example.com');
- $r = $client->send($request);
- $this->assertInstanceOf(ResponseInterface::class, $r);
- $this->assertSame(200, $r->getStatusCode());
- }
- public function testClientHasOptions()
- {
- $client = new Client([
- 'base_uri' => 'http://foo.com',
- 'timeout' => 2,
- 'headers' => ['bar' => 'baz'],
- 'handler' => new MockHandler()
- ]);
- $base = $client->getConfig('base_uri');
- $this->assertSame('http://foo.com', (string) $base);
- $this->assertInstanceOf(Uri::class, $base);
- $this->assertNotNull($client->getConfig('handler'));
- $this->assertSame(2, $client->getConfig('timeout'));
- $this->assertArrayHasKey('timeout', $client->getConfig());
- $this->assertArrayHasKey('headers', $client->getConfig());
- }
- public function testCanMergeOnBaseUri()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client([
- 'base_uri' => 'http://foo.com/bar/',
- 'handler' => $mock
- ]);
- $client->get('baz');
- $this->assertSame(
- 'http://foo.com/bar/baz',
- (string)$mock->getLastRequest()->getUri()
- );
- }
- public function testCanMergeOnBaseUriWithRequest()
- {
- $mock = new MockHandler([new Response(), new Response()]);
- $client = new Client([
- 'handler' => $mock,
- 'base_uri' => 'http://foo.com/bar/'
- ]);
- $client->request('GET', new Uri('baz'));
- $this->assertSame(
- 'http://foo.com/bar/baz',
- (string) $mock->getLastRequest()->getUri()
- );
- $client->request('GET', new Uri('baz'), ['base_uri' => 'http://example.com/foo/']);
- $this->assertSame(
- 'http://example.com/foo/baz',
- (string) $mock->getLastRequest()->getUri(),
- 'Can overwrite the base_uri through the request options'
- );
- }
- public function testCanUseRelativeUriWithSend()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client([
- 'handler' => $mock,
- 'base_uri' => 'http://bar.com'
- ]);
- $this->assertSame('http://bar.com', (string) $client->getConfig('base_uri'));
- $request = new Request('GET', '/baz');
- $client->send($request);
- $this->assertSame(
- 'http://bar.com/baz',
- (string) $mock->getLastRequest()->getUri()
- );
- }
- public function testMergesDefaultOptionsAndDoesNotOverwriteUa()
- {
- $c = new Client(['headers' => ['User-agent' => 'foo']]);
- $this->assertSame(['User-agent' => 'foo'], $c->getConfig('headers'));
- $this->assertInternalType('array', $c->getConfig('allow_redirects'));
- $this->assertTrue($c->getConfig('http_errors'));
- $this->assertTrue($c->getConfig('decode_content'));
- $this->assertTrue($c->getConfig('verify'));
- }
- public function testDoesNotOverwriteHeaderWithDefault()
- {
- $mock = new MockHandler([new Response()]);
- $c = new Client([
- 'headers' => ['User-agent' => 'foo'],
- 'handler' => $mock
- ]);
- $c->get('http://example.com', ['headers' => ['User-Agent' => 'bar']]);
- $this->assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
- }
- 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->assertSame('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->assertSame('YO', $mock->getLastRequest()->getHeaderLine('User-Agent'));
- }
- public function testCanUnsetRequestOptionWithNull()
- {
- $mock = new MockHandler([new Response()]);
- $c = new Client([
- 'headers' => ['foo' => 'bar'],
- 'handler' => $mock
- ]);
- $c->get('http://example.com', ['headers' => null]);
- $this->assertFalse($mock->getLastRequest()->hasHeader('foo'));
- }
- public function testRewriteExceptionsToHttpErrors()
- {
- $client = new Client(['handler' => new MockHandler([new Response(404)])]);
- $res = $client->get('http://foo.com', ['exceptions' => false]);
- $this->assertSame(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']);
- }
- public function testAllowRedirectsCanBeTrue()
- {
- $mock = new MockHandler([new Response(200, [], 'foo')]);
- $handler = HandlerStack::create($mock);
- $client = new Client(['handler' => $handler]);
- $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);
- $client = new Client(['handler' => $handler]);
- $client->get('http://foo.com', ['allow_redirects' => 'foo']);
- }
- /**
- * @expectedException \GuzzleHttp\Exception\ClientException
- */
- public function testThrowsHttpErrorsByDefault()
- {
- $mock = new MockHandler([new Response(404)]);
- $handler = HandlerStack::create($mock);
- $client = new Client(['handler' => $handler]);
- $client->get('http://foo.com');
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface
- */
- public function testValidatesCookies()
- {
- $mock = new MockHandler([new Response(200, [], 'foo')]);
- $handler = HandlerStack::create($mock);
- $client = new Client(['handler' => $handler]);
- $client->get('http://foo.com', ['cookies' => 'foo']);
- }
- public function testSetCookieToTrueUsesSharedJar()
- {
- $mock = new MockHandler([
- new Response(200, ['Set-Cookie' => 'foo=bar']),
- new Response()
- ]);
- $handler = HandlerStack::create($mock);
- $client = new Client(['handler' => $handler, 'cookies' => true]);
- $client->get('http://foo.com');
- $client->get('http://foo.com');
- $this->assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
- }
- public function testSetCookieToJar()
- {
- $mock = new MockHandler([
- new Response(200, ['Set-Cookie' => 'foo=bar']),
- new Response()
- ]);
- $handler = HandlerStack::create($mock);
- $client = new Client(['handler' => $handler]);
- $jar = new CookieJar();
- $client->get('http://foo.com', ['cookies' => $jar]);
- $client->get('http://foo.com', ['cookies' => $jar]);
- $this->assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
- }
- 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();
- $this->assertSame('gzip', $last->getHeaderLine('Accept-Encoding'));
- $this->assertSame('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->assertSame('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->assertSame('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->assertSame('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->assertSame('{"foo":"bar"}', (string) $mock->getLastRequest()->getBody());
- $this->assertSame('application/json', $last->getHeaderLine('Content-Type'));
- }
- 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->assertSame('"a"', (string) $mock->getLastRequest()->getBody());
- $this->assertSame('foo', $last->getHeaderLine('Content-Type'));
- }
- 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();
- $this->assertSame('Basic YTpi', $last->getHeaderLine('Authorization'));
- }
- 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->assertSame([
- CURLOPT_HTTPAUTH => 2,
- CURLOPT_USERPWD => 'a:b'
- ], $last['curl']);
- }
- public function testAuthCanBeArrayForNtlmAuth()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $client->get('http://foo.com', ['auth' => ['a', 'b', 'ntlm']]);
- $last = $mock->getLastOptions();
- $this->assertSame([
- CURLOPT_HTTPAUTH => 8,
- CURLOPT_USERPWD => 'a:b'
- ], $last['curl']);
- }
- public function testAuthCanBeCustomType()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $client->get('http://foo.com', ['auth' => 'foo']);
- $last = $mock->getLastOptions();
- $this->assertSame('foo', $last['auth']);
- }
- public function testCanAddFormParams()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $client->post('http://foo.com', [
- 'form_params' => [
- 'foo' => 'bar bam',
- 'baz' => ['boo' => 'qux']
- ]
- ]);
- $last = $mock->getLastRequest();
- $this->assertSame(
- 'application/x-www-form-urlencoded',
- $last->getHeaderLine('Content-Type')
- );
- $this->assertSame(
- 'foo=bar+bam&baz%5Bboo%5D=qux',
- (string) $last->getBody()
- );
- }
- public function testFormParamsEncodedProperly()
- {
- $separator = ini_get('arg_separator.output');
- ini_set('arg_separator.output', '&');
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $client->post('http://foo.com', [
- 'form_params' => [
- 'foo' => 'bar bam',
- 'baz' => ['boo' => 'qux']
- ]
- ]);
- $last = $mock->getLastRequest();
- $this->assertSame(
- 'foo=bar+bam&baz%5Bboo%5D=qux',
- (string) $last->getBody()
- );
- ini_set('arg_separator.output', $separator);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsuresThatFormParamsAndMultipartAreExclusive()
- {
- $client = new Client(['handler' => function () {}]);
- $client->post('http://foo.com', [
- 'form_params' => ['foo' => 'bar bam'],
- 'multipart' => []
- ]);
- }
- public function testCanSendMultipart()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $client->post('http://foo.com', [
- 'multipart' => [
- [
- 'name' => 'foo',
- 'contents' => 'bar'
- ],
- [
- 'name' => 'test',
- 'contents' => fopen(__FILE__, 'r')
- ]
- ]
- ]);
- $last = $mock->getLastRequest();
- $this->assertContains(
- 'multipart/form-data; boundary=',
- $last->getHeaderLine('Content-Type')
- );
- $this->assertContains(
- 'Content-Disposition: form-data; name="foo"',
- (string) $last->getBody()
- );
- $this->assertContains('bar', (string) $last->getBody());
- $this->assertContains(
- 'Content-Disposition: form-data; name="foo"' . "\r\n",
- (string) $last->getBody()
- );
- $this->assertContains(
- 'Content-Disposition: form-data; name="test"; filename="ClientTest.php"',
- (string) $last->getBody()
- );
- }
- public function testCanSendMultipartWithExplicitBody()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $client->send(
- new Request(
- 'POST',
- 'http://foo.com',
- [],
- new Psr7\MultipartStream(
- [
- [
- 'name' => 'foo',
- 'contents' => 'bar',
- ],
- [
- 'name' => 'test',
- 'contents' => fopen(__FILE__, 'r'),
- ],
- ]
- )
- )
- );
- $last = $mock->getLastRequest();
- $this->assertContains(
- 'multipart/form-data; boundary=',
- $last->getHeaderLine('Content-Type')
- );
- $this->assertContains(
- 'Content-Disposition: form-data; name="foo"',
- (string) $last->getBody()
- );
- $this->assertContains('bar', (string) $last->getBody());
- $this->assertContains(
- 'Content-Disposition: form-data; name="foo"' . "\r\n",
- (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');
- $no = getenv('NO_PROXY');
- $client = new Client();
- $this->assertNull($client->getConfig('proxy'));
- putenv('HTTP_PROXY=127.0.0.1');
- $client = new Client();
- $this->assertSame(
- ['http' => '127.0.0.1'],
- $client->getConfig('proxy')
- );
- putenv('HTTPS_PROXY=127.0.0.2');
- putenv('NO_PROXY=127.0.0.3, 127.0.0.4');
- $client = new Client();
- $this->assertSame(
- ['http' => '127.0.0.1', 'https' => '127.0.0.2', 'no' => ['127.0.0.3','127.0.0.4']],
- $client->getConfig('proxy')
- );
- putenv("HTTP_PROXY=$http");
- putenv("HTTPS_PROXY=$https");
- putenv("NO_PROXY=$no");
- }
- public function testRequestSendsWithSync()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $client->request('GET', 'http://foo.com');
- $this->assertTrue($mock->getLastOptions()['synchronous']);
- }
- 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()['synchronous']);
- }
- public function testCanSetCustomHandler()
- {
- $mock = new MockHandler([new Response(500)]);
- $client = new Client(['handler' => $mock]);
- $mock2 = new MockHandler([new Response(200)]);
- $this->assertSame(
- 200,
- $client->send(new Request('GET', 'http://foo.com'), [
- 'handler' => $mock2
- ])->getStatusCode()
- );
- }
- public function testProperlyBuildsQuery()
- {
- $mock = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mock]);
- $request = new Request('PUT', 'http://foo.com');
- $client->send($request, ['query' => ['foo' => 'bar', 'john' => 'doe']]);
- $this->assertSame('foo=bar&john=doe', $mock->getLastRequest()->getUri()->getQuery());
- }
- public function testSendSendsWithIpAddressAndPortAndHostHeaderInRequestTheHostShouldBePreserved()
- {
- $mockHandler = new MockHandler([new Response()]);
- $client = new Client(['base_uri' => 'http://127.0.0.1:8585', 'handler' => $mockHandler]);
- $request = new Request('GET', '/test', ['Host'=>'foo.com']);
- $client->send($request);
- $this->assertSame('foo.com', $mockHandler->getLastRequest()->getHeader('Host')[0]);
- }
- public function testSendSendsWithDomainAndHostHeaderInRequestTheHostShouldBePreserved()
- {
- $mockHandler = new MockHandler([new Response()]);
- $client = new Client(['base_uri' => 'http://foo2.com', 'handler' => $mockHandler]);
- $request = new Request('GET', '/test', ['Host'=>'foo.com']);
- $client->send($request);
- $this->assertSame('foo.com', $mockHandler->getLastRequest()->getHeader('Host')[0]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testValidatesSink()
- {
- $mockHandler = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mockHandler]);
- $client->get('http://test.com', ['sink' => true]);
- }
- public function testHttpDefaultSchemeIfUriHasNone()
- {
- $mockHandler = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mockHandler]);
- $client->request('GET', '//example.org/test');
- $this->assertSame('http://example.org/test', (string) $mockHandler->getLastRequest()->getUri());
- }
- public function testOnlyAddSchemeWhenHostIsPresent()
- {
- $mockHandler = new MockHandler([new Response()]);
- $client = new Client(['handler' => $mockHandler]);
- $client->request('GET', 'baz');
- $this->assertSame(
- 'baz',
- (string) $mockHandler->getLastRequest()->getUri()
- );
- }
- /**
- * @expectedException InvalidArgumentException
- */
- public function testHandlerIsCallable()
- {
- new Client(['handler' => 'not_cllable']);
- }
- }
|