From 7a250d80c16330030e3ff0ce321d5033c895ffd3 Mon Sep 17 00:00:00 2001 From: Javier Espinosa Date: Sat, 18 Apr 2020 01:05:12 +0200 Subject: [PATCH] [HttpClient] Add MockResponse::getRequestMethod() and getRequestUrl() to allow inspecting which request has been sent --- src/Symfony/Component/HttpClient/CHANGELOG.md | 1 + .../HttpClient/Response/MockResponse.php | 20 +++++++++++++++++++ .../Tests/Response/MockResponseTest.php | 13 ++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/Symfony/Component/HttpClient/CHANGELOG.md b/src/Symfony/Component/HttpClient/CHANGELOG.md index e7762da5de1b1..ae2c9c00eaaf8 100644 --- a/src/Symfony/Component/HttpClient/CHANGELOG.md +++ b/src/Symfony/Component/HttpClient/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/HttpClient/Response/MockResponse.php b/src/Symfony/Component/HttpClient/Response/MockResponse.php index 4a43969127780..1d399d66252fc 100644 --- a/src/Symfony/Component/HttpClient/Response/MockResponse.php +++ b/src/Symfony/Component/HttpClient/Response/MockResponse.php @@ -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 @@ public function getRequestOptions(): array 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 @@ public static function fromRequest(string $method, string $url, array $options, if ($mock instanceof self) { $mock->requestOptions = $response->requestOptions; + $mock->requestMethod = $method; + $mock->requestUrl = $url; } self::writeRequest($response, $options, $mock); diff --git a/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php b/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php index dcc256e960440..aee6847b75ae4 100644 --- a/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php +++ b/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php @@ -33,6 +33,19 @@ public function testToArrayError($content, $responseHeaders, $message) $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 [