Skip to content

Commit d86f5cf

Browse files
authored
Merge pull request #187 from open-runtimes/feat-set-cookie-comma-separtion
Feat: coma-separated set-cookie
2 parents b7e80f7 + 3170346 commit d86f5cf

File tree

6 files changed

+24
-45
lines changed

6 files changed

+24
-45
lines changed

app/controllers.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,6 @@ function (
261261
$log
262262
);
263263

264-
// Backwards compatibility for headers
265-
$responseFormat = $request->getHeader('x-executor-response-format', '0.8.6'); // Last version without support for this header
266-
if (version_compare($responseFormat, '0.9.0', '<')) {
267-
$oldHeaders = [];
268-
foreach ($execution['headers'] as $pair) {
269-
$oldHeaders[$pair['key']] = $pair['value'];
270-
}
271-
$execution['headers'] = $oldHeaders;
272-
}
273-
274264
$acceptTypes = \explode(', ', $request->getHeader('accept', 'multipart/form-data'));
275265
$isJson = false;
276266

src/Executor/Runner/Docker.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,11 @@ public function createExecution(
977977
$value = \urldecode($value);
978978
}
979979

980-
$responseHeaders[] = ['key' => $key, 'value' => $value ];
980+
if (\array_key_exists($key, $responseHeaders)) {
981+
$responseHeaders[$key] .= ', ' . $value;
982+
} else {
983+
$responseHeaders[$key] = $value;
984+
}
981985

982986
return $len;
983987
});
@@ -1025,14 +1029,7 @@ public function createExecution(
10251029
}
10261030

10271031
// Extract logs and errors from file based on fileId in header
1028-
$logIdHeader = [];
1029-
foreach ($responseHeaders as $responseHeader) {
1030-
if ($responseHeader['key'] === 'x-open-runtimes-log-id') {
1031-
$logIdHeader = $responseHeader;
1032-
break;
1033-
}
1034-
}
1035-
$fileId = $logIdHeader['value'] ?? '';
1032+
$fileId = $responseHeaders['x-open-runtimes-log-id'] ?? '';
10361033
$logs = '';
10371034
$errors = '';
10381035
if (!empty($fileId)) {
@@ -1067,12 +1064,12 @@ public function createExecution(
10671064
}
10681065

10691066
$outputHeaders = [];
1070-
foreach ($responseHeaders as $pair) {
1071-
if (\str_starts_with($pair['key'], 'x-open-runtimes-')) {
1067+
foreach ($responseHeaders as $key => $value) {
1068+
if (\str_starts_with($key, 'x-open-runtimes-')) {
10721069
continue;
10731070
}
10741071

1075-
$outputHeaders[] = $pair;
1072+
$outputHeaders[$key] = $value;
10761073
}
10771074

10781075
return [

tests/Client.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class Client
1818
public const METHOD_TRACE = 'TRACE';
1919

2020
protected bool $selfSigned = false;
21-
protected string $version = '0.9.0';
2221
protected string $endpoint = '';
2322

2423
/**
@@ -62,9 +61,6 @@ public function addHeader(string $key, string $value): self
6261
public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true, callable $callback = null): array
6362
{
6463
$headers = array_merge($this->headers, $headers);
65-
if (!\array_key_exists('x-executor-response-format', $headers)) {
66-
$headers['x-executor-response-format'] = $this->version;
67-
}
6864

6965
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
7066

tests/ExecutorTest.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -382,24 +382,7 @@ public function testExecute(): void
382382
$response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec/executions');
383383
$this->assertEquals(200, $response['headers']['status-code']);
384384
$this->assertEquals(200, $response['body']['statusCode']);
385-
386-
// Example of expected headers:
387-
// "[{"key":"x-my-cookie","value":"cookieValue"},{"key":"content-type","value":"application\/json; charset=utf-8"},{"key":"server","value":"swoole-http-server"},{"key":"date","value":"Tue, 02 Sep 2025 13:43:32 GMT"},{"key":"connection","value":"keep-alive"},{"key":"content-length","value":"235"}]"
388-
$headers = [];
389-
foreach (\json_decode($response['body']['headers'], true) as $pair) {
390-
$headers[$pair['key']] = $pair['value'];
391-
}
392-
$this->assertEquals('cookieValue', $headers['x-my-cookie']);
393-
394-
/** Execute with intentionally old version to ensure response filter */
395-
$response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec/executions', [
396-
'x-executor-response-format' => '0.8.6'
397-
]);
398-
$this->assertEquals(200, $response['headers']['status-code']);
399-
$this->assertEquals(200, $response['body']['statusCode']);
400-
// Example of expected headers:
401-
// "{"x-my-cookie":"cookieValue","content-type":"application\/json; charset=utf-8","server":"swoole-http-server","date":"Tue, 02 Sep 2025 13:38:22 GMT","connection":"keep-alive","content-length":"235"}"
402-
$this->assertEquals('cookieValue', \json_decode($response['body']['headers'], true)['x-my-cookie']);
385+
$this->assertEquals('cookie1=value1; Path=/; HttpOnly; Secure; SameSite=Lax, cookie2=value2; Path=/; HttpOnly; Secure; SameSite=Lax', \json_decode($response['body']['headers'], true)['set-cookie']);
403386

404387
/** Execute on cold-started runtime */
405388
$response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec/executions', [], [
@@ -550,6 +533,7 @@ public function testExecute(): void
550533
$this->assertEquals(200, $response['headers']['status-code']);
551534
}
552535

536+
// We also test SSR two Set-cookie here
553537
public function testSSRLogs(): void
554538
{
555539
/** Prepare function */
@@ -586,6 +570,7 @@ public function testSSRLogs(): void
586570
$this->assertEquals(200, $response['headers']['status-code']);
587571
$this->assertEquals(200, $response['body']['statusCode']);
588572
$this->assertStringContainsString('<p>OK</p>', $response['body']['body']);
573+
$this->assertEquals('astroCookie1=astroValue1; Max-Age=1800; HttpOnly, astroCookie2=astroValue2; Max-Age=1800; HttpOnly', \json_decode($response['body']['headers'], true)['set-cookie']);
589574

590575
$this->assertNotEmpty($response['body']['logs']);
591576
$this->assertStringContainsString('Open runtimes log', $response['body']['logs']);

tests/resources/functions/php/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
'url' => $context->req->url,
2323
'todo' => $todo
2424
], 200, [
25-
'x-my-cookie' => 'cookieValue'
25+
'Set-Cookie' => 'cookie1=value1; Path=/; HttpOnly; Secure; SameSite=Lax, cookie2=value2; Path=/; HttpOnly; Secure; SameSite=Lax'
2626
]);
2727
};

tests/resources/sites/astro/src/pages/logs.astro

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
console.log('Open runtimes log');
33
console.log('A developer log');
44
console.error('Open runtimes error');
5+
6+
Astro.cookies.set('astroCookie1', 'astroValue1', {
7+
httpOnly: true,
8+
maxAge: 1800
9+
});
10+
11+
Astro.cookies.set('astroCookie2', 'astroValue2', {
12+
httpOnly: true,
13+
maxAge: 1800
14+
});
15+
516
---
617

718
<p>OK</p>

0 commit comments

Comments
 (0)