|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\BrowserKit\Response as BrowserKitResponse; |
|
17
|
|
|
use Symfony\Component\HttpClient\Exception\ClientException; |
|
18
|
|
|
use Symfony\Component\HttpClient\Exception\JsonException; |
|
19
|
|
|
use Symfony\Component\HttpClient\Exception\RedirectionException; |
|
20
|
|
|
use Symfony\Component\HttpClient\Exception\ServerException; |
|
21
|
|
|
use Symfony\Component\HttpClient\Exception\TransportException; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; |
|
23
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* HTTP Response. |
|
27
|
|
|
* |
|
28
|
|
|
* @internal |
|
29
|
|
|
* |
|
30
|
|
|
* Partially copied from \Symfony\Component\HttpClient\Response\ResponseTrait |
|
31
|
|
|
* |
|
32
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
final class Response implements ResponseInterface |
|
35
|
|
|
{ |
|
36
|
|
|
private $httpFoundationResponse; |
|
37
|
|
|
private $browserKitResponse; |
|
38
|
|
|
private $headers; |
|
39
|
|
|
private $info; |
|
40
|
|
|
private $content; |
|
41
|
|
|
private $jsonData; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(HttpFoundationResponse $httpFoundationResponse, BrowserKitResponse $browserKitResponse, array $info) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->httpFoundationResponse = $httpFoundationResponse; |
|
46
|
|
|
$this->browserKitResponse = $browserKitResponse; |
|
47
|
|
|
|
|
48
|
|
|
$this->headers = $httpFoundationResponse->headers->all(); |
|
49
|
|
|
|
|
50
|
|
|
// Compute raw headers |
|
51
|
|
|
$responseHeaders = []; |
|
52
|
|
|
foreach ($this->headers as $key => $values) { |
|
53
|
|
|
foreach ($values as $value) { |
|
54
|
|
|
$responseHeaders[] = sprintf('%s: %s', $key, $value); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->content = $httpFoundationResponse->getContent(); |
|
59
|
|
|
$this->info = [ |
|
60
|
|
|
'http_code' => $httpFoundationResponse->getStatusCode(), |
|
61
|
|
|
'error' => null, |
|
62
|
|
|
'response_headers' => $responseHeaders, |
|
63
|
|
|
] + $info; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getInfo(string $type = null) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($type) { |
|
72
|
|
|
return $this->info[$type] ?? null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->info; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Checks the status, and try to extract message if appropriate. |
|
80
|
|
|
*/ |
|
81
|
|
|
private function checkStatusCode(): void |
|
82
|
|
|
{ |
|
83
|
|
|
if (500 <= $this->info['http_code']) { |
|
84
|
|
|
throw new ServerException($this); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (400 <= $this->info['http_code']) { |
|
88
|
|
|
throw new ClientException($this); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (300 <= $this->info['http_code']) { |
|
92
|
|
|
throw new RedirectionException($this); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getContent(bool $throw = true): string |
|
100
|
|
|
{ |
|
101
|
|
|
if ($throw) { |
|
102
|
|
|
$this->checkStatusCode(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->content; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getStatusCode(): int |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->info['http_code']; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getHeaders(bool $throw = true): array |
|
120
|
|
|
{ |
|
121
|
|
|
if ($throw) { |
|
122
|
|
|
$this->checkStatusCode(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->headers; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function toArray(bool $throw = true): array |
|
136
|
|
|
{ |
|
137
|
|
|
if ('' === $content = $this->getContent($throw)) { |
|
138
|
|
|
throw new TransportException('Response body is empty.'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if (null !== $this->jsonData) { |
|
142
|
|
|
return $this->jsonData; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$contentType = $this->headers['content-type'][0] ?? 'application/json'; |
|
146
|
|
|
|
|
147
|
|
|
if (!preg_match('/\bjson\b/i', $contentType)) { |
|
148
|
|
|
throw new JsonException(sprintf('Response content-type is "%s" while a JSON-compatible one was expected.', $contentType)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
try { |
|
152
|
|
|
$content = json_decode($content, true, 512, JSON_BIGINT_AS_STRING | (\PHP_VERSION_ID >= 70300 ? \JSON_THROW_ON_ERROR : 0)); |
|
153
|
|
|
} catch (\JsonException $e) { |
|
154
|
|
|
throw new JsonException($e->getMessage(), $e->getCode()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (\PHP_VERSION_ID < 70300 && JSON_ERROR_NONE !== json_last_error()) { |
|
158
|
|
|
throw new JsonException(json_last_error_msg(), json_last_error()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (!\is_array($content)) { |
|
162
|
|
|
throw new JsonException(sprintf('JSON content was expected to decode to an array, %s returned.', \gettype($content))); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->jsonData = $content; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Returns the internal HttpKernel response. |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getKernelResponse(): HttpFoundationResponse |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->httpFoundationResponse; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Returns the internal BrowserKit reponse. |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getBrowserKitResponse(): BrowserKitResponse |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->browserKitResponse; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc}. |
|
186
|
|
|
*/ |
|
187
|
|
|
public function cancel(): void |
|
188
|
|
|
{ |
|
189
|
|
|
$this->info['error'] = 'Response has been canceled.'; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|