Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class MockResponse implements ResponseInterface, StreamableInterface

private $body;
private $requestOptions = [];
private $requestUrl;
private $requestMethod;

private static $mainMulti;
private static $idSequence = 0;
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down