|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micronaut.http.server.tck.tests.filter; |
| 17 | + |
| 18 | +import io.micronaut.context.annotation.Requires; |
| 19 | +import io.micronaut.http.HttpRequest; |
| 20 | +import io.micronaut.http.HttpResponse; |
| 21 | +import io.micronaut.http.HttpStatus; |
| 22 | +import io.micronaut.http.MediaType; |
| 23 | +import io.micronaut.http.MutableHttpRequest; |
| 24 | +import io.micronaut.http.MutableHttpResponse; |
| 25 | +import io.micronaut.http.annotation.Controller; |
| 26 | +import io.micronaut.http.annotation.Get; |
| 27 | +import io.micronaut.http.annotation.Produces; |
| 28 | +import io.micronaut.http.annotation.RequestFilter; |
| 29 | +import io.micronaut.http.annotation.ResponseFilter; |
| 30 | +import io.micronaut.http.annotation.ServerFilter; |
| 31 | +import io.micronaut.http.server.annotation.PreMatching; |
| 32 | +import io.micronaut.http.server.exceptions.ExceptionHandler; |
| 33 | +import io.micronaut.http.tck.AssertionUtils; |
| 34 | +import io.micronaut.http.tck.HttpResponseAssertion; |
| 35 | +import io.micronaut.http.tck.TestScenario; |
| 36 | +import jakarta.inject.Singleton; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | + |
| 41 | +import static io.micronaut.http.annotation.Filter.MATCH_ALL_PATTERN; |
| 42 | + |
| 43 | +@SuppressWarnings({ |
| 44 | + "java:S5960", // We're allowed assertions, as these are used in tests only |
| 45 | + "checkstyle:MissingJavadocType", |
| 46 | + "checkstyle:DesignForExtension" |
| 47 | +}) |
| 48 | +public class ResponseFilterOnPreMatchExceptionHandlerTest { |
| 49 | + private static final String SPEC_NAME = "ResponseFilterOnPreMatchExceptionHandlerTest"; |
| 50 | + |
| 51 | + @Test |
| 52 | + public void exceptionHandlerTest() throws IOException { |
| 53 | + TestScenario.builder() |
| 54 | + .specName(SPEC_NAME) |
| 55 | + .request(HttpRequest.GET("/foo")) |
| 56 | + .assertion((server, request) -> AssertionUtils.assertDoesNotThrow(server, request, HttpResponseAssertion.builder() |
| 57 | + .status(HttpStatus.OK) |
| 58 | + .body("EXCEPTION MAPPER RESPONSE FILTER") |
| 59 | + .build())) |
| 60 | + .run(); |
| 61 | + } |
| 62 | + |
| 63 | + static class FooException extends RuntimeException { |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @ServerFilter(MATCH_ALL_PATTERN) |
| 68 | + @Requires(property = "spec.name", value = SPEC_NAME) |
| 69 | + static class ErrorThrowingFilter { |
| 70 | + @PreMatching |
| 71 | + @RequestFilter |
| 72 | + public void onPreMatching(MutableHttpRequest<?> request) { |
| 73 | + throw new FooException(); |
| 74 | + } |
| 75 | + |
| 76 | + @ResponseFilter |
| 77 | + public void onResponse(MutableHttpResponse<?> response) { |
| 78 | + response.body(response.getBody(String.class).get() + " RESPONSE FILTER"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Requires(property = "spec.name", value = SPEC_NAME) |
| 83 | + @Controller("/foo") |
| 84 | + static class FooController { |
| 85 | + @Produces(MediaType.TEXT_PLAIN) |
| 86 | + @Get |
| 87 | + String index() { |
| 88 | + return "Hello World"; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Requires(property = "spec.name", value = SPEC_NAME) |
| 93 | + @Singleton |
| 94 | + static class FooExceptionHandler implements ExceptionHandler<FooException, HttpResponse<?>> { |
| 95 | + |
| 96 | + @Override |
| 97 | + public HttpResponse<?> handle(HttpRequest request, FooException exception) { |
| 98 | + return HttpResponse.ok("EXCEPTION MAPPER"); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments