Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 5ab1256

Browse files
committed
Add redirect in filter is possible test
1 parent 7c3316f commit 5ab1256

2 files changed

Lines changed: 65 additions & 26 deletions

File tree

jooby-core/src/main/java/jooby/internal/RouteHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void handle(final HttpServletRequest request, final HttpServletResponse r
112112

113113
final String path = verb + requestURI;
114114

115-
log.debug("handling: {}", path);
115+
log.info("handling: {}", path);
116116

117117
log.debug(" content-type: {}", type);
118118

@@ -170,7 +170,7 @@ public void handle(final HttpServletRequest request, final HttpServletResponse r
170170
}
171171
} finally {
172172
long end = System.currentTimeMillis();
173-
log.debug(" status -> {} in {}ms", response.getStatus(), end - start);
173+
log.info(" status -> {} in {}ms", response.getStatus(), end - start);
174174
}
175175
}
176176

jooby-tests/src/test/java/jooby/FilterFeature.java

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNull;
55

6+
import org.apache.http.HttpRequest;
67
import org.apache.http.HttpResponse;
8+
import org.apache.http.ProtocolException;
9+
import org.apache.http.client.RedirectStrategy;
10+
import org.apache.http.client.fluent.Executor;
711
import org.apache.http.client.fluent.Request;
12+
import org.apache.http.client.methods.HttpUriRequest;
813
import org.apache.http.client.utils.URIBuilder;
14+
import org.apache.http.impl.client.HttpClientBuilder;
15+
import org.apache.http.protocol.HttpContext;
916
import org.apache.http.util.EntityUtils;
1017
import org.junit.Test;
1118

@@ -17,46 +24,55 @@ public interface HttpResponseValidator {
1724

1825
{
1926

20-
use((req, resp, chain) -> {
21-
chain.next(req, resp);
27+
use((req, res, chain) -> {
28+
chain.next(req, res);
2229
});
2330

24-
get("/no-next", (req, resp, chain) -> {
31+
get("/no-next", (req, res, chain) -> {
2532
});
2633

27-
get("/no-next", (req, resp, chain) -> {
34+
get("/no-next", (req, res, chain) -> {
2835
throw new IllegalStateException("Should NOT execute ever");
2936
});
3037

31-
get("/before", (req, resp, chain) -> {
32-
resp.header("before", "before");
33-
chain.next(req, resp);
38+
get("/before", (req, res, chain) -> {
39+
res.header("before", "before");
40+
chain.next(req, res);
3441
});
3542

36-
get("/before", (req, resp) -> {
37-
resp.send(resp.header("before").stringValue());
43+
get("/before", (req, res) -> {
44+
res.send(res.header("before").stringValue());
3845
});
3946

40-
get("/after", (req, resp, chain) -> {
41-
chain.next(req, resp);
42-
resp.header("after", "after");
47+
get("/after", (req, res, chain) -> {
48+
chain.next(req, res);
49+
res.header("after", "after");
4350
});
4451

45-
get("/after", (req, resp) -> {
46-
resp.send(resp.header("after").toOptional(String.class).orElse("after-missing"));
52+
get("/after", (req, res) -> {
53+
res.send(res.header("after").toOptional(String.class).orElse("after-missing"));
4754
});
4855

49-
get("/commit", (req, resp, chain) -> {
50-
resp.send("commit");
56+
get("/commit", (req, res, chain) -> {
57+
res.send("commit");
5158
});
5259

53-
get("/commitx2", (req, resp, chain) -> {
54-
resp.send("commit1");
55-
chain.next(req, resp);
60+
get("/commitx2", (req, res, chain) -> {
61+
res.send("commit1");
62+
chain.next(req, res);
5663
});
5764

58-
get("/commitx2", (req, resp) -> {
59-
resp.send("ignored");
65+
get("/commitx2", (req, res) -> {
66+
res.send("ignored");
67+
});
68+
69+
get("/redirect", (req, res, chain) -> {
70+
res.redirect("/commit");
71+
chain.next(req, res);
72+
});
73+
74+
get("/redirect", (req, res) -> {
75+
res.send("ignored");
6076
});
6177

6278
}
@@ -98,6 +114,13 @@ public void commitIsPossibleFromFilter() throws Exception {
98114
}));
99115
}
100116

117+
@Test
118+
public void redirectIsPossibleFromFilter() throws Exception {
119+
assertEquals("", execute(GET(uri("/redirect")), (response) -> {
120+
assertEquals(302, response.getStatusLine().getStatusCode());
121+
}));
122+
}
123+
101124
@Test
102125
public void secondCommitIsIgnored() throws Exception {
103126
assertEquals("commit1", execute(GET(uri("/commitx2")), (response) -> {
@@ -111,9 +134,25 @@ private static Request GET(final URIBuilder uri) throws Exception {
111134

112135
private static String execute(final Request request, final HttpResponseValidator validator)
113136
throws Exception {
114-
HttpResponse resp = request.execute().returnResponse();
115-
validator.validate(resp);
116-
return EntityUtils.toString(resp.getEntity());
137+
Executor executor = Executor.newInstance(HttpClientBuilder.create()
138+
.setRedirectStrategy(new RedirectStrategy() {
139+
140+
@Override
141+
public boolean isRedirected(final HttpRequest request, final HttpResponse response,
142+
final HttpContext context) throws ProtocolException {
143+
return false;
144+
}
145+
146+
@Override
147+
public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response,
148+
final HttpContext context) throws ProtocolException {
149+
return null;
150+
}
151+
}).build());
152+
153+
HttpResponse response = executor.execute(request).returnResponse();
154+
validator.validate(response);
155+
return EntityUtils.toString(response.getEntity());
117156
}
118157

119158
}

0 commit comments

Comments
 (0)