diff --git a/src/Symfony/Component/HttpClient/CHANGELOG.md b/src/Symfony/Component/HttpClient/CHANGELOG.md index e7762da5de..ae2c9c00ea 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 4a43969127..1d399d6625 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 @@ 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); diff --git a/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php b/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php index dcc256e960..aee6847b75 100644 --- a/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php +++ b/src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php @@ -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 [