|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. |
| 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 | + * http://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 | + |
| 17 | +package io.helidon.webserver.tests; |
| 18 | + |
| 19 | +import io.helidon.http.Header; |
| 20 | +import io.helidon.http.HeaderName; |
| 21 | +import io.helidon.http.HeaderNames; |
| 22 | +import io.helidon.http.HeaderValues; |
| 23 | +import io.helidon.webclient.api.ClientResponseTyped; |
| 24 | +import io.helidon.webclient.http1.Http1Client; |
| 25 | +import io.helidon.webserver.http.ErrorHandler; |
| 26 | +import io.helidon.webserver.http.HttpRouting; |
| 27 | +import io.helidon.webserver.http.ServerRequest; |
| 28 | +import io.helidon.webserver.http.ServerResponse; |
| 29 | +import io.helidon.webserver.testing.junit5.DirectClient; |
| 30 | +import io.helidon.webserver.testing.junit5.RoutingTest; |
| 31 | +import io.helidon.webserver.testing.junit5.SetUpRoute; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import static io.helidon.common.testing.http.junit5.HttpHeaderMatcher.hasHeader; |
| 36 | +import static org.hamcrest.CoreMatchers.is; |
| 37 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | + |
| 39 | +@RoutingTest |
| 40 | +class ErrorHandlingThrowableTest { |
| 41 | + private static final HeaderName CONTROL_HEADER = HeaderNames.create("X-HELIDON-JUNIT"); |
| 42 | + private static final Header THROW = HeaderValues.create(CONTROL_HEADER, "throw"); |
| 43 | + |
| 44 | + private final Http1Client client; |
| 45 | + |
| 46 | + ErrorHandlingThrowableTest(DirectClient client) { |
| 47 | + this.client = client; |
| 48 | + } |
| 49 | + |
| 50 | + @SetUpRoute |
| 51 | + static void routing(HttpRouting.Builder builder) { |
| 52 | + builder.error(SomeError.class, new SomeErrorHandler()) |
| 53 | + .get("/", ErrorHandlingThrowableTest::handler); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testOk() { |
| 58 | + String response = client.get() |
| 59 | + .requestEntity(String.class); |
| 60 | + assertThat(response, is("Done")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testSomeError() { |
| 65 | + ClientResponseTyped<String> response = client.get() |
| 66 | + .header(THROW) |
| 67 | + .request(String.class); |
| 68 | + assertThat(response.headers(), hasHeader(HeaderValues.CONNECTION_CLOSE)); |
| 69 | + assertThat(response.entity(), is("Handled")); |
| 70 | + } |
| 71 | + |
| 72 | + private static void handler(ServerRequest req, ServerResponse res) throws Exception { |
| 73 | + if (req.headers().contains(THROW)) { |
| 74 | + throw new SomeError(); |
| 75 | + } |
| 76 | + res.send("Done"); |
| 77 | + } |
| 78 | + |
| 79 | + private static class SomeErrorHandler implements ErrorHandler<SomeError> { |
| 80 | + @Override |
| 81 | + public void handle(ServerRequest req, ServerResponse res, SomeError throwable) { |
| 82 | + res.header(HeaderValues.CONNECTION_CLOSE); |
| 83 | + res.send("Handled"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static class SomeError extends Error { |
| 88 | + } |
| 89 | +} |
0 commit comments