-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClient.php
More file actions
305 lines (253 loc) · 9.72 KB
/
Copy pathClient.php
File metadata and controls
305 lines (253 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Testing;
use Hyperf\Codec\Packer\JsonPacker;
use Hyperf\Collection\Arr;
use Hyperf\Context\Context;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\PackerInterface;
use Hyperf\Dispatcher\HttpDispatcher;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
use Hyperf\HttpMessage\Server\Request as Psr7Request;
use Hyperf\HttpMessage\Server\Response as Psr7Response;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpMessage\Uri\Uri;
use Hyperf\HttpServer\MiddlewareManager;
use Hyperf\HttpServer\ResponseEmitter;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\HttpServer\Server;
use Hyperf\Support\Filesystem\Filesystem;
use Hyperf\Testing\HttpMessage\Upload\UploadedFile;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
use function Hyperf\Collection\data_get;
use function Hyperf\Coroutine\wait;
class Client extends Server
{
protected PackerInterface $packer;
protected float $waitTimeout = 10.0;
protected string $baseUri = 'http://127.0.0.1/';
public function __construct(ContainerInterface $container, ?PackerInterface $packer = null, $server = 'http')
{
parent::__construct(
$container,
$container->get(HttpDispatcher::class),
$container->get(ExceptionHandlerDispatcher::class),
$container->get(ResponseEmitter::class)
);
$this->packer = $packer ?? new JsonPacker();
$this->initCoreMiddleware($server);
$this->initBaseUri($server);
}
public function get(string $uri, array $data = [], array $headers = [])
{
$response = $this->request('GET', $uri, [
'headers' => $headers,
'query' => $data,
]);
return $this->packer->unpack((string) $response->getBody());
}
public function post(string $uri, array $data = [], array $headers = [])
{
$response = $this->request('POST', $uri, [
'headers' => $headers,
'form_params' => $data,
]);
return $this->packer->unpack((string) $response->getBody());
}
public function put(string $uri, array $data = [], array $headers = [])
{
$response = $this->request('PUT', $uri, [
'headers' => $headers,
'form_params' => $data,
]);
return $this->packer->unpack((string) $response->getBody());
}
public function patch(string $uri, array $data = [], array $headers = [])
{
$response = $this->request('PATCH', $uri, [
'headers' => $headers,
'form_params' => $data,
]);
return $this->packer->unpack((string) $response->getBody());
}
public function delete(string $uri, array $data = [], array $headers = [])
{
$response = $this->request('DELETE', $uri, [
'headers' => $headers,
'query' => $data,
]);
return $this->packer->unpack((string) $response->getBody());
}
public function json(string $uri, array $data = [], array $headers = [])
{
$headers['Content-Type'] = 'application/json';
$response = $this->request('POST', $uri, [
'headers' => $headers,
'json' => $data,
]);
return $this->packer->unpack((string) $response->getBody());
}
public function file(string $uri, array $data = [], array $headers = [])
{
$multipart = [];
if (Arr::isAssoc($data)) {
$data = [$data];
}
foreach ($data as $item) {
$name = $item['name'];
$file = $item['file'];
$multipart[] = [
'name' => $name,
'contents' => fopen($file, 'r'),
'filename' => basename($file),
];
}
$response = $this->request('POST', $uri, [
'headers' => $headers,
'multipart' => $multipart,
]);
return $this->packer->unpack((string) $response->getBody());
}
public function request(string $method, string $path, array $options = [], ?callable $callable = null)
{
return wait(function () use ($method, $path, $options, $callable) {
$callable && $callable();
return $this->execute($this->initRequest($method, $path, $options));
}, $this->waitTimeout);
}
public function sendRequest(ServerRequestInterface $psr7Request, ?callable $callable = null): ResponseInterface
{
return wait(function () use ($psr7Request, $callable) {
$callable && $callable();
return $this->execute($psr7Request);
}, $this->waitTimeout);
}
public function initRequest(string $method, string $path, array $options = []): ServerRequestInterface
{
$query = $options['query'] ?? [];
$params = $options['form_params'] ?? [];
$json = $options['json'] ?? [];
$headers = $options['headers'] ?? [];
$multipart = $options['multipart'] ?? [];
$parsePath = parse_url($path);
$path = $parsePath['path'];
$uriPathQuery = $parsePath['query'] ?? [];
if (! empty($uriPathQuery)) {
parse_str($uriPathQuery, $pathQuery);
$query = array_merge($pathQuery, $query);
}
$data = $params;
// Initialize PSR-7 Request and Response objects.
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
$content = http_build_query($params);
if (data_get($headers, 'Content-Type') == 'application/json' && ! empty($json)) {
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
$data = $json;
}
$body = new SwooleStream($content);
$request = new Psr7Request($method, $uri, $headers, $body);
$request->setServerParams($this->getServerParams($method, $uri->getPath()));
return $request->withQueryParams($query)
->withParsedBody($data)
->withUploadedFiles($this->normalizeFiles($multipart));
}
protected function execute(ServerRequestInterface $psr7Request): ResponseInterface
{
$this->persistToContext($psr7Request, new Psr7Response());
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
/** @var Dispatched $dispatched */
$dispatched = $psr7Request->getAttribute(Dispatched::class);
$middlewares = $this->middlewares;
if ($dispatched->isFound()) {
$registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
$middlewares = array_merge($middlewares, $registeredMiddlewares);
}
$middlewares = MiddlewareManager::sortMiddlewares($middlewares);
try {
$psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware);
} catch (Throwable $throwable) {
// Delegate the exception to exception handler.
$psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
}
return $psr7Response;
}
protected function persistToContext(ServerRequestInterface $request, ResponseInterface $response)
{
Context::set(ServerRequestInterface::class, $request);
Context::set(ResponseInterface::class, $response);
}
protected function initBaseUri(string $server): void
{
if ($this->container->has(ConfigInterface::class)) {
$config = $this->container->get(ConfigInterface::class);
$servers = $config->get('server.servers', []);
foreach ($servers as $item) {
if ($item['name'] == $server) {
$this->baseUri = sprintf('http://127.0.0.1:%d/', (int) $item['port']);
break;
}
}
}
}
protected function normalizeFiles(array $multipart): array
{
$files = [];
$fileSystem = $this->container->get(Filesystem::class);
foreach ($multipart as $item) {
if (isset($item['name'], $item['contents'], $item['filename'])) {
$name = $item['name'];
$contents = $item['contents'];
$filename = $item['filename'];
$dir = BASE_PATH . '/runtime/uploads';
$tmpName = $dir . '/' . $filename;
if (! is_dir($dir)) {
$fileSystem->makeDirectory($dir);
}
$fileSystem->put($tmpName, $contents);
$stats = fstat($contents);
$files[$name] = new UploadedFile(
$tmpName,
$stats['size'],
0,
$name
);
}
}
return $files;
}
protected function getStream(string $resource)
{
$stream = fopen('php://temp', 'r+');
if ($resource !== '') {
fwrite($stream, $resource);
fseek($stream, 0);
}
return $stream;
}
protected function getServerParams(string $method, string $uri): array
{
return [
'request_method' => $method,
'request_uri' => $uri,
'path_info' => $uri,
'request_time' => time(),
'request_time_float' => microtime(true),
'server_protocol' => 'HTTP/1.1',
'server_port' => 9501,
'remote_port' => 40005,
'remote_addr' => '127.0.0.1',
'master_time' => time(),
];
}
}