Skip to content

Commit

Permalink
Fix Octane cookies for v2 (#185)
Browse files Browse the repository at this point in the history
* Fix Octane cookies for v2

* Fix tests
  • Loading branch information
Vinimaks authored Dec 6, 2024
1 parent 3eea89b commit 43cc210
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/Runtime/Octane/OctaneRequestContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function fromEvent($event, $serverVariables)
$request->serverVariables
);

$serverRequest = $serverRequest->withCookieParams(static::cookies($request->headers));
$serverRequest = $serverRequest->withCookieParams(static::cookies($event, $request->headers));

$serverRequest = $serverRequest->withUploadedFiles(static::uploadedFiles(
$method, $contentType, $request->body
Expand All @@ -60,18 +60,25 @@ public static function fromEvent($event, $serverVariables)
/**
* Get the cookies from the given headers.
*
* @param array $event
* @param array $headers
* @return array
*/
protected static function cookies($headers)
protected static function cookies($event, $headers)
{
$headers = array_change_key_case($headers);
if (isset($event['version']) && $event['version'] === '2.0') {
$cookies = $event['cookies'] ?? [];
} else {
$headers = array_change_key_case($headers);

$cookies = isset($headers['cookie']) ? explode('; ', $headers['cookie']) : [];
}

if (! isset($headers['cookie']) || empty($headers['cookie'])) {
if (empty($cookies)) {
return [];
}

return Collection::make(explode('; ', $headers['cookie']))->mapWithKeys(function ($cookie) {
return Collection::make($cookies)->mapWithKeys(function ($cookie) {
$cookie = explode('=', trim($cookie), 2);

$key = $cookie[0];
Expand Down
9 changes: 5 additions & 4 deletions tests/Feature/LambdaProxyOctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ public function test_request_cookies()
'path' => '/',
],
],
'headers' => [
'cookie' => 'XSRF-TOKEN=token_value',
'cookies' => [
'XSRF-TOKEN=token_value',
],
]);

Expand All @@ -423,8 +423,9 @@ public function test_request_ignores_invalid_cookies()
'path' => '/',
],
],
'headers' => [
'cookie' => 'cookieKey1; cookieKey2=cookieValue2',
'cookies' => [
'cookieKey1',
'cookieKey2=cookieValue2',
],
]);

Expand Down

0 comments on commit 43cc210

Please sign in to comment.