mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(apigateway): use last duplicate header value (#1806)
This commit is contained in:
1 parent
733c72d02a
commit
6a214e84d6
2 files changed
+48
-10
No files matched your search
+8
-3
@@ -721,14 +721,19 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
}
|
||||
|
||||
private void putSingleValueHeaders(ObjectNode event, HttpHeaders headers) {
|
||||
// Package-private for unit testing (see ApiGatewayExecuteControllerTest).
|
||||
void putSingleValueHeaders(ObjectNode event, HttpHeaders headers) {
|
||||
ObjectNode headersNode = event.putObject("headers");
|
||||
headers.getRequestHeaders().forEach((name, values) -> {
|
||||
if (!values.isEmpty()) headersNode.put(name, values.get(0));
|
||||
// AWS collapses duplicate request headers to the LAST value in the single-value `headers`
|
||||
// map (multiValueHeaders keeps every value). Taking the first value diverged from AWS.
|
||||
if (!values.isEmpty()) {
|
||||
headersNode.put(name, values.get(values.size() - 1));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void putMultiValueHeaders(ObjectNode event, HttpHeaders headers) {
|
||||
void putMultiValueHeaders(ObjectNode event, HttpHeaders headers) {
|
||||
ObjectNode mvHeaders = event.putObject("multiValueHeaders");
|
||||
headers.getRequestHeaders().forEach((name, values) -> {
|
||||
ArrayNode arr = mvHeaders.putArray(name);
|
||||
|
||||
+40
-7
@@ -1,21 +1,33 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Verifies {@link ApiGatewayExecuteController#extractV2PathParams(String, String)}
|
||||
* captures path parameters correctly for both greedy ({proxy+}) and non-greedy
|
||||
* ({proxy}) route templates, and that repeated invocations against the same
|
||||
* route key reuse the cached compiled Pattern (correctness check; the cache
|
||||
* itself is an internal implementation detail).
|
||||
*/
|
||||
/** Unit coverage for API Gateway request-event construction and HTTP API v2 route matching. */
|
||||
class ApiGatewayExecuteControllerTest {
|
||||
|
||||
private static ApiGatewayExecuteController controller(ObjectMapper objectMapper) {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
return new ApiGatewayExecuteController(
|
||||
null, null, null,
|
||||
regionResolver, objectMapper, null,
|
||||
null, null, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void capturesGreedyProxyMultiSegment() {
|
||||
Map<String, String> p = ApiGatewayExecuteController.extractV2PathParams(
|
||||
@@ -76,4 +88,25 @@ class ApiGatewayExecuteControllerTest {
|
||||
assertEquals("spei/" + i, p.get("proxy"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateRequestHeaderUsesLastSingleValueAndPreservesAllMultiValues() {
|
||||
MultivaluedMap<String, String> requestHeaders = new MultivaluedHashMap<>();
|
||||
requestHeaders.add("X-Dup", "first");
|
||||
requestHeaders.add("X-Dup", "second");
|
||||
requestHeaders.add("X-Dup", "third");
|
||||
HttpHeaders headers = mock(HttpHeaders.class);
|
||||
when(headers.getRequestHeaders()).thenReturn(requestHeaders);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
ObjectNode event = objectMapper.createObjectNode();
|
||||
ApiGatewayExecuteController controller = controller(objectMapper);
|
||||
controller.putSingleValueHeaders(event, headers);
|
||||
controller.putMultiValueHeaders(event, headers);
|
||||
|
||||
assertEquals("third", event.path("headers").path("X-Dup").asText());
|
||||
assertEquals(
|
||||
objectMapper.valueToTree(List.of("first", "second", "third")),
|
||||
event.path("multiValueHeaders").path("X-Dup"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user