-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAsyncTestClient.php
122 lines (108 loc) · 2.78 KB
/
AsyncTestClient.php
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
<?php
/*
* This file is part of the Apisearch Bundle.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/
declare(strict_types=1);
namespace Apisearch\Drift;
use Apisearch\Http\Client;
use Apisearch\Http\HttpClient;
use Apisearch\Http\RetryMap;
use Clue\React\Block;
use Drift\HttpKernel\AsyncKernel;
use Symfony\Component\HttpFoundation\Request;
/**
* Class AsyncTestClient.
*/
class AsyncTestClient extends Client implements HttpClient
{
/**
* @var AsyncKernel
*
* Async kernel
*/
private $kernel;
/**
* TestClient constructor.
*
* @param AsyncKernel $kernel
* @param string $version
* @param RetryMap $retryMap
*/
public function __construct(
AsyncKernel $kernel,
string $version,
RetryMap $retryMap
) {
$this->kernel = $kernel;
parent::__construct(
$version,
$retryMap
);
}
/**
* Get a response given some parameters.
* Return an array with the status code and the body.
*
* @param string $url
* @param string $method
* @param array $query
* @param array $body
* @param array $server
*
* @return array
*/
public function get(
string $url,
string $method,
array $query = [],
array $body = [],
array $server = []
): array {
$method = trim(strtolower($method));
$requestParts = $this->buildRequestParts(
$url,
$query,
$body,
$server
);
$headersFormatted = [];
foreach ($server as $key => $value) {
$headersFormatted['HTTP_'.str_replace('-', '_', $key)] = $value;
}
$request = new Request(
array_map('urldecode', $query),
[],
[],
[],
[],
array_merge($headersFormatted, [
'CONTENT_TYPE' => 'application/json',
'HTTP_REFERER' => 'http://localhost',
]),
json_encode($requestParts->getParameters()['json'])
);
$request->setMethod($method);
$request->server->set('REQUEST_URI', $requestParts->getUrl());
$promise = $this
->kernel
->handleAsync($request);
$response = Block\await(
$promise,
$this
->kernel
->getContainer()
->get('reactphp.event_loop')
);
return [
'code' => $response->getStatusCode(),
'body' => json_decode($response->getContent(), true),
];
}
}