Skip to content

Commit ffbc27e

Browse files
authored
Merge pull request #188 from open-runtimes/feat-best-cookie-support
Feat: Best set-cookie suport implementation
2 parents d86f5cf + d70419b commit ffbc27e

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

app/controllers.php

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

264+
// Backwards compatibility for headers
265+
$responseFormat = $request->getHeader('x-executor-response-format', '0.10.0'); // Last version without support for array value for headers
266+
if (version_compare($responseFormat, '0.11.0', '<')) {
267+
foreach ($execution['headers'] as $key => $value) {
268+
if (\is_array($value)) {
269+
$execution['headers'][$key] = $value[\array_key_last($value)] ?? '';
270+
}
271+
}
272+
}
273+
264274
$acceptTypes = \explode(', ', $request->getHeader('accept', 'multipart/form-data'));
265275
$isJson = false;
266276

src/Executor/Runner/Docker.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,11 @@ public function createExecution(
978978
}
979979

980980
if (\array_key_exists($key, $responseHeaders)) {
981-
$responseHeaders[$key] .= ', ' . $value;
981+
if (is_array($responseHeaders[$key])) {
982+
$responseHeaders[$key][] = $value;
983+
} else {
984+
$responseHeaders[$key] = [$responseHeaders[$key], $value];
985+
}
982986
} else {
983987
$responseHeaders[$key] = $value;
984988
}
@@ -1030,6 +1034,9 @@ public function createExecution(
10301034

10311035
// Extract logs and errors from file based on fileId in header
10321036
$fileId = $responseHeaders['x-open-runtimes-log-id'] ?? '';
1037+
if (\is_array($fileId)) {
1038+
$fileId = $fileId[0] ?? '';
1039+
}
10331040
$logs = '';
10341041
$errors = '';
10351042
if (!empty($fileId)) {

tests/Client.php

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

2020
protected bool $selfSigned = false;
21+
protected string $version = '0.11.0';
2122
protected string $endpoint = '';
2223

2324
/**
@@ -61,6 +62,9 @@ public function addHeader(string $key, string $value): self
6162
public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true, callable $callback = null): array
6263
{
6364
$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+
}
6468

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

tests/ExecutorTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +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-
$this->assertEquals('cookie1=value1; Path=/; HttpOnly; Secure; SameSite=Lax, cookie2=value2; Path=/; HttpOnly; Secure; SameSite=Lax', \json_decode($response['body']['headers'], true)['set-cookie']);
385+
$this->assertEquals('aValue', \json_decode($response['body']['headers'], true)['x-key']);
386386

387387
/** Execute on cold-started runtime */
388388
$response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec/executions', [], [
@@ -570,7 +570,10 @@ public function testSSRLogs(): void
570570
$this->assertEquals(200, $response['headers']['status-code']);
571571
$this->assertEquals(200, $response['body']['statusCode']);
572572
$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']);
573+
574+
$setCookieList = \json_decode($response['body']['headers'], true)['set-cookie'];
575+
$this->assertEquals('astroCookie1=astroValue1; Max-Age=1800; HttpOnly', $setCookieList[0]);
576+
$this->assertEquals('astroCookie2=astroValue2; Max-Age=1800; HttpOnly', $setCookieList[1]);
574577

575578
$this->assertNotEmpty($response['body']['logs']);
576579
$this->assertStringContainsString('Open runtimes log', $response['body']['logs']);
@@ -579,9 +582,17 @@ public function testSSRLogs(): void
579582
$this->assertNotEmpty($response['body']['errors']);
580583
$this->assertStringContainsString('Open runtimes error', $response['body']['errors']);
581584

585+
$response = $this->client->call(Client::METHOD_POST, '/runtimes/test-ssr-exec/executions', [
586+
'x-executor-response-format' => '0.10.0' // Last version to report string header values only
587+
], $params);
588+
$this->assertEquals(200, $response['headers']['status-code']);
589+
$this->assertEquals(200, $response['body']['statusCode']);
590+
$this->assertEquals('astroCookie2=astroValue2; Max-Age=1800; HttpOnly', \json_decode($response['body']['headers'], true)['set-cookie']);
591+
582592
/** Delete runtime */
583593
$response = $this->client->call(Client::METHOD_DELETE, '/runtimes/test-ssr-exec', [], []);
584594
$this->assertEquals(200, $response['headers']['status-code']);
595+
$this->assertEquals('astroCookie1=astroValue1; Max-Age=1800; HttpOnly', $setCookieList[0]);
585596
}
586597

587598
public function testRestartPolicy(): void

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-
'Set-Cookie' => 'cookie1=value1; Path=/; HttpOnly; Secure; SameSite=Lax, cookie2=value2; Path=/; HttpOnly; Secure; SameSite=Lax'
25+
'x-key' => 'aValue'
2626
]);
2727
};

0 commit comments

Comments
 (0)