Skip to content

Commit e8787f2

Browse files
authored
Merge pull request #3788 from kliushnichenko/fix/i3787-problem-details-in-mock-router
Fix/i3787 problem details in mock router
2 parents a866a9f + f0cfa18 commit e8787f2

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.jooby.Router;
1919
import io.jooby.SneakyThrows;
2020
import io.jooby.WebSocket;
21+
import io.jooby.problem.ProblemDetailsHandler;
2122

2223
/**
2324
* Utility class that allows us to execute routes using a {@link MockContext}.
@@ -74,6 +75,9 @@ public Object value() {
7475
* @param application Source application.
7576
*/
7677
public MockRouter(@NonNull Jooby application) {
78+
if (application.problemDetailsIsEnabled()) {
79+
application.error(ProblemDetailsHandler.from(application.getConfig()));
80+
}
7781
this.supplier = () -> application;
7882
}
7983

@@ -472,6 +476,8 @@ public void tryError(Throwable cause, Consumer<MockResponse> consumer) {
472476
*/
473477
public void tryError(Throwable cause, Context ctx) {
474478
var app = supplier.get();
479+
MockContext findContext = ctx instanceof MockContext ? (MockContext) ctx : newContext();
480+
findContext.setRouter(app);
475481
var handler = app.getErrorHandler();
476482
handler.apply(ctx, cause, app.errorCode(cause));
477483
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.jooby.i3787;
2+
3+
import com.typesafe.config.Config;
4+
import com.typesafe.config.ConfigFactory;
5+
import io.jooby.Jooby;
6+
import io.jooby.problem.HttpProblem;
7+
8+
import java.util.Map;
9+
10+
class Application extends Jooby {
11+
{
12+
Config problemDetailsConfig = ConfigFactory.parseMap(
13+
Map.of("problem.details.enabled", true)
14+
);
15+
getEnvironment().setConfig(problemDetailsConfig.withFallback(getConfig()));
16+
17+
get("/throw", ctx -> {
18+
throw new CustomException();
19+
});
20+
21+
error(CustomException.class, (ctx, throwable, statusCode) -> {
22+
var problem = HttpProblem.badRequest("A Client Error — Obviously");
23+
ctx.getRouter().getErrorHandler().apply(ctx, problem, statusCode);
24+
});
25+
}
26+
27+
public static void main(String[] args) {
28+
runApp(args, Application::new);
29+
}
30+
31+
static class CustomException extends RuntimeException {
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.jooby.i3787;
2+
3+
import io.jooby.StatusCode;
4+
import io.jooby.test.MockContext;
5+
import io.jooby.test.MockRouter;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class Issue3787 {
11+
12+
private final MockRouter router = new MockRouter(new Application()).setFullExecution(true);
13+
14+
@Test
15+
void issue3787() {
16+
var ctx = new MockContext();
17+
router.tryError(new Application.CustomException(), ctx);
18+
assertEquals(StatusCode.BAD_REQUEST, ctx.getResponseCode());
19+
}
20+
}

0 commit comments

Comments
 (0)