fix(lambda): add cookies array to function URL events (#5288)

* Fix #5255

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-07 22:35:51 -04:00
1 parent 985921c3db
commit 673c809eff
5 files changed
+127 -20

No files matched your search

@@ -0,0 +1,29 @@
package io.github.hectorvent.floci.core.common;
import org.junit.jupiter.api.Test;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CookieHeadersTest {
@Test
void splitsEveryCookieHeaderInRequestOrder() {
Map<String, List<String>> headers = new LinkedHashMap<>();
headers.put("Cookie", List.of("a=1; b=2;; ", " c=3 "));
headers.put("x-other", List.of("v"));
headers.put("cookie", List.of("d=4"));
assertEquals(List.of("a=1", "b=2", "c=3", "d=4"), CookieHeaders.cookiePairs(headers));
}
@Test
void noCookieHeaderGivesNoPairs() {
assertTrue(CookieHeaders.cookiePairs(Map.of("x-other", List.of("v"))).isEmpty());
assertTrue(CookieHeaders.cookiePairs(Map.of("Cookie", List.of(" ; "))).isEmpty());
}
}
@@ -301,6 +301,52 @@ class LambdaUrlInvocationControllerTest {
assertNull(response.getEntity());
}
private JsonNode eventFor(HttpHeaders headers) {
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("my-function");
fn.setFunctionArn(FUNCTION_ARN);
fn.setAccountId("100000000012");
LambdaService lambdaService = mock(LambdaService.class);
when(lambdaService.getTargetByUrlId("url-id")).thenReturn(fn);
InvokeResult invokeResult = new InvokeResult();
invokeResult.setStatusCode(200);
invokeResult.setPayload("{\"statusCode\":200}".getBytes(StandardCharsets.UTF_8));
when(lambdaService.invokeArn(eq(FUNCTION_ARN), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(invokeResult);
newController(lambdaService).handleGet("url-id", "", headers,
uriInfoFor("http://localhost/lambda-url/url-id/"));
ArgumentCaptor<byte[]> event = ArgumentCaptor.forClass(byte[].class);
verify(lambdaService).invokeArn(eq(FUNCTION_ARN), event.capture(), eq(InvocationType.RequestResponse));
return readTree(event.getValue());
}
@Test
void cookieHeadersBecomeCookiesArray() {
HttpHeaders headers = headersWith(null);
headers.getRequestHeaders().add("Cookie", "a=1; b=2;; ");
headers.getRequestHeaders().add("cookie", " c=3 ");
headers.getRequestHeaders().putSingle("x-other", "v");
JsonNode eventNode = eventFor(headers);
assertEquals(3, eventNode.get("cookies").size());
assertEquals("a=1", eventNode.get("cookies").get(0).asText());
assertEquals("b=2", eventNode.get("cookies").get(1).asText());
assertEquals("c=3", eventNode.get("cookies").get(2).asText());
assertTrue(eventNode.get("headers").has("cookie"));
assertEquals("v", eventNode.get("headers").get("x-other").asText());
}
@Test
void requestWithoutCookiesOmitsCookiesField() {
JsonNode eventNode = eventFor(headersWith(null));
assertFalse(eventNode.has("cookies"));
}
private JsonNode readTree(byte[] json) {
try {
return new ObjectMapper().readTree(json);