minor #30626 [HttpClient] improve MockResponse (nicolas-grekas)
This PR was merged into the 4.3-dev branch.
Discussion
----------
[HttpClient] improve MockResponse
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
While working with `MockHttpClient`, we figured out these would be useful:
- `MockResponse::getRequestOptions()` to get the options that were used when doing the request
- relax the format of the `raw_headers` info and allow it to be defined as name=>value(s) pairs.
Commits
-------
26f6e28160
[HttpClient] improve MockResponse
This commit is contained in:
commit
6fdc1b43f7
@ -27,6 +27,7 @@ class MockResponse implements ResponseInterface
|
|||||||
use ResponseTrait;
|
use ResponseTrait;
|
||||||
|
|
||||||
private $body;
|
private $body;
|
||||||
|
private $requestOptions = [];
|
||||||
|
|
||||||
private static $mainMulti;
|
private static $mainMulti;
|
||||||
private static $idSequence = 0;
|
private static $idSequence = 0;
|
||||||
@ -42,6 +43,28 @@ class MockResponse implements ResponseInterface
|
|||||||
{
|
{
|
||||||
$this->body = \is_iterable($body) ? $body : (string) $body;
|
$this->body = \is_iterable($body) ? $body : (string) $body;
|
||||||
$this->info = $info + $this->info;
|
$this->info = $info + $this->info;
|
||||||
|
|
||||||
|
if (!isset($info['raw_headers'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rawHeaders = [];
|
||||||
|
|
||||||
|
foreach ($info['raw_headers'] as $k => $v) {
|
||||||
|
foreach ((array) $v as $v) {
|
||||||
|
$rawHeaders[] = (\is_string($k) ? $k.': ' : '').$v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$info['raw_headers'] = $rawHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the options used when doing the request.
|
||||||
|
*/
|
||||||
|
public function getRequestOptions(): array
|
||||||
|
{
|
||||||
|
return $this->requestOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,6 +89,7 @@ class MockResponse implements ResponseInterface
|
|||||||
public static function fromRequest(string $method, string $url, array $options, ResponseInterface $mock): self
|
public static function fromRequest(string $method, string $url, array $options, ResponseInterface $mock): self
|
||||||
{
|
{
|
||||||
$response = new self([]);
|
$response = new self([]);
|
||||||
|
$response->requestOptions = $options;
|
||||||
$response->id = ++self::$idSequence;
|
$response->id = ++self::$idSequence;
|
||||||
$response->content = ($options['buffer'] ?? true) ? fopen('php://temp', 'w+') : null;
|
$response->content = ($options['buffer'] ?? true) ? fopen('php://temp', 'w+') : null;
|
||||||
$response->initializer = static function (self $response) {
|
$response->initializer = static function (self $response) {
|
||||||
|
Reference in New Issue
Block a user