Skip to content

Commit 6337737

Browse files
Added document about how to use cookies for testing client. (#4427)
Co-authored-by: 李铭昕 <[email protected]>
1 parent 1786dcc commit 6337737

File tree

3 files changed

+63
-36
lines changed

3 files changed

+63
-36
lines changed

src/Client.php

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function file($uri, $data = [], $headers = [])
137137
public function request(string $method, string $path, array $options = [])
138138
{
139139
return wait(function () use ($method, $path, $options) {
140-
return $this->execute($this->init($method, $path, $options));
140+
return $this->execute($this->initRequest($method, $path, $options));
141141
}, $this->waitTimeout);
142142
}
143143

@@ -148,6 +148,50 @@ public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterf
148148
}, $this->waitTimeout);
149149
}
150150

151+
public function initRequest(string $method, string $path, array $options = []): ServerRequestInterface
152+
{
153+
$query = $options['query'] ?? [];
154+
$params = $options['form_params'] ?? [];
155+
$json = $options['json'] ?? [];
156+
$headers = $options['headers'] ?? [];
157+
$multipart = $options['multipart'] ?? [];
158+
159+
$parsePath = parse_url($path);
160+
$path = $parsePath['path'];
161+
$uriPathQuery = $parsePath['query'] ?? [];
162+
if (! empty($uriPathQuery)) {
163+
parse_str($uriPathQuery, $pathQuery);
164+
$query = array_merge($pathQuery, $query);
165+
}
166+
167+
$data = $params;
168+
169+
// Initialize PSR-7 Request and Response objects.
170+
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
171+
172+
$content = http_build_query($params);
173+
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
174+
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
175+
$data = $json;
176+
}
177+
178+
$body = new SwooleStream($content);
179+
180+
$request = new Psr7Request($method, $uri, $headers, $body);
181+
182+
return $request->withQueryParams($query)
183+
->withParsedBody($data)
184+
->withUploadedFiles($this->normalizeFiles($multipart));
185+
}
186+
187+
/**
188+
* @deprecated It will be removed in v3.0
189+
*/
190+
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
191+
{
192+
return $this->initRequest($method, $path, $options);
193+
}
194+
151195
protected function execute(ServerRequestInterface $psr7Request): ResponseInterface
152196
{
153197
$this->persistToContext($psr7Request, new Psr7Response());
@@ -191,41 +235,6 @@ protected function initBaseUri($server): void
191235
}
192236
}
193237

194-
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
195-
{
196-
$query = $options['query'] ?? [];
197-
$params = $options['form_params'] ?? [];
198-
$json = $options['json'] ?? [];
199-
$headers = $options['headers'] ?? [];
200-
$multipart = $options['multipart'] ?? [];
201-
202-
$parsePath = parse_url($path);
203-
$path = $parsePath['path'];
204-
$uriPathQuery = $parsePath['query'] ?? [];
205-
if (! empty($uriPathQuery)) {
206-
parse_str($uriPathQuery, $pathQuery);
207-
$query = array_merge($pathQuery, $query);
208-
}
209-
210-
$data = $params;
211-
212-
// Initialize PSR-7 Request and Response objects.
213-
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
214-
215-
$content = http_build_query($params);
216-
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
217-
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
218-
$data = $json;
219-
}
220-
221-
$body = new SwooleStream($content);
222-
223-
$request = new Psr7Request($method, $uri, $headers, $body);
224-
return $request->withQueryParams($query)
225-
->withParsedBody($data)
226-
->withUploadedFiles($this->normalizeFiles($multipart));
227-
}
228-
229238
protected function normalizeFiles(array $multipart): array
230239
{
231240
$files = [];

tests/ClientTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Hyperf\Server\Server;
2929
use Hyperf\Testing\Client;
3030
use Hyperf\Utils\ApplicationContext;
31+
use Hyperf\Utils\Codec\Json;
3132
use Hyperf\Utils\Coroutine;
3233
use Hyperf\Utils\Filesystem\Filesystem;
3334
use Hyperf\Utils\Serializer\SimpleNormalizer;
@@ -55,6 +56,21 @@ public function testClientRequest()
5556
$this->assertSame('Hello Hyperf!', $data['data']);
5657
}
5758

59+
public function testSendCookies()
60+
{
61+
$container = $this->getContainer();
62+
63+
$client = new Client($container);
64+
65+
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
66+
'X-CODE' => $id = uniqid(),
67+
]));
68+
69+
$data = Json::decode((string) $response->getBody());
70+
71+
$this->assertSame($id, $data['cookies']['X-CODE']);
72+
}
73+
5874
public function testClientReturnCoroutineId()
5975
{
6076
$container = $this->getContainer();

tests/Stub/FooController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function id()
3434

3535
public function request()
3636
{
37+
/** @var ServerRequestInterface $request */
3738
$request = Context::get(ServerRequestInterface::class);
3839
$uri = $request->getUri();
3940
return [
@@ -45,6 +46,7 @@ public function request()
4546
'query' => $uri->getQuery(),
4647
],
4748
'params' => $request->getQueryParams(),
49+
'cookies' => $request->getCookieParams(),
4850
];
4951
}
5052
}

0 commit comments

Comments
 (0)