Skip to content

Commit

Permalink
[context] Expose HandlerType in Context (#1264) (#1265)
Browse files Browse the repository at this point in the history
  • Loading branch information
spinscale authored Jun 1, 2021
1 parent c49a659 commit bb4b3ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions javalin/src/main/java/io/javalin/http/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,7 @@ open class Context(@JvmField val req: HttpServletRequest, @JvmField val res: Htt

/** Gets a list of all the [splat] values. */
fun splats(): List<String> = Collections.unmodifiableList(splatList)

/** Gets the handler type of the current handler */
fun handlerType() : HandlerType = handlerType
}
30 changes: 30 additions & 0 deletions javalin/src/test/java/io/javalin/TestContextHandlerType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Javalin - https://javalin.io
* Copyright 2021 David Åse
* Licensed under Apache 2.0: https://github.com/tipsy/javalin/blob/master/LICENSE
*/
package io.javalin;

import io.javalin.http.HandlerType;
import io.javalin.testing.TestUtil;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class TestContextHandlerType {

@Test
public void testHandlerTypeCanBeAccessedInContext() {
TestUtil.test(Javalin.create(), (app, http) -> {
List<HandlerType> handlerTypes = new ArrayList<>();
app.before(ctx -> handlerTypes.add(ctx.handlerType()));
app.after(ctx -> handlerTypes.add(ctx.handlerType()));
app.get("/", ctx -> handlerTypes.add(ctx.handlerType()));

Assertions.assertThat(http.get("/").getStatus()).isEqualTo(200);
Assertions.assertThat(handlerTypes).containsExactly(HandlerType.BEFORE, HandlerType.GET, HandlerType.AFTER);
});
}
}

0 comments on commit bb4b3ba

Please sign in to comment.