|
| 1 | +package io.micronaut.function.aws.proxy |
| 2 | + |
| 3 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent |
| 4 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent |
| 5 | +import io.micronaut.core.convert.ConversionService |
| 6 | +import io.micronaut.http.CaseInsensitiveMutableHttpHeaders |
| 7 | +import io.micronaut.http.HttpHeaders |
| 8 | +import io.micronaut.http.MutableHttpHeaders |
| 9 | +import io.micronaut.test.extensions.spock.annotation.MicronautTest |
| 10 | +import jakarta.inject.Inject |
| 11 | +import spock.lang.Specification |
| 12 | + |
| 13 | +@MicronautTest |
| 14 | +class MapCollapseUtilsSpec extends Specification { |
| 15 | + |
| 16 | + @Inject |
| 17 | + ConversionService conversionService |
| 18 | + |
| 19 | + /** |
| 20 | + * |
| 21 | + * GET /prod/ HTTP/1.1 |
| 22 | + * Key: value1,value2,value3 |
| 23 | + * Foo: Bar |
| 24 | + */ |
| 25 | + void "payload v2 example"() { |
| 26 | + given: |
| 27 | + APIGatewayV2HTTPEvent event = new APIGatewayV2HTTPEvent() |
| 28 | + Map<String, String> headers = new HashMap<>(); |
| 29 | + headers.put(HttpHeaders.DATE, "Wed, 21 Oct 2015 07:28:00 GMT") |
| 30 | + headers.put("foo", "Bar") |
| 31 | + headers.put("key", "value1,value2,value3") |
| 32 | + headers.put(HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)") |
| 33 | + event.setHeaders(headers) |
| 34 | + |
| 35 | + when: |
| 36 | + MutableHttpHeaders httpHeaders = new CaseInsensitiveMutableHttpHeaders(MapCollapseUtils.collapse(Collections.emptyMap(), event.getHeaders()), conversionService); |
| 37 | + |
| 38 | + then: |
| 39 | + noExceptionThrown() |
| 40 | + |
| 41 | + and: |
| 42 | + "Bar" == httpHeaders.get("Foo") |
| 43 | + ["value1", "value2", "value3"] == httpHeaders.getAll("Key") |
| 44 | + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" == httpHeaders.get(HttpHeaders.USER_AGENT) |
| 45 | + "Wed, 21 Oct 2015 07:28:00 GMT" == httpHeaders.get(HttpHeaders.DATE) |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * |
| 50 | + * GET /prod/ HTTP/1.1 |
| 51 | + * Key: value1,value2,value3 |
| 52 | + * Foo: Bar |
| 53 | + */ |
| 54 | + void "payload v1 example"() { |
| 55 | + given: |
| 56 | + APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent() |
| 57 | + Map<String, String> headers = new HashMap<>() |
| 58 | + headers.put("Foo", "Bar") |
| 59 | + headers.put("Key", "value1,value2,value3") |
| 60 | + request.setHeaders(headers) |
| 61 | + Map<String, List<String>> multiValueHeaders = new HashMap<>() |
| 62 | + multiValueHeaders.put("Key", Arrays.asList("value1", "value2", "value3")) |
| 63 | + multiValueHeaders.put("Foo", Collections.singletonList("Bar")) |
| 64 | + request.setMultiValueHeaders(multiValueHeaders) |
| 65 | + |
| 66 | + when: |
| 67 | + MutableHttpHeaders httpHeaders = new CaseInsensitiveMutableHttpHeaders(MapCollapseUtils.collapse(request.getMultiValueHeaders(), request.getHeaders()), conversionService); |
| 68 | + |
| 69 | + then: |
| 70 | + noExceptionThrown() |
| 71 | + |
| 72 | + and: |
| 73 | + "Bar" == httpHeaders.get("Foo") |
| 74 | + ["value1", "value2", "value3"] == httpHeaders.getAll("Key") |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments