2015-02-26 16:43:45 -08:00
|
|
|
<?php
|
|
|
|
namespace GuzzleHttp\Tests;
|
|
|
|
|
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
|
use GuzzleHttp\Cookie\SetCookie;
|
2015-03-21 22:40:21 -07:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2015-02-26 16:43:45 -08:00
|
|
|
use GuzzleHttp\Handler\MockHandler;
|
2015-03-22 10:43:34 -07:00
|
|
|
use GuzzleHttp\HandlerStack;
|
2015-04-19 21:23:25 -07:00
|
|
|
use GuzzleHttp\MessageFormatter;
|
2015-02-26 16:43:45 -08:00
|
|
|
use GuzzleHttp\Middleware;
|
2015-03-21 15:53:38 -07:00
|
|
|
use GuzzleHttp\Promise\PromiseInterface;
|
2015-02-26 16:43:45 -08:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2019-10-30 12:56:22 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-02-26 16:43:45 -08:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
2015-03-28 13:52:39 -07:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2018-11-21 19:36:52 +02:00
|
|
|
use Psr\Log\Test\TestLogger;
|
2015-02-26 16:43:45 -08:00
|
|
|
|
2017-11-10 03:04:35 -02:00
|
|
|
class MiddlewareTest extends TestCase
|
2015-02-26 16:43:45 -08:00
|
|
|
{
|
|
|
|
public function testAddsCookiesToRequests()
|
|
|
|
{
|
|
|
|
$jar = new CookieJar();
|
|
|
|
$m = Middleware::cookies($jar);
|
2015-03-22 20:19:33 -07:00
|
|
|
$h = new MockHandler(
|
|
|
|
[
|
|
|
|
function (RequestInterface $request) {
|
|
|
|
return new Response(200, [
|
2019-08-13 09:35:30 +02:00
|
|
|
'Set-Cookie' => (string) new SetCookie([
|
2015-03-22 20:19:33 -07:00
|
|
|
'Name' => 'name',
|
|
|
|
'Value' => 'value',
|
|
|
|
'Domain' => 'foo.com'
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
2015-02-26 16:43:45 -08:00
|
|
|
$f = $m($h);
|
2015-04-13 23:35:07 -07:00
|
|
|
$f(new Request('GET', 'http://foo.com'), ['cookies' => $jar])->wait();
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertCount(1, $jar);
|
2015-02-26 16:43:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowsExceptionOnHttpClientError()
|
|
|
|
{
|
2015-03-31 21:33:31 -07:00
|
|
|
$m = Middleware::httpErrors();
|
2015-03-22 20:19:33 -07:00
|
|
|
$h = new MockHandler([new Response(404)]);
|
2015-02-26 16:43:45 -08:00
|
|
|
$f = $m($h);
|
2015-03-31 21:33:31 -07:00
|
|
|
$p = $f(new Request('GET', 'http://foo.com'), ['http_errors' => true]);
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertSame('pending', $p->getState());
|
2019-12-08 17:23:23 +02:00
|
|
|
|
|
|
|
$this->expectException(\GuzzleHttp\Exception\ClientException::class);
|
2015-02-26 16:43:45 -08:00
|
|
|
$p->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowsExceptionOnHttpServerError()
|
|
|
|
{
|
2015-03-31 21:33:31 -07:00
|
|
|
$m = Middleware::httpErrors();
|
2015-03-22 20:19:33 -07:00
|
|
|
$h = new MockHandler([new Response(500)]);
|
2015-02-26 16:43:45 -08:00
|
|
|
$f = $m($h);
|
2015-03-31 21:33:31 -07:00
|
|
|
$p = $f(new Request('GET', 'http://foo.com'), ['http_errors' => true]);
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertSame('pending', $p->getState());
|
2019-12-08 17:23:23 +02:00
|
|
|
|
|
|
|
$this->expectException(\GuzzleHttp\Exception\ServerException::class);
|
2015-02-26 16:43:45 -08:00
|
|
|
$p->wait();
|
|
|
|
}
|
|
|
|
|
2016-01-22 13:41:00 -08:00
|
|
|
/**
|
|
|
|
* @dataProvider getHistoryUseCases
|
|
|
|
*/
|
|
|
|
public function testTracksHistory($container)
|
2015-02-26 16:43:45 -08:00
|
|
|
{
|
|
|
|
$m = Middleware::history($container);
|
|
|
|
$h = new MockHandler([new Response(200), new Response(201)]);
|
|
|
|
$f = $m($h);
|
|
|
|
$p1 = $f(new Request('GET', 'http://foo.com'), ['headers' => ['foo' => 'bar']]);
|
|
|
|
$p2 = $f(new Request('HEAD', 'http://foo.com'), ['headers' => ['foo' => 'baz']]);
|
|
|
|
$p1->wait();
|
|
|
|
$p2->wait();
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertCount(2, $container);
|
|
|
|
self::assertSame(200, $container[0]['response']->getStatusCode());
|
|
|
|
self::assertSame(201, $container[1]['response']->getStatusCode());
|
|
|
|
self::assertSame('GET', $container[0]['request']->getMethod());
|
|
|
|
self::assertSame('HEAD', $container[1]['request']->getMethod());
|
|
|
|
self::assertSame('bar', $container[0]['options']['headers']['foo']);
|
|
|
|
self::assertSame('baz', $container[1]['options']['headers']['foo']);
|
2015-02-26 16:43:45 -08:00
|
|
|
}
|
|
|
|
|
2016-01-22 13:41:00 -08:00
|
|
|
public function getHistoryUseCases()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[[]], // 1. Container is an array
|
|
|
|
[new \ArrayObject()] // 2. Container is an ArrayObject
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-03-21 22:40:21 -07:00
|
|
|
public function testTracksHistoryForFailures()
|
|
|
|
{
|
|
|
|
$container = [];
|
|
|
|
$m = Middleware::history($container);
|
|
|
|
$request = new Request('GET', 'http://foo.com');
|
|
|
|
$h = new MockHandler([new RequestException('error', $request)]);
|
|
|
|
$f = $m($h);
|
2015-04-13 23:35:07 -07:00
|
|
|
$f($request, [])->wait(false);
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertCount(1, $container);
|
|
|
|
self::assertSame('GET', $container[0]['request']->getMethod());
|
|
|
|
self::assertInstanceOf(RequestException::class, $container[0]['error']);
|
2015-03-21 22:40:21 -07:00
|
|
|
}
|
|
|
|
|
2015-02-26 16:43:45 -08:00
|
|
|
public function testTapsBeforeAndAfter()
|
|
|
|
{
|
|
|
|
$calls = [];
|
|
|
|
$m = function ($handler) use (&$calls) {
|
|
|
|
return function ($request, $options) use ($handler, &$calls) {
|
|
|
|
$calls[] = '2';
|
|
|
|
return $handler($request, $options);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$m2 = Middleware::tap(
|
|
|
|
function (RequestInterface $request, array $options) use (&$calls) {
|
|
|
|
$calls[] = '1';
|
|
|
|
},
|
2015-03-21 15:53:38 -07:00
|
|
|
function (RequestInterface $request, array $options, PromiseInterface $p) use (&$calls) {
|
2015-02-26 16:43:45 -08:00
|
|
|
$calls[] = '3';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-03-22 20:19:33 -07:00
|
|
|
$h = new MockHandler([new Response()]);
|
2015-03-22 10:43:34 -07:00
|
|
|
$b = new HandlerStack($h);
|
2015-03-22 14:51:11 -07:00
|
|
|
$b->push($m2);
|
|
|
|
$b->push($m);
|
2015-03-22 10:43:34 -07:00
|
|
|
$comp = $b->resolve();
|
2015-02-26 16:43:45 -08:00
|
|
|
$p = $comp(new Request('GET', 'http://foo.com'), []);
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertSame('123', implode('', $calls));
|
|
|
|
self::assertInstanceOf(PromiseInterface::class, $p);
|
|
|
|
self::assertSame(200, $p->wait()->getStatusCode());
|
2015-02-26 16:43:45 -08:00
|
|
|
}
|
|
|
|
|
2015-03-28 13:52:39 -07:00
|
|
|
public function testMapsRequest()
|
|
|
|
{
|
|
|
|
$h = new MockHandler([
|
|
|
|
function (RequestInterface $request, array $options) {
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertSame('foo', $request->getHeaderLine('Bar'));
|
2015-03-28 13:52:39 -07:00
|
|
|
return new Response(200);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
$stack = new HandlerStack($h);
|
|
|
|
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
|
|
|
|
return $request->withHeader('Bar', 'foo');
|
|
|
|
}));
|
|
|
|
$comp = $stack->resolve();
|
|
|
|
$p = $comp(new Request('PUT', 'http://www.google.com'), []);
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertInstanceOf(PromiseInterface::class, $p);
|
2015-03-28 13:52:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMapsResponse()
|
|
|
|
{
|
|
|
|
$h = new MockHandler([new Response(200)]);
|
|
|
|
$stack = new HandlerStack($h);
|
|
|
|
$stack->push(Middleware::mapResponse(function (ResponseInterface $response) {
|
|
|
|
return $response->withHeader('Bar', 'foo');
|
|
|
|
}));
|
|
|
|
$comp = $stack->resolve();
|
|
|
|
$p = $comp(new Request('PUT', 'http://www.google.com'), []);
|
2015-04-13 23:35:07 -07:00
|
|
|
$p->wait();
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertSame('foo', $p->wait()->getHeaderLine('Bar'));
|
2015-03-28 13:52:39 -07:00
|
|
|
}
|
2015-04-19 21:23:25 -07:00
|
|
|
|
|
|
|
public function testLogsRequestsAndResponses()
|
|
|
|
{
|
|
|
|
$h = new MockHandler([new Response(200)]);
|
|
|
|
$stack = new HandlerStack($h);
|
2018-11-21 19:36:52 +02:00
|
|
|
$logger = new TestLogger();
|
2015-04-19 21:23:25 -07:00
|
|
|
$formatter = new MessageFormatter();
|
|
|
|
$stack->push(Middleware::log($logger, $formatter));
|
|
|
|
$comp = $stack->resolve();
|
|
|
|
$p = $comp(new Request('PUT', 'http://www.google.com'), []);
|
|
|
|
$p->wait();
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertCount(1, $logger->records);
|
2019-12-08 17:51:18 +02:00
|
|
|
self::assertStringContainsString('"PUT / HTTP/1.1" 200', $logger->records[0]['message']);
|
2015-04-19 21:23:25 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 08:58:35 -07:00
|
|
|
public function testLogsRequestsAndResponsesCustomLevel()
|
|
|
|
{
|
|
|
|
$h = new MockHandler([new Response(200)]);
|
|
|
|
$stack = new HandlerStack($h);
|
2018-11-21 19:36:52 +02:00
|
|
|
$logger = new TestLogger();
|
2015-05-29 08:58:35 -07:00
|
|
|
$formatter = new MessageFormatter();
|
|
|
|
$stack->push(Middleware::log($logger, $formatter, 'debug'));
|
|
|
|
$comp = $stack->resolve();
|
|
|
|
$p = $comp(new Request('PUT', 'http://www.google.com'), []);
|
|
|
|
$p->wait();
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertCount(1, $logger->records);
|
2019-12-08 17:51:18 +02:00
|
|
|
self::assertStringContainsString('"PUT / HTTP/1.1" 200', $logger->records[0]['message']);
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertSame('debug', $logger->records[0]['level']);
|
2015-05-29 08:58:35 -07:00
|
|
|
}
|
|
|
|
|
2015-04-19 21:23:25 -07:00
|
|
|
public function testLogsRequestsAndErrors()
|
|
|
|
{
|
|
|
|
$h = new MockHandler([new Response(404)]);
|
|
|
|
$stack = new HandlerStack($h);
|
2018-11-21 19:36:52 +02:00
|
|
|
$logger = new TestLogger();
|
2015-10-16 11:42:49 -05:00
|
|
|
$formatter = new MessageFormatter('{code} {error}');
|
2015-04-19 21:23:25 -07:00
|
|
|
$stack->push(Middleware::log($logger, $formatter));
|
|
|
|
$stack->push(Middleware::httpErrors());
|
|
|
|
$comp = $stack->resolve();
|
|
|
|
$p = $comp(new Request('PUT', 'http://www.google.com'), ['http_errors' => true]);
|
|
|
|
$p->wait(false);
|
2019-10-30 12:22:04 +01:00
|
|
|
self::assertCount(1, $logger->records);
|
2019-12-08 17:51:18 +02:00
|
|
|
self::assertStringContainsString('PUT http://www.google.com', $logger->records[0]['message']);
|
|
|
|
self::assertStringContainsString('404 Not Found', $logger->records[0]['message']);
|
2015-04-19 21:23:25 -07:00
|
|
|
}
|
2015-02-26 16:43:45 -08:00
|
|
|
}
|