Repository: brefphp/bref (language: php)

Issue:
Stop truncating multi-value headers
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

> Format 2.0 doesn't have multiValueHeaders or multiValueQueryStringParameters fields. Duplicate headers are combined with commas and included in the headers field

As API Gateway 2.0 doesn't support `multiValueHeaders` – Bref takes only the last value.

It seems like a better solution to not do anything and allow APIG to join them with a comma – at least that way there isn't any data loss, and your app can adjust to get the values.

Related: https://github.com/brefphp/bref/issues/818


Discussion / hints:
Hi, I'm not sure I understand everything.

Could you clarify with:

- what you expect to happen
- what happens instead

Thanks!
### What currently happens
Given a response with multi-value headers (2 unique headers with the same name), Bref will only keep the last header: https://github.com/brefphp/bref/blob/master/src/Event/Http/HttpResponse.php#L37

Eg, given

```
x-foo: bar
x-foo: baz
```

The APIG response will be constructed with only `x-foo: baz`

### What I expect to happen
I expect headers with duplicate names to be joined with a `,`, which is API Gateway v2's default behavior:

So, `x-foo: bar,baz`

https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

> Format 2.0 doesn't have multiValueHeaders or multiValueQueryStringParameters fields. Duplicate headers are combined with commas and included in the headers field

So, it seems like Bref shouldn't do any special handling for these headers, and instead rely on APIG's default handling.

Fix patch:
diff --git a/src/Event/Http/HttpResponse.php b/src/Event/Http/HttpResponse.php
index 7129f9073..80a679bc2 100644
--- a/src/Event/Http/HttpResponse.php
+++ b/src/Event/Http/HttpResponse.php
@@ -72,7 +72,7 @@ public function toApiGatewayFormatV2(): array
             } else {
                 // Make sure the values are never arrays
                 // because API Gateway v2 does not support multi-value headers
-                $headers[$name] = is_array($values) ? end($values) : $values;
+                $headers[$name] = is_array($values) ? implode(', ', $values) : $values;
             }
         }
 


Test patch:
diff --git a/tests/Event/Http/HttpResponseTest.php b/tests/Event/Http/HttpResponseTest.php
index 5dcc53cbc..dab15792e 100644
--- a/tests/Event/Http/HttpResponseTest.php
+++ b/tests/Event/Http/HttpResponseTest.php
@@ -58,7 +58,7 @@ public function test headers are capitalized()
         ], $response->toApiGatewayFormatV2());
     }
 
-    public function test nested arrays in headers are flattened()
+    public function test multi value headers()
     {
         $response = new HttpResponse('', [
             'foo' => ['bar', 'baz'],
@@ -76,8 +76,10 @@ public function test nested arrays in headers are flattened()
             'cookies' => [],
             'isBase64Encoded' => false,
             'statusCode' => 200,
-            // The last value is kept (when multiheaders are not enabled)
-            'headers' => ['Foo' => 'baz'],
+            // Headers are joined with a comma
+            // See https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2
+            // API Gateway v2 does not support multi-value headers
+            'headers' => ['Foo' => 'bar, baz'],
             'body' => '',
         ], $response->toApiGatewayFormatV2());
     }