|
| 1 | +package io.micronaut.function.aws.proxy |
| 2 | + |
| 3 | +import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder |
| 4 | +import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext |
| 5 | +import com.amazonaws.serverless.proxy.model.AwsProxyResponse |
| 6 | +import com.amazonaws.services.lambda.runtime.Context |
| 7 | +import io.micronaut.context.ApplicationContext |
| 8 | +import io.micronaut.context.annotation.Requires |
| 9 | +import io.micronaut.http.HttpHeaders |
| 10 | +import io.micronaut.http.HttpMethod |
| 11 | +import io.micronaut.http.HttpStatus |
| 12 | +import io.micronaut.http.MediaType |
| 13 | +import io.micronaut.http.annotation.Body |
| 14 | +import io.micronaut.http.annotation.Controller |
| 15 | +import io.micronaut.http.annotation.Get |
| 16 | +import io.micronaut.http.annotation.Post |
| 17 | +import io.micronaut.http.annotation.Status |
| 18 | +import spock.lang.AutoCleanup |
| 19 | +import spock.lang.Issue |
| 20 | +import spock.lang.PendingFeature |
| 21 | +import spock.lang.Shared |
| 22 | +import spock.lang.Specification |
| 23 | + |
| 24 | +class ContentTypeSpec extends Specification { |
| 25 | + @Shared |
| 26 | + @AutoCleanup |
| 27 | + MicronautLambdaContainerHandler handler = new MicronautLambdaContainerHandler( |
| 28 | + ApplicationContext.builder().properties([ |
| 29 | + 'spec.name' : 'ContentTypeSpec', |
| 30 | + 'micronaut.security.enabled': false, |
| 31 | + ]) |
| 32 | + ) |
| 33 | + @Shared |
| 34 | + Context lambdaContext = new MockLambdaContext() |
| 35 | + |
| 36 | + void "verify controllers return json by default and mulitvalue headers are populated"() { |
| 37 | + given: |
| 38 | + AwsProxyRequestBuilder builder = new AwsProxyRequestBuilder('/json/bydefault', HttpMethod.GET.toString()) |
| 39 | + builder.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) |
| 40 | + |
| 41 | + when: |
| 42 | + AwsProxyResponse response = handler.proxy(builder.build(), lambdaContext) |
| 43 | + |
| 44 | + then: |
| 45 | + response.statusCode == 200 |
| 46 | + response.body == '{"msg":"Hello world"}' |
| 47 | + |
| 48 | + and: 'works with multivalue headers' |
| 49 | + response.multiValueHeaders |
| 50 | + ["application/json"] == response.getMultiValueHeaders().get("Content-Type") |
| 51 | + } |
| 52 | + |
| 53 | + @Issue("https://github.com/micronaut-projects/micronaut-aws/issues/1330") |
| 54 | + void "verify controllers return json by default and headers are populated"() { |
| 55 | + given: |
| 56 | + AwsProxyRequestBuilder builder = new AwsProxyRequestBuilder('/json/bydefault', HttpMethod.GET.toString()) |
| 57 | + builder.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) |
| 58 | + |
| 59 | + when: |
| 60 | + AwsProxyResponse response = handler.proxy(builder.build(), lambdaContext) |
| 61 | + |
| 62 | + then: |
| 63 | + response.statusCode == 200 |
| 64 | + response.body == '{"msg":"Hello world"}' |
| 65 | + |
| 66 | + response.headers |
| 67 | + "application/json" == response.getHeaders().get("Content-Type") |
| 68 | + } |
| 69 | + |
| 70 | + @Controller('/json') |
| 71 | + @Requires(property = 'spec.name', value = 'ContentTypeSpec') |
| 72 | + static class BodyController { |
| 73 | + |
| 74 | + @Get("/bydefault") |
| 75 | + @Status(HttpStatus.OK) |
| 76 | + Map<String, Object> index() { |
| 77 | + [msg: "Hello world"] |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments