scratch

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 86  →  ?path2? @ 87
/vendor/guzzlehttp/guzzle/tests/Message/AbstractMessageTest.php
@@ -0,0 +1,282 @@
<?php
namespace GuzzleHttp\Tests\Message;
 
use GuzzleHttp\Message\AbstractMessage;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
 
/**
* @covers \GuzzleHttp\Message\AbstractMessage
*/
class AbstractMessageTest extends \PHPUnit_Framework_TestCase
{
public function testHasProtocolVersion()
{
$m = new Request('GET', '/');
$this->assertEquals(1.1, $m->getProtocolVersion());
}
 
public function testHasHeaders()
{
$m = new Request('GET', 'http://foo.com');
$this->assertFalse($m->hasHeader('foo'));
$m->addHeader('foo', 'bar');
$this->assertTrue($m->hasHeader('foo'));
}
 
public function testInitializesMessageWithProtocolVersionOption()
{
$m = new Request('GET', '/', [], null, [
'protocol_version' => '10'
]);
$this->assertEquals(10, $m->getProtocolVersion());
}
 
public function testHasBody()
{
$m = new Request('GET', 'http://foo.com');
$this->assertNull($m->getBody());
$s = Stream::factory('test');
$m->setBody($s);
$this->assertSame($s, $m->getBody());
$this->assertFalse($m->hasHeader('Content-Length'));
}
 
public function testCanRemoveBodyBySettingToNullAndRemovesCommonBodyHeaders()
{
$m = new Request('GET', 'http://foo.com');
$m->setBody(Stream::factory('foo'));
$m->setHeader('Content-Length', 3)->setHeader('Transfer-Encoding', 'chunked');
$m->setBody(null);
$this->assertNull($m->getBody());
$this->assertFalse($m->hasHeader('Content-Length'));
$this->assertFalse($m->hasHeader('Transfer-Encoding'));
}
 
public function testCastsToString()
{
$m = new Request('GET', 'http://foo.com');
$m->setHeader('foo', 'bar');
$m->setBody(Stream::factory('baz'));
$this->assertEquals("GET / HTTP/1.1\r\nHost: foo.com\r\nfoo: bar\r\n\r\nbaz", (string) $m);
}
 
public function parseParamsProvider()
{
$res1 = array(
array(
'<http:/.../front.jpeg>',
'rel' => 'front',
'type' => 'image/jpeg',
),
array(
'<http://.../back.jpeg>',
'rel' => 'back',
'type' => 'image/jpeg',
),
);
 
return array(
array(
'<http:/.../front.jpeg>; rel="front"; type="image/jpeg", <http://.../back.jpeg>; rel=back; type="image/jpeg"',
$res1
),
array(
'<http:/.../front.jpeg>; rel="front"; type="image/jpeg",<http://.../back.jpeg>; rel=back; type="image/jpeg"',
$res1
),
array(
'foo="baz"; bar=123, boo, test="123", foobar="foo;bar"',
array(
array('foo' => 'baz', 'bar' => '123'),
array('boo'),
array('test' => '123'),
array('foobar' => 'foo;bar')
)
),
array(
'<http://.../side.jpeg?test=1>; rel="side"; type="image/jpeg",<http://.../side.jpeg?test=2>; rel=side; type="image/jpeg"',
array(
array('<http://.../side.jpeg?test=1>', 'rel' => 'side', 'type' => 'image/jpeg'),
array('<http://.../side.jpeg?test=2>', 'rel' => 'side', 'type' => 'image/jpeg')
)
),
array(
'',
array()
)
);
}
 
/**
* @dataProvider parseParamsProvider
*/
public function testParseParams($header, $result)
{
$request = new Request('GET', '/', ['foo' => $header]);
$this->assertEquals($result, Request::parseHeader($request, 'foo'));
}
 
public function testAddsHeadersWhenNotPresent()
{
$h = new Request('GET', 'http://foo.com');
$h->addHeader('foo', 'bar');
$this->assertInternalType('string', $h->getHeader('foo'));
$this->assertEquals('bar', $h->getHeader('foo'));
}
 
public function testAddsHeadersWhenPresentSameCase()
{
$h = new Request('GET', 'http://foo.com');
$h->addHeader('foo', 'bar')->addHeader('foo', 'baz');
$this->assertEquals('bar, baz', $h->getHeader('foo'));
$this->assertEquals(['bar', 'baz'], $h->getHeader('foo', true));
}
 
public function testAddsMultipleHeaders()
{
$h = new Request('GET', 'http://foo.com');
$h->addHeaders([
'foo' => ' bar',
'baz' => [' bam ', 'boo']
]);
$this->assertEquals([
'foo' => ['bar'],
'baz' => ['bam', 'boo'],
'Host' => ['foo.com']
], $h->getHeaders());
}
 
public function testAddsHeadersWhenPresentDifferentCase()
{
$h = new Request('GET', 'http://foo.com');
$h->addHeader('Foo', 'bar')->addHeader('fOO', 'baz');
$this->assertEquals('bar, baz', $h->getHeader('foo'));
}
 
public function testAddsHeadersWithArray()
{
$h = new Request('GET', 'http://foo.com');
$h->addHeader('Foo', ['bar', 'baz']);
$this->assertEquals('bar, baz', $h->getHeader('foo'));
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionWhenInvalidValueProvidedToAddHeader()
{
(new Request('GET', 'http://foo.com'))->addHeader('foo', false);
}
 
public function testGetHeadersReturnsAnArrayOfOverTheWireHeaderValues()
{
$h = new Request('GET', 'http://foo.com');
$h->addHeader('foo', 'bar');
$h->addHeader('Foo', 'baz');
$h->addHeader('boO', 'test');
$result = $h->getHeaders();
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('Foo', $result);
$this->assertArrayNotHasKey('foo', $result);
$this->assertArrayHasKey('boO', $result);
$this->assertEquals(['bar', 'baz'], $result['Foo']);
$this->assertEquals(['test'], $result['boO']);
}
 
public function testSetHeaderOverwritesExistingValues()
{
$h = new Request('GET', 'http://foo.com');
$h->setHeader('foo', 'bar');
$this->assertEquals('bar', $h->getHeader('foo'));
$h->setHeader('Foo', 'baz');
$this->assertEquals('baz', $h->getHeader('foo'));
$this->assertArrayHasKey('Foo', $h->getHeaders());
}
 
public function testSetHeaderOverwritesExistingValuesUsingHeaderArray()
{
$h = new Request('GET', 'http://foo.com');
$h->setHeader('foo', ['bar']);
$this->assertEquals('bar', $h->getHeader('foo'));
}
 
public function testSetHeaderOverwritesExistingValuesUsingArray()
{
$h = new Request('GET', 'http://foo.com');
$h->setHeader('foo', ['bar']);
$this->assertEquals('bar', $h->getHeader('foo'));
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionWhenInvalidValueProvidedToSetHeader()
{
(new Request('GET', 'http://foo.com'))->setHeader('foo', false);
}
 
public function testSetHeadersOverwritesAllHeaders()
{
$h = new Request('GET', 'http://foo.com');
$h->setHeader('foo', 'bar');
$h->setHeaders(['foo' => 'a', 'boo' => 'b']);
$this->assertEquals(['foo' => ['a'], 'boo' => ['b']], $h->getHeaders());
}
 
public function testChecksIfCaseInsensitiveHeaderIsPresent()
{
$h = new Request('GET', 'http://foo.com');
$h->setHeader('foo', 'bar');
$this->assertTrue($h->hasHeader('foo'));
$this->assertTrue($h->hasHeader('Foo'));
$h->setHeader('fOo', 'bar');
$this->assertTrue($h->hasHeader('Foo'));
}
 
public function testRemovesHeaders()
{
$h = new Request('GET', 'http://foo.com');
$h->setHeader('foo', 'bar');
$h->removeHeader('foo');
$this->assertFalse($h->hasHeader('foo'));
$h->setHeader('Foo', 'bar');
$h->removeHeader('FOO');
$this->assertFalse($h->hasHeader('foo'));
}
 
public function testReturnsCorrectTypeWhenMissing()
{
$h = new Request('GET', 'http://foo.com');
$this->assertInternalType('string', $h->getHeader('foo'));
$this->assertInternalType('array', $h->getHeader('foo', true));
}
 
public function testSetsIntegersAndFloatsAsHeaders()
{
$h = new Request('GET', 'http://foo.com');
$h->setHeader('foo', 10);
$h->setHeader('bar', 10.5);
$h->addHeader('foo', 10);
$h->addHeader('bar', 10.5);
$this->assertSame('10, 10', $h->getHeader('foo'));
$this->assertSame('10.5, 10.5', $h->getHeader('bar'));
}
 
public function testGetsResponseStartLine()
{
$m = new Response(200);
$this->assertEquals('HTTP/1.1 200 OK', Response::getStartLine($m));
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsWhenMessageIsUnknown()
{
$m = $this->getMockBuilder('GuzzleHttp\Message\AbstractMessage')
->getMockForAbstractClass();
AbstractMessage::getStartLine($m);
}
}
/vendor/guzzlehttp/guzzle/tests/Message/MessageFactoryTest.php
@@ -0,0 +1,597 @@
<?php
 
namespace GuzzleHttp\Tests\Message;
 
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Post\PostFile;
use GuzzleHttp\Query;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Subscriber\Cookie;
use GuzzleHttp\Subscriber\History;
use GuzzleHttp\Subscriber\Mock;
 
/**
* @covers GuzzleHttp\Message\MessageFactory
*/
class MessageFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreatesResponses()
{
$f = new MessageFactory();
$response = $f->createResponse(200, ['foo' => 'bar'], 'test', [
'protocol_version' => 1.0
]);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(['foo' => ['bar']], $response->getHeaders());
$this->assertEquals('test', $response->getBody());
$this->assertEquals(1.0, $response->getProtocolVersion());
}
 
public function testCreatesRequestFromMessage()
{
$f = new MessageFactory();
$req = $f->fromMessage("GET / HTTP/1.1\r\nBaz: foo\r\n\r\n");
$this->assertEquals('GET', $req->getMethod());
$this->assertEquals('/', $req->getPath());
$this->assertEquals('foo', $req->getHeader('Baz'));
$this->assertNull($req->getBody());
}
 
public function testCreatesRequestFromMessageWithBody()
{
$req = (new MessageFactory())->fromMessage("GET / HTTP/1.1\r\nBaz: foo\r\n\r\ntest");
$this->assertEquals('test', $req->getBody());
}
 
public function testCreatesRequestWithPostBody()
{
$req = (new MessageFactory())->createRequest('GET', 'http://www.foo.com', ['body' => ['abc' => '123']]);
$this->assertEquals('abc=123', $req->getBody());
}
 
public function testCreatesRequestWithPostBodyScalars()
{
$req = (new MessageFactory())->createRequest(
'GET',
'http://www.foo.com',
['body' => [
'abc' => true,
'123' => false,
'foo' => null,
'baz' => 10,
'bam' => 1.5,
'boo' => [1]]
]
);
$this->assertEquals(
'abc=1&123=&foo&baz=10&bam=1.5&boo%5B0%5D=1',
(string) $req->getBody()
);
}
 
public function testCreatesRequestWithPostBodyAndPostFiles()
{
$pf = fopen(__FILE__, 'r');
$pfi = new PostFile('ghi', 'abc', __FILE__);
$req = (new MessageFactory())->createRequest('GET', 'http://www.foo.com', [
'body' => [
'abc' => '123',
'def' => $pf,
'ghi' => $pfi
]
]);
$this->assertInstanceOf('GuzzleHttp\Post\PostBody', $req->getBody());
$s = (string) $req;
$this->assertContains('testCreatesRequestWithPostBodyAndPostFiles', $s);
$this->assertContains('multipart/form-data', $s);
$this->assertTrue(in_array($pfi, $req->getBody()->getFiles(), true));
}
 
public function testCreatesResponseFromMessage()
{
$response = (new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest");
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals('4', $response->getHeader('Content-Length'));
$this->assertEquals('test', $response->getBody(true));
}
 
public function testCanCreateHeadResponses()
{
$response = (new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\n");
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(null, $response->getBody());
$this->assertEquals('4', $response->getHeader('Content-Length'));
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testFactoryRequiresMessageForRequest()
{
(new MessageFactory())->fromMessage('');
}
 
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage foo
*/
public function testValidatesOptionsAreImplemented()
{
(new MessageFactory())->createRequest('GET', 'http://test.com', ['foo' => 'bar']);
}
 
public function testOptionsAddsRequestOptions()
{
$request = (new MessageFactory())->createRequest(
'GET', 'http://test.com', ['config' => ['baz' => 'bar']]
);
$this->assertEquals('bar', $request->getConfig()->get('baz'));
}
 
public function testCanDisableRedirects()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['allow_redirects' => false]);
$this->assertEmpty($request->getEmitter()->listeners('complete'));
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesRedirects()
{
(new MessageFactory())->createRequest('GET', '/', ['allow_redirects' => []]);
}
 
public function testCanEnableStrictRedirectsAndSpecifyMax()
{
$request = (new MessageFactory())->createRequest('GET', '/', [
'allow_redirects' => ['max' => 10, 'strict' => true]
]);
$this->assertTrue($request->getConfig()['redirect']['strict']);
$this->assertEquals(10, $request->getConfig()['redirect']['max']);
}
 
public function testCanAddCookiesFromHash()
{
$request = (new MessageFactory())->createRequest('GET', 'http://www.test.com/', [
'cookies' => ['Foo' => 'Bar']
]);
$cookies = null;
foreach ($request->getEmitter()->listeners('before') as $l) {
if ($l[0] instanceof Cookie) {
$cookies = $l[0];
break;
}
}
if (!$cookies) {
$this->fail('Did not add cookie listener');
} else {
$this->assertCount(1, $cookies->getCookieJar());
}
}
 
public function testAddsCookieUsingTrue()
{
$factory = new MessageFactory();
$request1 = $factory->createRequest('GET', '/', ['cookies' => true]);
$request2 = $factory->createRequest('GET', '/', ['cookies' => true]);
$listeners = function ($r) {
return array_filter($r->getEmitter()->listeners('before'), function ($l) {
return $l[0] instanceof Cookie;
});
};
$this->assertSame($listeners($request1), $listeners($request2));
}
 
public function testAddsCookieFromCookieJar()
{
$jar = new CookieJar();
$request = (new MessageFactory())->createRequest('GET', '/', ['cookies' => $jar]);
foreach ($request->getEmitter()->listeners('before') as $l) {
if ($l[0] instanceof Cookie) {
$this->assertSame($jar, $l[0]->getCookieJar());
}
}
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesCookies()
{
(new MessageFactory())->createRequest('GET', '/', ['cookies' => 'baz']);
}
 
public function testCanAddQuery()
{
$request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [
'query' => ['Foo' => 'Bar']
]);
$this->assertEquals('Bar', $request->getQuery()->get('Foo'));
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesQuery()
{
(new MessageFactory())->createRequest('GET', 'http://foo.com', [
'query' => 'foo'
]);
}
 
public function testCanSetDefaultQuery()
{
$request = (new MessageFactory())->createRequest('GET', 'http://foo.com?test=abc', [
'query' => ['Foo' => 'Bar', 'test' => 'def']
]);
$this->assertEquals('Bar', $request->getQuery()->get('Foo'));
$this->assertEquals('abc', $request->getQuery()->get('test'));
}
 
public function testCanSetDefaultQueryWithObject()
{
$request = (new MessageFactory())->createRequest('GET', 'http://foo.com?test=abc', [
'query' => new Query(['Foo' => 'Bar', 'test' => 'def'])
]);
$this->assertEquals('Bar', $request->getQuery()->get('Foo'));
$this->assertEquals('abc', $request->getQuery()->get('test'));
}
 
public function testCanAddBasicAuth()
{
$request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [
'auth' => ['michael', 'test']
]);
$this->assertTrue($request->hasHeader('Authorization'));
}
 
public function testCanAddDigestAuth()
{
$request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [
'auth' => ['michael', 'test', 'digest']
]);
$this->assertEquals('michael:test', $request->getConfig()->getPath('curl/' . CURLOPT_USERPWD));
$this->assertEquals(CURLAUTH_DIGEST, $request->getConfig()->getPath('curl/' . CURLOPT_HTTPAUTH));
}
 
public function testCanDisableAuth()
{
$request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [
'auth' => false
]);
$this->assertFalse($request->hasHeader('Authorization'));
}
 
public function testCanSetCustomAuth()
{
$request = (new MessageFactory())->createRequest('GET', 'http://foo.com', [
'auth' => 'foo'
]);
$this->assertEquals('foo', $request->getConfig()['auth']);
}
 
public function testCanAddEvents()
{
$foo = null;
$client = new Client();
$client->getEmitter()->attach(new Mock([new Response(200)]));
$client->get('http://test.com', [
'events' => [
'before' => function () use (&$foo) { $foo = true; }
]
]);
$this->assertTrue($foo);
}
 
public function testCanAddEventsWithPriority()
{
$foo = null;
$client = new Client();
$client->getEmitter()->attach(new Mock(array(new Response(200))));
$request = $client->createRequest('GET', 'http://test.com', [
'events' => [
'before' => [
'fn' => function () use (&$foo) { $foo = true; },
'priority' => 123
]
]
]);
$client->send($request);
$this->assertTrue($foo);
$l = $this->readAttribute($request->getEmitter(), 'listeners');
$this->assertArrayHasKey(123, $l['before']);
}
 
public function testCanAddEventsOnce()
{
$foo = 0;
$client = new Client();
$client->getEmitter()->attach(new Mock([
new Response(200),
new Response(200),
]));
$fn = function () use (&$foo) { ++$foo; };
$request = $client->createRequest('GET', 'http://test.com', [
'events' => ['before' => ['fn' => $fn, 'once' => true]]
]);
$client->send($request);
$this->assertEquals(1, $foo);
$client->send($request);
$this->assertEquals(1, $foo);
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesEventContainsFn()
{
$client = new Client(['base_url' => 'http://test.com']);
$client->createRequest('GET', '/', ['events' => ['before' => ['foo' => 'bar']]]);
}
 
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesEventIsArray()
{
$client = new Client(['base_url' => 'http://test.com']);
$client->createRequest('GET', '/', ['events' => ['before' => '123']]);
}
 
public function testCanAddSubscribers()
{
$mock = new Mock([new Response(200)]);
$client = new Client();
$client->getEmitter()->attach($mock);
$request = $client->get('http://test.com', ['subscribers' => [$mock]]);
}
 
public function testCanDisableExceptions()
{
$client = new Client();
$this->assertEquals(500, $client->get('http://test.com', [
'subscribers' => [new Mock([new Response(500)])],
'exceptions' => false
])->getStatusCode());
}
 
public function testCanChangeSaveToLocation()
{
$saveTo = Stream::factory();
$request = (new MessageFactory())->createRequest('GET', '/', ['save_to' => $saveTo]);
$this->assertSame($saveTo, $request->getConfig()->get('save_to'));
}
 
public function testCanSetProxy()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['proxy' => '192.168.16.121']);
$this->assertEquals('192.168.16.121', $request->getConfig()->get('proxy'));
}
 
public function testCanSetHeadersOption()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['headers' => ['Foo' => 'Bar']]);
$this->assertEquals('Bar', (string) $request->getHeader('Foo'));
}
 
public function testCanSetHeaders()
{
$request = (new MessageFactory())->createRequest('GET', '/', [
'headers' => ['Foo' => ['Baz', 'Bar'], 'Test' => '123']
]);
$this->assertEquals('Baz, Bar', $request->getHeader('Foo'));
$this->assertEquals('123', $request->getHeader('Test'));
}
 
public function testCanSetTimeoutOption()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['timeout' => 1.5]);
$this->assertEquals(1.5, $request->getConfig()->get('timeout'));
}
 
public function testCanSetConnectTimeoutOption()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['connect_timeout' => 1.5]);
$this->assertEquals(1.5, $request->getConfig()->get('connect_timeout'));
}
 
public function testCanSetDebug()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['debug' => true]);
$this->assertTrue($request->getConfig()->get('debug'));
}
 
public function testCanSetVerifyToOff()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['verify' => false]);
$this->assertFalse($request->getConfig()->get('verify'));
}
 
public function testCanSetVerifyToOn()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['verify' => true]);
$this->assertTrue($request->getConfig()->get('verify'));
}
 
public function testCanSetVerifyToPath()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['verify' => '/foo.pem']);
$this->assertEquals('/foo.pem', $request->getConfig()->get('verify'));
}
 
public function inputValidation()
{
return array_map(function ($option) { return array($option); }, array(
'headers', 'events', 'subscribers', 'params'
));
}
 
/**
* @dataProvider inputValidation
* @expectedException \InvalidArgumentException
*/
public function testValidatesInput($option)
{
(new MessageFactory())->createRequest('GET', '/', [$option => 'foo']);
}
 
public function testCanAddSslKey()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['ssl_key' => '/foo.pem']);
$this->assertEquals('/foo.pem', $request->getConfig()->get('ssl_key'));
}
 
public function testCanAddSslKeyPassword()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['ssl_key' => ['/foo.pem', 'bar']]);
$this->assertEquals(['/foo.pem', 'bar'], $request->getConfig()->get('ssl_key'));
}
 
public function testCanAddSslCert()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['cert' => '/foo.pem']);
$this->assertEquals('/foo.pem', $request->getConfig()->get('cert'));
}
 
public function testCanAddSslCertPassword()
{
$request = (new MessageFactory())->createRequest('GET', '/', ['cert' => ['/foo.pem', 'bar']]);
$this->assertEquals(['/foo.pem', 'bar'], $request->getConfig()->get('cert'));
}
 
public function testCreatesBodyWithoutZeroString()
{
$request = (new MessageFactory())->createRequest('PUT', 'http://test.com', ['body' => '0']);
$this->assertSame('0', (string) $request->getBody());
}
 
public function testCanSetProtocolVersion()
{
$request = (new MessageFactory())->createRequest('GET', 'http://t.com', ['version' => 1.0]);
$this->assertEquals(1.0, $request->getProtocolVersion());
}
 
public function testCanAddJsonData()
{
$request = (new MessageFactory())->createRequest('PUT', 'http://f.com', [
'json' => ['foo' => 'bar']
]);
$this->assertEquals(
'application/json',
$request->getHeader('Content-Type')
);
$this->assertEquals('{"foo":"bar"}', (string) $request->getBody());
}
 
public function testCanAddJsonDataToAPostRequest()
{
$request = (new MessageFactory())->createRequest('POST', 'http://f.com', [
'json' => ['foo' => 'bar']
]);
$this->assertEquals(
'application/json',
$request->getHeader('Content-Type')
);
$this->assertEquals('{"foo":"bar"}', (string) $request->getBody());
}
 
public function testCanAddJsonDataAndNotOverwriteContentType()
{
$request = (new MessageFactory())->createRequest('PUT', 'http://f.com', [
'headers' => ['Content-Type' => 'foo'],
'json' => null
]);
$this->assertEquals('foo', $request->getHeader('Content-Type'));
$this->assertEquals('null', (string) $request->getBody());
}
 
public function testCanUseCustomSubclassesWithMethods()
{
(new ExtendedFactory())->createRequest('PUT', 'http://f.com', [
'headers' => ['Content-Type' => 'foo'],
'foo' => 'bar'
]);
try {
$f = new MessageFactory();
$f->createRequest('PUT', 'http://f.com', [
'headers' => ['Content-Type' => 'foo'],
'foo' => 'bar'
]);
} catch (\InvalidArgumentException $e) {
$this->assertContains('foo config', $e->getMessage());
}
}
 
/**
* @ticket https://github.com/guzzle/guzzle/issues/706
*/
public function testDoesNotApplyPostBodyRightAway()
{
$request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [
'body' => ['foo' => ['bar', 'baz']]
]);
$this->assertEquals('', $request->getHeader('Content-Type'));
$this->assertEquals('', $request->getHeader('Content-Length'));
$request->getBody()->setAggregator(Query::duplicateAggregator());
$request->getBody()->applyRequestHeaders($request);
$this->assertEquals('foo=bar&foo=baz', $request->getBody());
}
 
public function testCanForceMultipartUploadWithContentType()
{
$client = new Client();
$client->getEmitter()->attach(new Mock([new Response(200)]));
$history = new History();
$client->getEmitter()->attach($history);
$client->post('http://foo.com', [
'headers' => ['Content-Type' => 'multipart/form-data'],
'body' => ['foo' => 'bar']
]);
$this->assertContains(
'multipart/form-data; boundary=',
$history->getLastRequest()->getHeader('Content-Type')
);
$this->assertContains(
"Content-Disposition: form-data; name=\"foo\"\r\n\r\nbar",
(string) $history->getLastRequest()->getBody()
);
}
 
public function testDecodeDoesNotForceAcceptHeader()
{
$request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [
'decode_content' => true
]);
$this->assertEquals('', $request->getHeader('Accept-Encoding'));
$this->assertTrue($request->getConfig()->get('decode_content'));
}
 
public function testDecodeCanAddAcceptHeader()
{
$request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [
'decode_content' => 'gzip'
]);
$this->assertEquals('gzip', $request->getHeader('Accept-Encoding'));
$this->assertTrue($request->getConfig()->get('decode_content'));
}
 
public function testCanDisableDecoding()
{
$request = (new MessageFactory())->createRequest('POST', 'http://f.cn', [
'decode_content' => false
]);
$this->assertEquals('', $request->getHeader('Accept-Encoding'));
$this->assertNull($request->getConfig()->get('decode_content'));
}
}
 
class ExtendedFactory extends MessageFactory
{
protected function add_foo() {}
}
/vendor/guzzlehttp/guzzle/tests/Message/MessageParserTest.php
@@ -0,0 +1,276 @@
<?php
 
namespace GuzzleHttp\Tests\Message;
 
use GuzzleHttp\Message\MessageParser;
 
/**
* @covers \GuzzleHttp\Message\MessageParser
*/
class MessageParserTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider requestProvider
*/
public function testParsesRequests($message, $parts)
{
$parser = new MessageParser();
$this->compareRequestResults($parts, $parser->parseRequest($message));
}
 
/**
* @dataProvider responseProvider
*/
public function testParsesResponses($message, $parts)
{
$parser = new MessageParser();
$this->compareResponseResults($parts, $parser->parseResponse($message));
}
 
public function testParsesRequestsWithMissingProtocol()
{
$parser = new MessageParser();
$parts = $parser->parseRequest("GET /\r\nHost: Foo.com\r\n\r\n");
$this->assertEquals('GET', $parts['method']);
$this->assertEquals('HTTP', $parts['protocol']);
$this->assertEquals('1.1', $parts['protocol_version']);
}
 
public function testParsesRequestsWithMissingVersion()
{
$parser = new MessageParser();
$parts = $parser->parseRequest("GET / HTTP\r\nHost: Foo.com\r\n\r\n");
$this->assertEquals('GET', $parts['method']);
$this->assertEquals('HTTP', $parts['protocol']);
$this->assertEquals('1.1', $parts['protocol_version']);
}
 
public function testParsesResponsesWithMissingReasonPhrase()
{
$parser = new MessageParser();
$parts = $parser->parseResponse("HTTP/1.1 200\r\n\r\n");
$this->assertEquals('200', $parts['code']);
$this->assertEquals('', $parts['reason_phrase']);
$this->assertEquals('HTTP', $parts['protocol']);
$this->assertEquals('1.1', $parts['protocol_version']);
}
 
public function requestProvider()
{
$auth = base64_encode('michael:foo');
 
return array(
 
// Empty request
array('', false),
 
// Converts casing of request. Does not require host header.
array("GET / HTTP/1.1\r\n\r\n", array(
'method' => 'GET',
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'request_url' => array(
'scheme' => 'http',
'host' => '',
'port' => '',
'path' => '/',
'query' => ''
),
'headers' => array(),
'body' => ''
)),
// Path and query string, multiple header values per header and case sensitive storage
array("HEAD /path?query=foo HTTP/1.0\r\nHost: example.com\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\nX-Foo: Baz\r\n\r\n", array(
'method' => 'HEAD',
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'request_url' => array(
'scheme' => 'http',
'host' => 'example.com',
'port' => '',
'path' => '/path',
'query' => 'query=foo'
),
'headers' => array(
'Host' => 'example.com',
'X-Foo' => array('foo', 'foo', 'Baz'),
'x-foo' => 'Bar'
),
'body' => ''
)),
// Includes a body
array("PUT / HTTP/1.0\r\nhost: example.com:443\r\nContent-Length: 4\r\n\r\ntest", array(
'method' => 'PUT',
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'request_url' => array(
'scheme' => 'https',
'host' => 'example.com',
'port' => '443',
'path' => '/',
'query' => ''
),
'headers' => array(
'host' => 'example.com:443',
'Content-Length' => '4'
),
'body' => 'test'
)),
// Includes Authorization headers
array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nAuthorization: Basic {$auth}\r\n\r\n", array(
'method' => 'GET',
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'request_url' => array(
'scheme' => 'http',
'host' => 'example.com',
'port' => '8080',
'path' => '/',
'query' => ''
),
'headers' => array(
'Host' => 'example.com:8080',
'Authorization' => "Basic {$auth}"
),
'body' => ''
)),
// Include authorization header
array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nauthorization: Basic {$auth}\r\n\r\n", array(
'method' => 'GET',
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'request_url' => array(
'scheme' => 'http',
'host' => 'example.com',
'port' => '8080',
'path' => '/',
'query' => ''
),
'headers' => array(
'Host' => 'example.com:8080',
'authorization' => "Basic {$auth}"
),
'body' => ''
)),
);
}
 
public function responseProvider()
{
return array(
// Empty request
array('', false),
 
array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'code' => '200',
'reason_phrase' => 'OK',
'headers' => array(
'Content-Length' => 0
),
'body' => ''
)),
array("HTTP/1.0 400 Bad Request\r\nContent-Length: 0\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'code' => '400',
'reason_phrase' => 'Bad Request',
'headers' => array(
'Content-Length' => 0
),
'body' => ''
)),
array("HTTP/1.0 100 Continue\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'code' => '100',
'reason_phrase' => 'Continue',
'headers' => array(),
'body' => ''
)),
array("HTTP/1.1 204 No Content\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'code' => '204',
'reason_phrase' => 'No Content',
'headers' => array(
'X-Foo' => array('foo', 'foo'),
'x-foo' => 'Bar'
),
'body' => ''
)),
array("HTTP/1.1 200 Ok that is great!\r\nContent-Length: 4\r\n\r\nTest", array(
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'code' => '200',
'reason_phrase' => 'Ok that is great!',
'headers' => array(
'Content-Length' => 4
),
'body' => 'Test'
)),
);
}
 
public function compareRequestResults($result, $expected)
{
if (!$result) {
$this->assertFalse($expected);
return;
}
 
$this->assertEquals($result['method'], $expected['method']);
$this->assertEquals($result['protocol'], $expected['protocol']);
$this->assertEquals($result['protocol_version'], $expected['protocol_version']);
$this->assertEquals($result['request_url'], $expected['request_url']);
$this->assertEquals($result['body'], $expected['body']);
$this->compareHttpHeaders($result['headers'], $expected['headers']);
}
 
public function compareResponseResults($result, $expected)
{
if (!$result) {
$this->assertFalse($expected);
return;
}
 
$this->assertEquals($result['protocol'], $expected['protocol']);
$this->assertEquals($result['protocol_version'], $expected['protocol_version']);
$this->assertEquals($result['code'], $expected['code']);
$this->assertEquals($result['reason_phrase'], $expected['reason_phrase']);
$this->assertEquals($result['body'], $expected['body']);
$this->compareHttpHeaders($result['headers'], $expected['headers']);
}
 
protected function normalizeHeaders($headers)
{
$normalized = array();
foreach ($headers as $key => $value) {
$key = strtolower($key);
if (!isset($normalized[$key])) {
$normalized[$key] = $value;
} elseif (!is_array($normalized[$key])) {
$normalized[$key] = array($value);
} else {
$normalized[$key][] = $value;
}
}
 
foreach ($normalized as $key => &$value) {
if (is_array($value)) {
sort($value);
}
}
 
return $normalized;
}
 
public function compareHttpHeaders($result, $expected)
{
// Aggregate all headers case-insensitively
$result = $this->normalizeHeaders($result);
$expected = $this->normalizeHeaders($expected);
$this->assertEquals($result, $expected);
}
}
/vendor/guzzlehttp/guzzle/tests/Message/RequestTest.php
@@ -0,0 +1,132 @@
<?php
 
namespace GuzzleHttp\Tests\Message;
 
use GuzzleHttp\Event\Emitter;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Query;
use GuzzleHttp\Stream\Stream;
 
/**
* @covers GuzzleHttp\Message\Request
*/
class RequestTest extends \PHPUnit_Framework_TestCase
{
public function testConstructorInitializesMessage()
{
$r = new Request('PUT', '/test', ['test' => '123'], Stream::factory('foo'));
$this->assertEquals('PUT', $r->getMethod());
$this->assertEquals('/test', $r->getUrl());
$this->assertEquals('123', $r->getHeader('test'));
$this->assertEquals('foo', $r->getBody());
}
 
public function testConstructorInitializesMessageWithProtocolVersion()
{
$r = new Request('GET', '', [], null, ['protocol_version' => 10]);
$this->assertEquals(10, $r->getProtocolVersion());
}
 
public function testConstructorInitializesMessageWithEmitter()
{
$e = new Emitter();
$r = new Request('GET', '', [], null, ['emitter' => $e]);
$this->assertSame($r->getEmitter(), $e);
}
 
public function testCloneIsDeep()
{
$r = new Request('GET', '/test', ['foo' => 'baz'], Stream::factory('foo'));
$r2 = clone $r;
 
$this->assertNotSame($r->getEmitter(), $r2->getEmitter());
$this->assertEquals('foo', $r2->getBody());
 
$r->getConfig()->set('test', 123);
$this->assertFalse($r2->getConfig()->hasKey('test'));
 
$r->setPath('/abc');
$this->assertEquals('/test', $r2->getPath());
}
 
public function testCastsToString()
{
$r = new Request('GET', 'http://test.com/test', ['foo' => 'baz'], Stream::factory('body'));
$s = explode("\r\n", (string) $r);
$this->assertEquals("GET /test HTTP/1.1", $s[0]);
$this->assertContains('Host: test.com', $s);
$this->assertContains('foo: baz', $s);
$this->assertContains('', $s);
$this->assertContains('body', $s);
}
 
public function testSettingUrlOverridesHostHeaders()
{
$r = new Request('GET', 'http://test.com/test');
$r->setUrl('https://baz.com/bar');
$this->assertEquals('baz.com', $r->getHost());
$this->assertEquals('baz.com', $r->getHeader('Host'));
$this->assertEquals('/bar', $r->getPath());
$this->assertEquals('https', $r->getScheme());
}
 
public function testQueryIsMutable()
{
$r = new Request('GET', 'http://www.foo.com?baz=bar');
$this->assertEquals('baz=bar', $r->getQuery());
$this->assertInstanceOf('GuzzleHttp\Query', $r->getQuery());
$r->getQuery()->set('hi', 'there');
$this->assertEquals('/?baz=bar&hi=there', $r->getResource());
}
 
public function testQueryCanChange()
{
$r = new Request('GET', 'http://www.foo.com?baz=bar');
$r->setQuery(new Query(['foo' => 'bar']));
$this->assertEquals('foo=bar', $r->getQuery());
}
 
public function testCanChangeMethod()
{
$r = new Request('GET', 'http://www.foo.com');
$r->setMethod('put');
$this->assertEquals('PUT', $r->getMethod());
}
 
public function testCanChangeSchemeWithPort()
{
$r = new Request('GET', 'http://www.foo.com:80');
$r->setScheme('https');
$this->assertEquals('https://www.foo.com', $r->getUrl());
}
 
public function testCanChangeScheme()
{
$r = new Request('GET', 'http://www.foo.com');
$r->setScheme('https');
$this->assertEquals('https://www.foo.com', $r->getUrl());
}
 
public function testCanChangeHost()
{
$r = new Request('GET', 'http://www.foo.com:222');
$r->setHost('goo');
$this->assertEquals('http://goo:222', $r->getUrl());
$this->assertEquals('goo:222', $r->getHeader('host'));
$r->setHost('goo:80');
$this->assertEquals('http://goo', $r->getUrl());
$this->assertEquals('goo', $r->getHeader('host'));
}
 
public function testCanChangePort()
{
$r = new Request('GET', 'http://www.foo.com:222');
$this->assertSame(222, $r->getPort());
$this->assertEquals('www.foo.com', $r->getHost());
$this->assertEquals('www.foo.com:222', $r->getHeader('host'));
$r->setPort(80);
$this->assertSame(80, $r->getPort());
$this->assertEquals('www.foo.com', $r->getHost());
$this->assertEquals('www.foo.com', $r->getHeader('host'));
}
}
/vendor/guzzlehttp/guzzle/tests/Message/ResponseTest.php
@@ -0,0 +1,111 @@
<?php
 
namespace GuzzleHttp\Tests\Message;
 
use GuzzleHttp\Exception\XmlParseException;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
 
/**
* @covers GuzzleHttp\Message\Response
*/
class ResponseTest extends \PHPUnit_Framework_TestCase
{
public function testCanProvideCustomStatusCodeAndReasonPhrase()
{
$response = new Response(999, [], null, ['reason_phrase' => 'hi!']);
$this->assertEquals(999, $response->getStatusCode());
$this->assertEquals('hi!', $response->getReasonPhrase());
}
 
public function testConvertsToString()
{
$response = new Response(200);
$this->assertEquals("HTTP/1.1 200 OK\r\n\r\n", (string) $response);
// Add another header
$response = new Response(200, ['X-Test' => 'Guzzle']);
$this->assertEquals("HTTP/1.1 200 OK\r\nX-Test: Guzzle\r\n\r\n", (string) $response);
$response = new Response(200, ['Content-Length' => 4], Stream::factory('test'));
$this->assertEquals("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest", (string) $response);
}
 
public function testConvertsToStringAndSeeksToByteZero()
{
$response = new Response(200);
$s = Stream::factory('foo');
$s->read(1);
$response->setBody($s);
$this->assertEquals("HTTP/1.1 200 OK\r\n\r\nfoo", (string) $response);
}
 
public function testParsesJsonResponses()
{
$json = '{"foo": "bar"}';
$response = new Response(200, [], Stream::factory($json));
$this->assertEquals(['foo' => 'bar'], $response->json());
$this->assertEquals(json_decode($json), $response->json(['object' => true]));
 
$response = new Response(200);
$this->assertEquals(null, $response->json());
}
 
/**
* @expectedException \GuzzleHttp\Exception\ParseException
* @expectedExceptionMessage Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
*/
public function testThrowsExceptionWhenFailsToParseJsonResponse()
{
$response = new Response(200, [], Stream::factory('{"foo": "'));
$response->json();
}
 
public function testParsesXmlResponses()
{
$response = new Response(200, [], Stream::factory('<abc><foo>bar</foo></abc>'));
$this->assertEquals('bar', (string) $response->xml()->foo);
// Always return a SimpleXMLElement from the xml method
$response = new Response(200);
$this->assertEmpty((string) $response->xml()->foo);
}
 
/**
* @expectedException \GuzzleHttp\Exception\XmlParseException
* @expectedExceptionMessage Unable to parse response body into XML: String could not be parsed as XML
*/
public function testThrowsExceptionWhenFailsToParseXmlResponse()
{
$response = new Response(200, [], Stream::factory('<abc'));
try {
$response->xml();
} catch (XmlParseException $e) {
$xmlParseError = $e->getError();
$this->assertInstanceOf('\LibXMLError', $xmlParseError);
$this->assertContains("Couldn't find end of Start Tag abc line 1", $xmlParseError->message);
throw $e;
}
}
 
public function testHasEffectiveUrl()
{
$r = new Response(200);
$this->assertNull($r->getEffectiveUrl());
$r->setEffectiveUrl('http://www.test.com');
$this->assertEquals('http://www.test.com', $r->getEffectiveUrl());
}
 
public function testPreventsComplexExternalEntities()
{
$xml = '<?xml version="1.0"?><!DOCTYPE scan[<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=ResponseTest.php">]><scan>&test;</scan>';
$response = new Response(200, [], Stream::factory($xml));
 
$oldCwd = getcwd();
chdir(__DIR__);
try {
$xml = $response->xml();
chdir($oldCwd);
$this->markTestIncomplete('Did not throw the expected exception! XML resolved as: ' . $xml->asXML());
} catch (\Exception $e) {
chdir($oldCwd);
}
}
}