Skip to content

Commit 995c119

Browse files
authored
Use Swoole parsed cookies again (#233)
* Use Swoole parsed cookies again * Skip Swoole test without extension * Remove Swoole adapter unit test
1 parent c8e7e8f commit 995c119

File tree

7 files changed

+54
-62
lines changed

7 files changed

+54
-62
lines changed

src/Http/Adapter/Swoole/Request.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,9 @@ public function getFiles($key): array
271271
*/
272272
public function getCookie(string $key, string $default = ''): string
273273
{
274-
$key = \strtolower($key);
275-
276-
$cookies = \explode(';', $this->getHeader('cookie', ''));
277-
foreach ($cookies as $cookie) {
278-
$cookie = \trim($cookie);
279-
if ($cookie === '') {
280-
continue;
281-
}
282-
$parts = \explode('=', $cookie, 2);
283-
$cookieKey = \trim($parts[0]);
284-
$cookieValue = isset($parts[1]) ? \trim($parts[1]) : '';
285-
if ($cookieKey === $key) {
286-
return $cookieValue;
287-
}
288-
}
274+
$key = strtolower($key);
289275

290-
return $default;
276+
return $this->swoole->cookie[$key] ?? $default;
291277
}
292278

293279
/**
@@ -391,12 +377,6 @@ protected function generateInput(): array
391377
*/
392378
protected function generateHeaders(): array
393379
{
394-
$headers = $this->swoole->header;
395-
396-
foreach ($headers as $key => $value) {
397-
$headers[strtolower($key)] = $value;
398-
}
399-
400-
return $headers;
380+
return $this->swoole->header;
401381
}
402382
}

src/Http/Adapter/Swoole/Server.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class Server extends Adapter
1818
public function __construct(string $host, ?string $port = null, array $settings = [], int $mode = SWOOLE_PROCESS, ?Container $container = null)
1919
{
2020
$this->server = new SwooleServer($host, (int) $port, $mode);
21-
$this->server->set(\array_merge($settings, [
22-
'http_parse_cookie' => false,
23-
]));
21+
$this->server->set($settings);
2422
$this->container = $container ?? new Container();
2523
}
2624

src/Http/Adapter/SwooleCoroutine/Server.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public function __construct(
2626
?Container $container = null
2727
) {
2828
$this->server = new SwooleServer($host, $port, false, true);
29-
$this->server->set(\array_merge($settings, [
30-
'http_parse_cookie' => false,
31-
]));
29+
$this->server->set($settings);
3230
$this->container = $container ?? new Container();
3331
}
3432

tests/e2e/BaseTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,6 @@ public function testFile()
3636
$this->assertEquals(204, $response['headers']['status-code']);
3737
}
3838

39-
public function testCookie()
40-
{
41-
// One cookie
42-
$cookie = 'cookie1=value1';
43-
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]);
44-
$this->assertEquals(200, $response['headers']['status-code']);
45-
$this->assertEquals($cookie, $response['body']);
46-
47-
// Two cookiees
48-
$cookie = 'cookie1=value1; cookie2=value2';
49-
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]);
50-
$this->assertEquals(200, $response['headers']['status-code']);
51-
$this->assertEquals($cookie, $response['body']);
52-
53-
// Two cookies without optional space
54-
$cookie = 'cookie1=value1;cookie2=value2';
55-
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]);
56-
$this->assertEquals(200, $response['headers']['status-code']);
57-
$this->assertEquals($cookie, $response['body']);
58-
59-
// Cookie with "=" in value
60-
$cookie = 'cookie1=value1=value2';
61-
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]);
62-
$this->assertEquals(200, $response['headers']['status-code']);
63-
$this->assertEquals($cookie, $response['body']);
64-
65-
// Case sensitivity for cookie names
66-
$cookie = 'cookie1=v1; Cookie1=v2';
67-
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie: ' . $cookie ]);
68-
$this->assertEquals(200, $response['headers']['status-code']);
69-
$this->assertEquals($cookie, $response['body']);
70-
}
71-
7239
public function testSetCookie()
7340
{
7441
$response = $this->client->call(Client::METHOD_GET, '/set-cookie');

tests/e2e/ResponseFPMTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,32 @@ public function setUp(): void
1414
{
1515
$this->client = new Client();
1616
}
17+
18+
public function testCookie(): void
19+
{
20+
$cookie = 'cookie1=value1';
21+
$response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]);
22+
$this->assertEquals(200, $response['headers']['status-code']);
23+
$this->assertEquals($cookie, $response['body']);
24+
25+
$cookie = 'cookie1=value1; cookie2=value2';
26+
$response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]);
27+
$this->assertEquals(200, $response['headers']['status-code']);
28+
$this->assertEquals($cookie, $response['body']);
29+
30+
$cookie = 'cookie1=value1;cookie2=value2';
31+
$response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]);
32+
$this->assertEquals(200, $response['headers']['status-code']);
33+
$this->assertEquals($cookie, $response['body']);
34+
35+
$cookie = 'cookie1=value1=value2';
36+
$response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]);
37+
$this->assertEquals(200, $response['headers']['status-code']);
38+
$this->assertEquals($cookie, $response['body']);
39+
40+
$cookie = 'cookie1=v1; Cookie1=v2';
41+
$response = $this->client->call(Client::METHOD_GET, '/cookies', ['Cookie: ' . $cookie]);
42+
$this->assertEquals(200, $response['headers']['status-code']);
43+
$this->assertEquals($cookie, $response['body']);
44+
}
1745
}

tests/e2e/ResponseSwooleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ public function setUp(): void
1515
$this->client = new Client('http://swoole');
1616
}
1717

18+
public function testCookie(): void
19+
{
20+
$headers = ['Cookie: cookie1=value1; cookie2=value2'];
21+
22+
$response = $this->client->call(Client::METHOD_GET, '/cookie/cookie1', $headers);
23+
$this->assertEquals(200, $response['headers']['status-code']);
24+
$this->assertEquals('value1', $response['body']);
25+
26+
$response = $this->client->call(Client::METHOD_GET, '/cookie/cookie2', $headers);
27+
$this->assertEquals(200, $response['headers']['status-code']);
28+
$this->assertEquals('value2', $response['body']);
29+
}
30+
1831
public function testSwooleResources(): void
1932
{
2033
$response = $this->client->call(Client::METHOD_DELETE, '/swoole-test');

tests/e2e/init.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
$response->send($request->getHeaders()['cookie'] ?? '');
3535
});
3636

37+
Http::get('/cookie/:key')
38+
->param('key', '', new Text(64))
39+
->inject('request')
40+
->inject('response')
41+
->action(function (string $key, Request $request, Response $response) {
42+
$response->send($request->getCookie($key, ''));
43+
});
44+
3745
Http::get('/set-cookie')
3846
->inject('request')
3947
->inject('response')

0 commit comments

Comments
 (0)