|
| 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.Shared |
| 20 | +import spock.lang.Specification |
| 21 | + |
| 22 | +class ContentTypeSpec extends Specification { |
| 23 | + @Shared |
| 24 | + @AutoCleanup |
| 25 | + MicronautLambdaContainerHandler handler = new MicronautLambdaContainerHandler( |
| 26 | + ApplicationContext.builder().properties([ |
| 27 | + 'spec.name' : 'ContentTypeSpec', |
| 28 | + 'micronaut.security.enabled': false, |
| 29 | + ]) |
| 30 | + ) |
| 31 | + @Shared |
| 32 | + Context lambdaContext = new MockLambdaContext() |
| 33 | + |
| 34 | + void "verify controllers return json by default"() { |
| 35 | + given: |
| 36 | + AwsProxyRequestBuilder builder = new AwsProxyRequestBuilder('/json/bydefault', HttpMethod.GET.toString()) |
| 37 | + builder.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) |
| 38 | + |
| 39 | + when: |
| 40 | + AwsProxyResponse response = handler.proxy(builder.build(), lambdaContext) |
| 41 | + |
| 42 | + then: |
| 43 | + response.statusCode == 200 |
| 44 | + response.body == '{"msg":"Hello world"}' |
| 45 | + response.headers |
| 46 | + "application/json" == response.getHeaders().get("Content-Type") |
| 47 | + } |
| 48 | + |
| 49 | + @Controller('/json') |
| 50 | + @Requires(property = 'spec.name', value = 'ContentTypeSpec') |
| 51 | + static class BodyController { |
| 52 | + |
| 53 | + @Get("/bydefault") |
| 54 | + @Status(HttpStatus.OK) |
| 55 | + Map<String, Object> index() { |
| 56 | + [msg: "Hello world"] |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments