Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fdc1b4

Browse files
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 ------- 26f6e28 [HttpClient] improve MockResponse
2 parents af28965 + 26f6e28 commit 6fdc1b4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Symfony/Component/HttpClient/Response/MockResponse.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class MockResponse implements ResponseInterface
2727
use ResponseTrait;
2828

2929
private $body;
30+
private $requestOptions = [];
3031

3132
private static $mainMulti;
3233
private static $idSequence = 0;
@@ -42,6 +43,28 @@ public function __construct($body = '', array $info = [])
4243
{
4344
$this->body = \is_iterable($body) ? $body : (string) $body;
4445
$this->info = $info + $this->info;
46+
47+
if (!isset($info['raw_headers'])) {
48+
return;
49+
}
50+
51+
$rawHeaders = [];
52+
53+
foreach ($info['raw_headers'] as $k => $v) {
54+
foreach ((array) $v as $v) {
55+
$rawHeaders[] = (\is_string($k) ? $k.': ' : '').$v;
56+
}
57+
}
58+
59+
$info['raw_headers'] = $rawHeaders;
60+
}
61+
62+
/**
63+
* Returns the options used when doing the request.
64+
*/
65+
public function getRequestOptions(): array
66+
{
67+
return $this->requestOptions;
4568
}
4669

4770
/**
@@ -66,6 +89,7 @@ protected function close(): void
6689
public static function fromRequest(string $method, string $url, array $options, ResponseInterface $mock): self
6790
{
6891
$response = new self([]);
92+
$response->requestOptions = $options;
6993
$response->id = ++self::$idSequence;
7094
$response->content = ($options['buffer'] ?? true) ? fopen('php://temp', 'w+') : null;
7195
$response->initializer = static function (self $response) {

0 commit comments

Comments
 (0)