1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 01:53:58 +01:00

Adding client tests

This commit is contained in:
Michael Dowling 2015-03-22 19:32:07 -07:00
parent 04a212896f
commit 831d3ab0ab
2 changed files with 344 additions and 43 deletions

View File

@ -336,6 +336,11 @@ class Client implements ClientInterface
// Case-insensitively merge in default headers if both defaults and
// options have headers specified.
if (!empty($defaults['headers']) && !empty($options['headers'])) {
if (!is_array($options['headers'])) {
throw new \InvalidArgumentException('headers must be an array');
}
// Create a set of lowercase keys that are present.
$lkeys = [];
foreach (array_keys($options['headers']) as $k) {
@ -468,8 +473,9 @@ class Client implements ClientInterface
*/
private function applyOptions(RequestInterface $request, array &$options)
{
$modify = [];
$this->extractFormData($options);
$modify = ['set_headers' => []];
$conditional = [];
$this->extractFormData($options, $conditional);
foreach ($options as $key => $value) {
if (isset(self::$transferOptions[$key])) {
@ -488,12 +494,7 @@ class Client implements ClientInterface
break;
case 'headers':
if (!is_array($value)) {
throw new Iae('header value must be an array');
}
foreach ($value as $k => $v) {
$modify['set_headers'][$k] = $v;
}
$modify['set_headers'] = $options['headers'] + $modify['set_headers'];
unset($options['headers']);
break;
@ -506,18 +507,20 @@ class Client implements ClientInterface
if (!$value) {
continue;
}
if (is_array($value)) {
$type = isset($value[2]) ? strtolower($value[2]) : 'basic';
} else {
$type = strtolower($value);
}
$type = is_array($value)
? (isset($value[2]) ? strtolower($value[2]) : 'basic')
: $value;
$config['auth'] = $value;
if ($type == 'basic') {
$modify['set_headers']['Authorization'] = 'Basic ' . base64_encode("$value[0]:$value[1]");
} elseif ($type == 'digest') {
// @todo: Do not rely on curl
$options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
$options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]";
switch (strtolower($type)) {
case 'basic':
$modify['set_headers']['Authorization'] = 'Basic '
. base64_encode("$value[0]:$value[1]");
break;
case 'digest':
// @todo: Do not rely on curl
$options['config']['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
$options['config']['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]";
break;
}
break;
@ -534,46 +537,37 @@ class Client implements ClientInterface
case 'json':
$modify['body'] = Psr7\stream_for(json_encode($value));
if (!$request->hasHeader('Content-Type')) {
$modify['set_headers']['Content-Type'] = 'application/json';
}
$conditional['Content-Type'] = 'application/json';
unset($options['json']);
break;
}
}
return Psr7\modify_request($request, $modify);
$request = Psr7\modify_request($request, $modify);
// Add in conditional headers.
foreach ($conditional as $k => $v) {
if (!$request->hasHeader($k)) {
$request = $request->withHeader($k, $v);
}
}
return $request;
}
/**
* Extracts form_fields and form_files into the "body" option.
*
* @param array $options
*/
private function extractFormData(array &$options)
private function extractFormData(array &$options, array &$conditional)
{
if (empty($options['form_files']) && empty($options['form_fields'])) {
return;
}
$contentType = null;
if (!empty($options['headers'])) {
foreach ($options['headers'] as $name => $value) {
if (strtolower($name) === 'content-type') {
$contentType = $value;
break;
}
}
}
$fields = [];
if (isset($options['form_fields'])) {
// Use a application/x-www-form-urlencoded POST with no files.
if (!isset($options['form_files'])) {
$options['body'] = http_build_query($options['form_fields']);
unset($options['form_fields']);
$options['headers']['Content-Type'] = $contentType
?: 'application/x-www-form-urlencoded';
$conditional['Content-Type'] = 'application/x-www-form-urlencoded';
return;
}
$fields = $options['form_fields'];
@ -584,8 +578,8 @@ class Client implements ClientInterface
unset($options['form_files']);
$options['body'] = new MultipartPostBody($fields, $files);
// Use a multipart/form-data POST if a Content-Type is not set.
$options['headers']['Content-Type'] = $contentType
?: 'multipart/form-data; boundary=' . $options['body']->getBoundary();
$conditional['Content-Type'] = 'multipart/form-data; boundary='
. $options['body']->getBoundary();
}
private function backwardsCompat(array &$options)

View File

@ -2,7 +2,10 @@
namespace GuzzleHttp\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
@ -151,4 +154,308 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$client->get('http://foo.com', ['save_to' => $r]);
$this->assertSame($r, $mock->getLastOptions()['sink']);
}
public function testAllowRedirectsCanBeTrue()
{
$mock = new MockHandler([new Response(200, [], 'foo')]);
$client = new Client(['handler' => $mock]);
$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')]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['allow_redirects' => 'foo']);
}
/**
* @expectedException \GuzzleHttp\Exception\ClientException
*/
public function testThrowsHttpErrorsByDefault()
{
$client = new Client(['handler' => new MockHandler([new Response(404)])]);
$client->get('http://foo.com');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage cookies must be an array, true, or CookieJarInterface
*/
public function testValidatesCookies()
{
$mock = new MockHandler([new Response(200, [], 'foo')]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['cookies' => 'foo']);
}
public function testSetCookieToTrueUsesSharedJar()
{
$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'foo=bar']),
new Response()
]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['cookies' => true]);
$client->get('http://foo.com', ['cookies' => true]);
$this->assertEquals('foo=bar', $mock->getLastRequest()->getHeader('Cookie'));
}
public function testSetCookieToJar()
{
$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'foo=bar']),
new Response()
]);
$client = new Client(['handler' => $mock]);
$jar = new CookieJar();
$client->get('http://foo.com', ['cookies' => $jar]);
$client->get('http://foo.com', ['cookies' => $jar]);
$this->assertEquals('foo=bar', $mock->getLastRequest()->getHeader('Cookie'));
}
public function testSetCookieToArray()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['cookies' => ['foo' => 'bar']]);
$this->assertEquals('foo=bar', $mock->getLastRequest()->getHeader('Cookie'));
}
public function testCanInjectIntoHandlerStackWithCallback()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', [
'stack' => function (HandlerStack $s) use (&$called) {
$s->push(Middleware::tap(function () use (&$called) {
$called = true;
}));
}
]);
$this->assertTrue($called);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testStackOptionMustBeCallable()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$client->get('http://foo.com', ['stack' => 'foo']);
}
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->assertEquals('gzip', $last->getHeader('Accept-Encoding'));
$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());
$this->assertEquals('application/json', $last->getHeader('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->assertEquals('"a"', (string) $mock->getLastRequest()->getBody());
$this->assertEquals('foo', $last->getHeader('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->assertEquals('Basic YTpi', $last->getHeader('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->assertEquals([
CURLOPT_HTTPAUTH => 2,
CURLOPT_USERPWD => 'a:b'
], $last['config']['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->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',
$last->getHeader('Content-Type')
);
$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=',
$last->getHeader('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="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");
}
}