Skip to content

Commit 8975701

Browse files
authored
handle no response (#78)
1 parent e121b7e commit 8975701

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

avaje-jex/src/main/java/io/avaje/jex/jdk/JdkContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class JdkContext implements Context, SpiContext {
4848
private Map<String, List<String>> formParams;
4949
private Map<String, List<String>> queryParams;
5050
private Map<String, String> cookieMap;
51-
private int statusCode;
51+
private int statusCode = 200;
5252
private String characterEncoding;
5353

5454
JdkContext(

avaje-jex/src/main/java/io/avaje/jex/jdk/RoutingFilter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.avaje.jex.jdk;
22

3+
import java.io.IOException;
34
import java.util.Map;
45
import java.util.Set;
56

@@ -42,6 +43,7 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
4243
processNoRoute(ctx, uri, routeType);
4344
exchange.setAttribute("JdkContext", ctx);
4445
chain.doFilter(exchange);
46+
handleNoResponse(exchange);
4547
} catch (Exception e) {
4648
handleException(ctx, e);
4749
} finally {
@@ -59,6 +61,7 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
5961
ExchangeHandler handlerConsumer = route::handle;
6062
exchange.setAttribute("SpiRoutes.Entry.Handler", handlerConsumer);
6163
chain.doFilter(exchange);
64+
handleNoResponse(exchange);
6265
} catch (Exception e) {
6366
handleException(ctx, e);
6467
}
@@ -69,6 +72,13 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
6972
}
7073
}
7174

75+
private void handleNoResponse(HttpExchange exchange) throws IOException {
76+
77+
if (exchange.getResponseCode() == -1) {
78+
exchange.sendResponseHeaders(204, -1);
79+
}
80+
}
81+
7282
private void handleException(SpiContext ctx, Exception e) {
7383
mgr.handleException(ctx, e);
7484
}

avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static TestPair init() {
2323
routing ->
2424
routing
2525
.get("/", ctx -> ctx.text("roo"))
26+
.get("/noResponse", ctx -> {})
2627
.get("/one", ctx -> ctx.text("one"))
2728
.get("/two", ctx -> ctx.text("two"))
2829
.get("/two/{id}", ctx -> ctx.text("two-id"))
@@ -77,6 +78,15 @@ void get() {
7778
assertNoBeforeAfterTwo(res);
7879
}
7980

81+
@Test
82+
void getNoResponse() {
83+
clearAfter();
84+
HttpResponse<String> res = pair.request().path("noResponse").GET().asString();
85+
assertThat(res.statusCode()).isEqualTo(204);
86+
assertHasBeforeAfterAll(res);
87+
assertNoBeforeAfterTwo(res);
88+
}
89+
8090
@Test
8191
void get_two_expect_extraFilters() {
8292
clearAfter();

0 commit comments

Comments
 (0)