diff --git a/src/Http/Adapter/Swoole/Request.php b/src/Http/Adapter/Swoole/Request.php index 41ac541..92b7360 100644 --- a/src/Http/Adapter/Swoole/Request.php +++ b/src/Http/Adapter/Swoole/Request.php @@ -271,23 +271,9 @@ public function getFiles($key): array */ public function getCookie(string $key, string $default = ''): string { - $key = \strtolower($key); - - $cookies = \explode(';', $this->getHeader('cookie', '')); - foreach ($cookies as $cookie) { - $cookie = \trim($cookie); - if ($cookie === '') { - continue; - } - $parts = \explode('=', $cookie, 2); - $cookieKey = \trim($parts[0]); - $cookieValue = isset($parts[1]) ? \trim($parts[1]) : ''; - if ($cookieKey === $key) { - return $cookieValue; - } - } + $key = strtolower($key); - return $default; + return $this->swoole->cookie[$key] ?? $default; } /** @@ -391,12 +377,6 @@ protected function generateInput(): array */ protected function generateHeaders(): array { - $headers = $this->swoole->header; - - foreach ($headers as $key => $value) { - $headers[strtolower($key)] = $value; - } - - return $headers; + return $this->swoole->header; } } diff --git a/src/Http/Adapter/Swoole/Server.php b/src/Http/Adapter/Swoole/Server.php index 14d93bb..8ba2adb 100755 --- a/src/Http/Adapter/Swoole/Server.php +++ b/src/Http/Adapter/Swoole/Server.php @@ -18,9 +18,7 @@ class Server extends Adapter public function __construct(string $host, ?string $port = null, array $settings = [], int $mode = SWOOLE_PROCESS, ?Container $container = null) { $this->server = new SwooleServer($host, (int) $port, $mode); - $this->server->set(\array_merge($settings, [ - 'http_parse_cookie' => false, - ])); + $this->server->set($settings); $this->container = $container ?? new Container(); } diff --git a/src/Http/Adapter/SwooleCoroutine/Server.php b/src/Http/Adapter/SwooleCoroutine/Server.php index 456e3a8..52b1411 100644 --- a/src/Http/Adapter/SwooleCoroutine/Server.php +++ b/src/Http/Adapter/SwooleCoroutine/Server.php @@ -26,9 +26,7 @@ public function __construct( ?Container $container = null ) { $this->server = new SwooleServer($host, $port, false, true); - $this->server->set(\array_merge($settings, [ - 'http_parse_cookie' => false, - ])); + $this->server->set($settings); $this->container = $container ?? new Container(); } diff --git a/tests/e2e/BaseTest.php b/tests/e2e/BaseTest.php index 04b4249..9a9d453 100644 --- a/tests/e2e/BaseTest.php +++ b/tests/e2e/BaseTest.php @@ -36,39 +36,6 @@ public function testFile() $this->assertEquals(204, $response['headers']['status-code']); } - public function testCookie() - { - // One cookie - $cookie = 'cookie1=value1'; - $response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals($cookie, $response['body']); - - // Two cookiees - $cookie = 'cookie1=value1; cookie2=value2'; - $response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals($cookie, $response['body']); - - // Two cookies without optional space - $cookie = 'cookie1=value1;cookie2=value2'; - $response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals($cookie, $response['body']); - - // Cookie with "=" in value - $cookie = 'cookie1=value1=value2'; - $response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals($cookie, $response['body']); - - // Case sensitivity for cookie names - $cookie = 'cookie1=v1; Cookie1=v2'; - $response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals($cookie, $response['body']); - } - public function testSetCookie() { $response = $this->client->call(Client::METHOD_GET, '/set-cookie'); diff --git a/tests/e2e/ResponseFPMTest.php b/tests/e2e/ResponseFPMTest.php index 8a3cb4f..cd04bcd 100644 --- a/tests/e2e/ResponseFPMTest.php +++ b/tests/e2e/ResponseFPMTest.php @@ -14,4 +14,32 @@ public function setUp(): void { $this->client = new Client(); } + + public function testCookie(): void + { + $cookie = 'cookie1=value1'; + $response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($cookie, $response['body']); + + $cookie = 'cookie1=value1; cookie2=value2'; + $response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($cookie, $response['body']); + + $cookie = 'cookie1=value1;cookie2=value2'; + $response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($cookie, $response['body']); + + $cookie = 'cookie1=value1=value2'; + $response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($cookie, $response['body']); + + $cookie = 'cookie1=v1; Cookie1=v2'; + $response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($cookie, $response['body']); + } } diff --git a/tests/e2e/ResponseSwooleTest.php b/tests/e2e/ResponseSwooleTest.php index 0851554..284084c 100755 --- a/tests/e2e/ResponseSwooleTest.php +++ b/tests/e2e/ResponseSwooleTest.php @@ -15,6 +15,19 @@ public function setUp(): void $this->client = new Client('http://swoole'); } + public function testCookie(): void + { + $headers = ['Cookie: cookie1=value1; cookie2=value2']; + + $response = $this->client->call(Client::METHOD_GET, '/cookie/cookie1', $headers); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('value1', $response['body']); + + $response = $this->client->call(Client::METHOD_GET, '/cookie/cookie2', $headers); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('value2', $response['body']); + } + public function testSwooleResources(): void { $response = $this->client->call(Client::METHOD_DELETE, '/swoole-test'); diff --git a/tests/e2e/init.php b/tests/e2e/init.php index fb0d9d5..e521265 100644 --- a/tests/e2e/init.php +++ b/tests/e2e/init.php @@ -34,6 +34,14 @@ $response->send($request->getHeaders()['cookie'] ?? ''); }); +Http::get('/cookie/:key') + ->param('key', '', new Text(64)) + ->inject('request') + ->inject('response') + ->action(function (string $key, Request $request, Response $response) { + $response->send($request->getCookie($key, '')); + }); + Http::get('/set-cookie') ->inject('request') ->inject('response')