-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: status httpStatus, response handler (#1392)
* test: status httpStatus, response handler see: #1387 * test MicronautLambdaHandler
- Loading branch information
Showing
2 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...y/src/test/groovy/io/micronaut/function/aws/proxy/MicronautLambdaHandlerStatusSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.micronaut.function.aws.proxy | ||
|
||
|
||
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext | ||
import com.amazonaws.serverless.proxy.model.AwsProxyRequest | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse | ||
import com.amazonaws.services.lambda.runtime.Context | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
import io.micronaut.http.server.exceptions.ExceptionHandler | ||
import io.micronaut.http.server.exceptions.response.ErrorContext | ||
import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor | ||
import jakarta.inject.Singleton | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Issue | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class MicronautLambdaHandlerStatusSpec extends Specification { | ||
|
||
@Shared | ||
@AutoCleanup | ||
MicronautLambdaHandler handler = new MicronautLambdaHandler( | ||
ApplicationContext.builder().properties([ | ||
'micronaut.security.enabled': false, | ||
'spec.name': 'MicronautLambdaHandlerStatusSpec' | ||
]) | ||
) | ||
|
||
@Shared | ||
Context context = new MockLambdaContext() | ||
|
||
@Issue("https://github.com/micronaut-projects/micronaut-aws/issues/1387") | ||
void "test controller returning HttpStatus"(String path) { | ||
given: | ||
AwsProxyRequest input = new AwsProxyRequest(); | ||
input.setPath(path) | ||
|
||
when: | ||
AwsProxyResponse response = handler.handleRequest(input, context) | ||
|
||
then: | ||
response.statusCode == 418 | ||
|
||
where: | ||
path << ['/http-status', '/http-response-status', '/http-exception'] | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'MicronautLambdaHandlerStatusSpec') | ||
@Controller('/http-status') | ||
static class HttpStatusController { | ||
|
||
@Get | ||
HttpStatus index() { | ||
HttpStatus.I_AM_A_TEAPOT | ||
} | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'MicronautLambdaHandlerStatusSpec') | ||
@Controller('/http-response-status') | ||
static class HttpResponseStatusController { | ||
|
||
@Get | ||
HttpResponse<?> index() { | ||
HttpResponse.status(HttpStatus.I_AM_A_TEAPOT) | ||
} | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'MicronautLambdaHandlerStatusSpec') | ||
@Controller('/http-exception') | ||
static class HttpResponseErrorController { | ||
|
||
@Get | ||
HttpResponse<?> index() { | ||
throw new TeapotException() | ||
} | ||
} | ||
|
||
static class TeapotException extends RuntimeException { | ||
} | ||
|
||
@Produces | ||
@Singleton | ||
static class TeapotExceptionHandler implements ExceptionHandler<TeapotException, HttpResponse<?>> { | ||
private final ErrorResponseProcessor<?> errorResponseProcessor | ||
|
||
TeapotExceptionHandler(ErrorResponseProcessor<?> errorResponseProcessor) { | ||
this.errorResponseProcessor = errorResponseProcessor | ||
} | ||
|
||
@Override | ||
HttpResponse<?> handle(HttpRequest request, TeapotException e) { | ||
errorResponseProcessor.processResponse(ErrorContext.builder(request) | ||
.cause(e) | ||
.build(), HttpResponse.status(HttpStatus.I_AM_A_TEAPOT)) | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
function-aws-api-proxy/src/test/groovy/io/micronaut/function/aws/proxy/StatusSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package io.micronaut.function.aws.proxy | ||
|
||
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder | ||
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext | ||
import com.amazonaws.services.lambda.runtime.Context | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpMethod | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
import io.micronaut.http.server.exceptions.ExceptionHandler | ||
import io.micronaut.http.server.exceptions.response.ErrorContext | ||
import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Issue | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
import jakarta.inject.Singleton | ||
|
||
class StatusSpec extends Specification { | ||
|
||
@Shared @AutoCleanup MicronautLambdaContainerHandler handler = new MicronautLambdaContainerHandler( | ||
ApplicationContext.builder().properties([ | ||
'micronaut.security.enabled': false, | ||
'spec.name': 'StatusSpec' | ||
]) | ||
) | ||
@Shared Context lambdaContext = new MockLambdaContext() | ||
|
||
@Issue("https://github.com/micronaut-projects/micronaut-aws/issues/1387") | ||
void "test controller returning HttpStatus"(String path) { | ||
given: | ||
AwsProxyRequestBuilder builder = new AwsProxyRequestBuilder(path, HttpMethod.GET.toString()) | ||
|
||
when: | ||
def response = handler.proxy(builder.build(), lambdaContext) | ||
|
||
then: | ||
response.statusCode == 418 | ||
|
||
where: | ||
path << ['/http-status', '/http-response-status', '/http-exception'] | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'StatusSpec') | ||
@Controller('/http-status') | ||
static class HttpStatusController { | ||
|
||
@Get | ||
HttpStatus index() { | ||
HttpStatus.I_AM_A_TEAPOT | ||
} | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'StatusSpec') | ||
@Controller('/http-response-status') | ||
static class HttpResponseStatusController { | ||
|
||
@Get | ||
HttpResponse<?> index() { | ||
HttpResponse.status(HttpStatus.I_AM_A_TEAPOT) | ||
} | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'StatusSpec') | ||
@Controller('/http-exception') | ||
static class HttpResponseErrorController { | ||
|
||
@Get | ||
HttpResponse<?> index() { | ||
throw new TeapotException() | ||
} | ||
} | ||
|
||
static class TeapotException extends RuntimeException { | ||
} | ||
|
||
@Produces | ||
@Singleton | ||
static class TeapotExceptionHandler implements ExceptionHandler<TeapotException, HttpResponse<?>> { | ||
private final ErrorResponseProcessor<?> errorResponseProcessor | ||
|
||
TeapotExceptionHandler(ErrorResponseProcessor<?> errorResponseProcessor) { | ||
this.errorResponseProcessor = errorResponseProcessor | ||
} | ||
|
||
@Override | ||
HttpResponse<?> handle(HttpRequest request, TeapotException e) { | ||
errorResponseProcessor.processResponse(ErrorContext.builder(request) | ||
.cause(e) | ||
.build(), HttpResponse.status(HttpStatus.I_AM_A_TEAPOT)) | ||
} | ||
} | ||
|
||
} |