Skip to content

Commit 723d7cd

Browse files
committed
after route: dont allow to change the response value
1 parent 3563616 commit 723d7cd

8 files changed

Lines changed: 71 additions & 38 deletions

File tree

examples/src/main/java/examples/DispatchApp.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public class DispatchApp extends Jooby {
2121
});
2222

2323
after((ctx, value) -> {
24-
Number n = (Number) value;
25-
return n.intValue() * 2;
2624
});
2725

2826
get("/", ctx -> ctx.query("n").intValue(2));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package examples;
2+
3+
import io.jooby.Jooby;
4+
import io.jooby.Route;
5+
import io.reactivex.Single;
6+
7+
import javax.annotation.Nonnull;
8+
9+
public class Poc extends Jooby {
10+
11+
{
12+
13+
decorator(next -> ctx -> {
14+
return next.apply(ctx);
15+
});
16+
17+
decorator(next -> ctx -> {
18+
return next.apply(ctx);
19+
});
20+
21+
decorator(next -> ctx -> {
22+
Single<String> single = Single.just("v");
23+
single.doOnSuccess(value -> {
24+
next.apply(ctx);
25+
});
26+
return next.apply(ctx);
27+
});
28+
29+
}
30+
}

jooby/src/main/java/io/jooby/Route.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ public interface After {
118118
* @return A new decorator.
119119
*/
120120
@Nonnull default After then(@Nonnull After next) {
121-
return (ctx, result) -> apply(ctx, next.apply(ctx, result));
121+
return (ctx, result) -> {
122+
next.apply(ctx, result);
123+
apply(ctx, result);
124+
};
122125
}
123126

124127
/**
@@ -129,7 +132,7 @@ public interface After {
129132
* @return Response to send.
130133
* @throws Exception If something goes wrong.
131134
*/
132-
@Nonnull Object apply(@Nonnull Context ctx, Object result) throws Exception;
135+
@Nonnull void apply(@Nonnull Context ctx, Object result) throws Exception;
133136
}
134137

135138
/**
@@ -167,14 +170,19 @@ public interface Handler extends Serializable {
167170
* @return A new handler.
168171
*/
169172
@Nonnull default Handler then(@Nonnull After next) {
170-
return ctx -> next.apply(ctx, apply(ctx));
173+
return ctx -> {
174+
Object result = apply(ctx);
175+
next.apply(ctx, result);
176+
return result;
177+
};
171178
}
172179
}
173180

174181
/**
175182
* Handler for {@link StatusCode#NOT_FOUND} responses.
176183
*/
177-
public static final Handler NOT_FOUND = ctx -> ctx.sendError(new StatusCodeException(StatusCode.NOT_FOUND));
184+
public static final Handler NOT_FOUND = ctx -> ctx
185+
.sendError(new StatusCodeException(StatusCode.NOT_FOUND));
178186

179187
/**
180188
* Handler for {@link StatusCode#METHOD_NOT_ALLOWED} responses.

modules/jooby-test/src/main/java/io/jooby/MockContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ public MockContext setResponseType(@Nonnull MediaType contentType, @Nullable Cha
310310
* @return Mock response.
311311
*/
312312
@Nonnull public MockResponse getResponse() {
313+
response.setHeaders(responseHeaders);
313314
return response;
314315
}
315316

@@ -409,7 +410,7 @@ public MockContext setResponseType(@Nonnull MediaType contentType, @Nullable Cha
409410
}
410411

411412
@Nonnull @Override public MockContext setResponseCookie(@Nonnull Cookie cookie) {
412-
String setCookie = response.getHeaders().get("Set-Cookie");
413+
String setCookie = (String) response.getHeaders().get("Set-Cookie");
413414
if (setCookie == null) {
414415
setCookie = cookie.toCookieString();
415416
} else {

modules/jooby-test/src/main/java/io/jooby/MockResponse.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class MockResponse implements MockValue {
4444

4545
private StatusCode statusCode = StatusCode.OK;
4646

47-
private Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
47+
private Map<String, Object> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
4848

4949
private MediaType contentType;
5050

@@ -55,7 +55,7 @@ public class MockResponse implements MockValue {
5555
*
5656
* @return Response headers.
5757
*/
58-
public @Nonnull Map<String, String> getHeaders() {
58+
public @Nonnull Map<String, Object> getHeaders() {
5959
return headers == null ? Collections.emptyMap() : Collections.unmodifiableMap(headers);
6060
}
6161

@@ -65,7 +65,7 @@ public class MockResponse implements MockValue {
6565
* @param headers Response headers.
6666
* @return This response.
6767
*/
68-
public @Nonnull MockResponse setHeaders(@Nonnull Map<String, String> headers) {
68+
public @Nonnull MockResponse setHeaders(@Nonnull Map<String, Object> headers) {
6969
headers.forEach(this::setHeader);
7070
return this;
7171
}
@@ -88,6 +88,17 @@ public class MockResponse implements MockValue {
8888
return this;
8989
}
9090

91+
/**
92+
* Set response header.
93+
*
94+
* @param name Header name.
95+
* @param value Header value.
96+
* @return This response.
97+
*/
98+
public @Nonnull MockResponse setHeader(@Nonnull String name, @Nonnull Object value) {
99+
return setHeader(name, value.toString());
100+
}
101+
91102
/**
92103
* Response content type.
93104
*

modules/jooby-test/src/test/java/io/jooby/UnitTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ public void unitTests() {
6060
public void pipeline() {
6161
Jooby app = new Jooby();
6262

63-
app.before(ctx -> ctx.attribute("prefix", "<"));
64-
app.after((ctx, result) -> result + ">");
65-
app.get("/", ctx -> ctx.attribute("prefix") + "OK");
63+
app.before(ctx -> ctx.setResponseHeader("before", "<"));
64+
app.after((ctx, result) -> ctx.setResponseHeader("after", ">"));
65+
app.get("/", ctx -> "OK");
6666

6767
MockRouter router = new MockRouter(app)
6868
.setFullExecution(true);
6969

70-
assertEquals("<OK>", router.get("/").value());
70+
router.get("/", rsp -> {
71+
assertEquals("OK", rsp.value());
72+
assertEquals("<", rsp.getHeaders().get("before"));
73+
assertEquals(">", rsp.getHeaders().get("after"));
74+
});
7175
}
7276

7377
@Test

tests/src/test/java/io/jooby/FeaturedTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ public void filter() {
635635
app.after((ctx, value) -> {
636636
StringBuilder buff = (StringBuilder) value;
637637
buff.append("after1:" + ctx.isInIoThread()).append(";");
638-
return buff;
639638
});
640639

641640
app.dispatch(() -> {
@@ -646,12 +645,14 @@ public void filter() {
646645

647646
app.after((ctx, value) -> {
648647
StringBuilder buff = ctx.attribute("buff");
649-
buff.append(value).append(";");
650648
buff.append("after2:" + ctx.isInIoThread()).append(";");
651-
return buff;
652649
});
653650

654-
app.get("/", ctx -> "result:" + ctx.isInIoThread());
651+
app.get("/", ctx -> {
652+
StringBuilder buff = ctx.attribute("buff");
653+
buff.append("result:").append(ctx.isInIoThread()).append(";");
654+
return buff;
655+
});
655656
});
656657

657658
}).mode(ExecutionMode.EVENT_LOOP).ready(client -> {

tests/src/test/java/io/jooby/ResponseHandlerTest.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)