Skip to content

Commit bb4b3ba

Browse files
authored
[context] Expose HandlerType in Context (#1264) (#1265)
1 parent c49a659 commit bb4b3ba

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

javalin/src/main/java/io/javalin/http/Context.kt

+3
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,7 @@ open class Context(@JvmField val req: HttpServletRequest, @JvmField val res: Htt
500500

501501
/** Gets a list of all the [splat] values. */
502502
fun splats(): List<String> = Collections.unmodifiableList(splatList)
503+
504+
/** Gets the handler type of the current handler */
505+
fun handlerType() : HandlerType = handlerType
503506
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Javalin - https://javalin.io
3+
* Copyright 2021 David Åse
4+
* Licensed under Apache 2.0: https://github.com/tipsy/javalin/blob/master/LICENSE
5+
*/
6+
package io.javalin;
7+
8+
import io.javalin.http.HandlerType;
9+
import io.javalin.testing.TestUtil;
10+
import org.assertj.core.api.Assertions;
11+
import org.junit.Test;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class TestContextHandlerType {
17+
18+
@Test
19+
public void testHandlerTypeCanBeAccessedInContext() {
20+
TestUtil.test(Javalin.create(), (app, http) -> {
21+
List<HandlerType> handlerTypes = new ArrayList<>();
22+
app.before(ctx -> handlerTypes.add(ctx.handlerType()));
23+
app.after(ctx -> handlerTypes.add(ctx.handlerType()));
24+
app.get("/", ctx -> handlerTypes.add(ctx.handlerType()));
25+
26+
Assertions.assertThat(http.get("/").getStatus()).isEqualTo(200);
27+
Assertions.assertThat(handlerTypes).containsExactly(HandlerType.BEFORE, HandlerType.GET, HandlerType.AFTER);
28+
});
29+
}
30+
}

0 commit comments

Comments
 (0)