Skip to content

Commit 9d62aad

Browse files
committed
Add cookies header support for swoole
1 parent f23a6e7 commit 9d62aad

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"scripts": {
2525
"lint": "vendor/bin/pint --test",
2626
"format": "vendor/bin/pint",
27-
"check": "vendor/bin/phpstan analyse -c phpstan.neon",
27+
"check": "vendor/bin/phpstan analyse -c phpstan.neon --memory-limit 512M",
2828
"test": "vendor/bin/phpunit --configuration phpunit.xml",
2929
"bench": "vendor/bin/phpbench run --report=benchmark"
3030
},

src/Http/Adapter/Swoole/Request.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,21 @@ protected function generateInput(): array
359359
*/
360360
protected function generateHeaders(): array
361361
{
362-
return $this->swoole->header;
362+
$headers = $this->swoole->header;
363+
364+
if (empty($this->swoole->cookie)) {
365+
return $headers;
366+
}
367+
368+
$cookieHeaders = [];
369+
foreach ($this->swoole->cookie as $key => $value) {
370+
$cookieHeaders[] = "{$key}={$value}";
371+
}
372+
373+
if (!empty($cookieHeaders)) {
374+
$headers['cookie'] = \implode('; ', $cookieHeaders);
375+
}
376+
377+
return $headers;
363378
}
364379
}

tests/e2e/BaseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ public function testFile()
3535
$response = $this->client->call(Client::METHOD_GET, '/humans.txt');
3636
$this->assertEquals(204, $response['headers']['status-code']);
3737
}
38+
39+
public function testCookie()
40+
{
41+
$response = $this->client->call(Client::METHOD_GET, '/cookies', [
42+
'Cookie: cookie1=value1; cookie2=value2'
43+
]);
44+
45+
$this->assertEquals(200, $response['headers']['status-code']);
46+
$this->assertEquals('cookie1=value1; cookie2=value2', $response['body']);
47+
}
3848
}

tests/e2e/init.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_once __DIR__.'/../../vendor/autoload.php';
44

55
use Utopia\Http\Http;
6+
use Utopia\Http\Request;
67
use Utopia\Http\Response;
78
use Utopia\Http\Validator\Text;
89

@@ -25,6 +26,14 @@
2526
$response->send($value);
2627
});
2728

29+
30+
Http::get('/cookies')
31+
->inject('request')
32+
->inject('response')
33+
->action(function (Request $request, Response $response) {
34+
$response->send($request->getHeaders()['cookie'] ?? '');
35+
});
36+
2837
Http::get('/chunked')
2938
->inject('response')
3039
->action(function (Response $response) {

0 commit comments

Comments
 (0)