[HttpClient] Add MockResponse::getRequestMethod() and getRequestUrl() to allow inspecting which request has been sent

This commit is contained in:
Javier Espinosa 2020-04-18 01:05:12 +02:00 committed by Nicolas Grekas
parent 187b66bb40
commit 7a250d80c1
3 changed files with 34 additions and 0 deletions

View File

@ -7,6 +7,7 @@ CHANGELOG
* added `AsyncDecoratorTrait` to ease processing responses without breaking async
* added support for pausing responses with a new `pause_handler` callable exposed as an info item
* added `StreamableInterface` to ease turning responses into PHP streams
* added `MockResponse::getRequestMethod()` and `getRequestUrl()` to allow inspecting which request has been sent
5.1.0
-----

View File

@ -32,6 +32,8 @@ class MockResponse implements ResponseInterface, StreamableInterface
private $body;
private $requestOptions = [];
private $requestUrl;
private $requestMethod;
private static $mainMulti;
private static $idSequence = 0;
@ -72,6 +74,22 @@ class MockResponse implements ResponseInterface, StreamableInterface
return $this->requestOptions;
}
/**
* Returns the URL used when doing the request.
*/
public function getRequestUrl(): string
{
return $this->requestUrl;
}
/**
* Returns the method used when doing the request.
*/
public function getRequestMethod(): string
{
return $this->requestMethod;
}
/**
* {@inheritdoc}
*/
@ -122,6 +140,8 @@ class MockResponse implements ResponseInterface, StreamableInterface
if ($mock instanceof self) {
$mock->requestOptions = $response->requestOptions;
$mock->requestMethod = $method;
$mock->requestUrl = $url;
}
self::writeRequest($response, $options, $mock);

View File

@ -33,6 +33,19 @@ class MockResponseTest extends TestCase
$response->toArray();
}
public function testUrlHttpMethodMockResponse(): void
{
$responseMock = new MockResponse(json_encode(['foo' => 'bar']));
$url = 'https://example.com/some-endpoint';
$response = MockResponse::fromRequest('GET', $url, [], $responseMock);
$this->assertSame('GET', $response->getInfo('http_method'));
$this->assertSame('GET', $responseMock->getRequestMethod());
$this->assertSame($url, $response->getInfo('url'));
$this->assertSame($url, $responseMock->getRequestUrl());
}
public function toArrayErrors()
{
yield [