Skip to content

Commit d197acb

Browse files
SentryManrbygrave
andauthored
Fix redirect on 404 error handlers (#249)
Co-authored-by: Rob Bygrave <[email protected]>
1 parent bd4b65d commit d197acb

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

avaje-jex/src/main/java/io/avaje/jex/core/RoutingHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void handle(HttpExchange exchange) {
3636

3737
if (route == null) {
3838
var ctx = new JdkContext(mgr, exchange, uri, Set.of());
39+
ctx.setMode(Mode.EXCHANGE);
3940
mgr.handleException(
4041
ctx,
4142
new NotFoundException(

avaje-jex/src/test/java/io/avaje/jex/core/RedirectTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.avaje.jex.core;
22

3-
import io.avaje.jex.Jex;
4-
import org.junit.jupiter.api.AfterAll;
5-
import org.junit.jupiter.api.Test;
3+
import static org.assertj.core.api.Assertions.assertThat;
64

75
import java.net.http.HttpResponse;
86

9-
import static org.assertj.core.api.Assertions.assertThat;
7+
import org.junit.jupiter.api.AfterAll;
8+
import org.junit.jupiter.api.Test;
9+
10+
import io.avaje.jex.Jex;
11+
import io.avaje.jex.http.NotFoundException;
1012

1113
class RedirectTest {
1214

@@ -26,6 +28,7 @@ static TestPair init() {
2628
.get("/one", ctx -> ctx.text("one"))
2729
.get("/two", ctx -> ctx.text("two"))
2830
.get("/redirect/me", ctx -> ctx.redirect("/one?from=handler"))
31+
.error(NotFoundException.class, (ctx, e) -> ctx.redirect("/one?from=error"))
2932
.get("/other/me", ctx -> ctx.text("never hit")));
3033
return TestPair.create(app);
3134
}
@@ -42,6 +45,13 @@ void redirect_via_handler() {
4245
assertThat(res.statusCode()).isEqualTo(200);
4346
}
4447

48+
@Test
49+
void redirect_via_error_handler() {
50+
HttpResponse<String> res = pair.request().path("redirect/error").GET().asString();
51+
assertThat(res.body()).isEqualTo("one");
52+
assertThat(res.statusCode()).isEqualTo(200);
53+
}
54+
4555
@Test
4656
void redirect_via_beforeHandler() {
4757
HttpResponse<String> res = pair.request().path("other/me").GET().asString();

0 commit comments

Comments
 (0)