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

Rounding out test coverage

This commit is contained in:
Michael Dowling 2014-09-07 15:11:17 -07:00
parent e4b1486fd9
commit 15defe7bec
3 changed files with 100 additions and 8 deletions

View File

@ -172,7 +172,7 @@ final class RequestEvents
if (!($pos = strpos($url, '?'))) {
$qs = null;
} else {
$qs = substr($url, $pos);
$qs = substr($url, $pos + 1);
}
$r = [
@ -184,6 +184,7 @@ final class RequestEvents
'headers' => $request->getHeaders(),
'body' => $request->getBody(),
'client' => $request->getConfig()->toArray(),
'version' => $request->getProtocolVersion(),
'then' => function (array $response) use ($trans, $messageFactory) {
self::completeRingResponse($trans, $response, $messageFactory);
}
@ -205,8 +206,12 @@ final class RequestEvents
/**
* Handles the process of processing a response received from a handler.
*
* @param Transaction $trans Owns request and response.
* @param array $res Response array
* @param MessageFactoryInterface $messageFactory Creates response objects.
*/
private static function completeRingResponse(
public static function completeRingResponse(
Transaction $trans,
array $res,
MessageFactoryInterface $messageFactory

View File

@ -2,6 +2,12 @@
namespace GuzzleHttp\Tests\Event;
use GuzzleHttp\Client;
use GuzzleHttp\Event\ProgressEvent;
use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Ring\Client\MockAdapter;
use GuzzleHttp\Ring\Client\StreamAdapter;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Tests\Server;
use GuzzleHttp\Transaction;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\ErrorEvent;
@ -208,4 +214,86 @@ class RequestEventsTest extends \PHPUnit_Framework_TestCase
$result = RequestEvents::convertEventArray($in, $events, $add);
$this->assertEquals($out, $result);
}
public function testCreatesRingRequests()
{
$stream = Stream::factory('test');
$request = new Request('GET', 'http://httpbin.org/get?a=b', [
'test' => 'hello'
], $stream);
$request->getConfig()->set('foo', 'bar');
$trans = new Transaction(new Client(), $request);
$factory = new MessageFactory();
$r = RequestEvents::createRingRequest($trans, $factory);
$this->assertEquals('http', $r['scheme']);
$this->assertEquals('GET', $r['http_method']);
$this->assertEquals('http://httpbin.org/get?a=b', $r['url']);
$this->assertEquals('/get', $r['uri']);
$this->assertEquals('a=b', $r['query_string']);
$this->assertEquals([
'Host' => ['httpbin.org'],
'test' => ['hello']
], $r['headers']);
$this->assertSame($stream, $r['body']);
$this->assertEquals(['foo' => 'bar'], $r['client']);
$this->assertTrue(is_callable($r['then']));
}
public function testCreatesRingRequestsWithNullQueryString()
{
$request = new Request('GET', 'http://httpbin.org');
$trans = new Transaction(new Client(), $request);
$factory = new MessageFactory();
$r = RequestEvents::createRingRequest($trans, $factory);
$this->assertNull($r['query_string']);
$this->assertEquals('/', $r['uri']);
$this->assertEquals(['Host' => ['httpbin.org']], $r['headers']);
$this->assertNull($r['body']);
$this->assertEquals([], $r['client']);
}
public function testCallsThenAndAddsProgress()
{
Server::enqueue([new Response(200)]);
$client = new Client(['base_url' => Server::$url]);
$request = $client->createRequest('GET');
$called = false;
$request->getEmitter()->on(
'progress',
function (ProgressEvent $e) use (&$called) {
$called = true;
}
);
$this->assertEquals(200, $client->send($request)->getStatusCode());
$this->assertTrue($called);
}
public function testEmitsErrorEventOnError()
{
$client = new Client(['base_url' => 'http://127.0.0.1:123']);
$request = $client->createRequest('GET');
$request->getConfig()['timeout'] = 0.001;
$request->getConfig()['connect_timeout'] = 0.001;
try {
$client->send($request);
$this->fail('did not throw');
} catch (RequestException $e) {
$this->assertSame($request, $e->getRequest());
$this->assertContains('cURL error 7:', $e->getMessage());
}
}
public function testGetsResponseProtocolVersion()
{
$client = new Client([
'adapter' => new MockAdapter([
'status' => 200,
'headers' => [],
'version' => '1.0'
])
]);
$request = $client->createRequest('GET', 'http://foo.com');
$response = $client->send($request);
$this->assertEquals('1.0', $response->getProtocolVersion());
}
}

View File

@ -23,11 +23,10 @@ class Server
* Any currently queued responses will be overwritten. Subsequent requests
* on the server will return queued responses in FIFO order.
*
* @param array|ResponseInterface $responses A single or array of Responses
* to queue.
* @param array $responses Responses to queue.
* @throws \Exception
*/
public static function enqueue($responses)
public static function enqueue(array $responses)
{
static $factory;
if (!$factory) {
@ -35,7 +34,7 @@ class Server
}
$data = [];
foreach ((array) $responses as $response) {
foreach ($responses as $response) {
// Create the response object from a string
if (is_string($response)) {
$response = $factory->fromMessage($response);
@ -45,7 +44,7 @@ class Server
$data[] = self::convertResponse($response);
}
TestServer::enqueue($responses);
TestServer::enqueue($data);
}
/**
@ -94,7 +93,7 @@ class Server
TestServer::start();
}
private function convertResponse(Response $response)
private static function convertResponse(Response $response)
{
$headers = array_map(function ($h) {
return implode(', ', $h);