Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new field and getter in Request for matchedRoute #1049

Merged
merged 3 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/spark/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class Request {

private Session session = null;
private boolean validSession = false;
private String matchedRouteUri = null;


/* Lazy loaded stuff */
Expand Down Expand Up @@ -100,6 +101,7 @@ protected Request() {
*/
Request(RouteMatch match, HttpServletRequest request) {
this.servletRequest = request;
this.matchedRouteUri = match.getMatchUri();
changeMatch(match);
}

Expand All @@ -120,6 +122,7 @@ protected void changeMatch(RouteMatch match) {
List<String> requestList = SparkUtils.convertRouteToList(match.getRequestURI());
List<String> matchedList = SparkUtils.convertRouteToList(match.getMatchUri());

this.matchedRouteUri = match.getMatchUri();
params = getParams(requestList, matchedList);
splat = getSplat(requestList, matchedList);
}
Expand Down Expand Up @@ -203,6 +206,12 @@ public String pathInfo() {
return servletRequest.getPathInfo();
}

/**
* @return the matched route
* Example return: "/account/:accountId"
*/
public String matchedRouteUri() { return this.matchedRouteUri; }

/**
* @return the servlet path
*/
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/spark/http/matching/RequestWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public String pathInfo() {
return delegate.pathInfo();
}

@Override
public String matchedRouteUri() { return delegate.matchedRouteUri(); }

@Override
public String servletPath() {
return delegate.servletPath();
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/spark/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.junit.Before;
import org.junit.Test;
import spark.examples.sugar.http;
import spark.routematch.RouteMatch;
import spark.util.SparkTestUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -14,20 +16,46 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static spark.Spark.*;

public class RequestTest {

private static final String THE_SERVLET_PATH = "/the/servlet/path";
private static final String THE_CONTEXT_PATH = "/the/context/path";
private static final String THE_MATCHED_ROUTE = "/users/:username";
private static final String BEFORE_MATCHED_ROUTE = "/users/:before";
private static final String AFTER_MATCHED_ROUTE = "/users/:after";
private static final String AFTERAFTER_MATCHED_ROUTE = "/users/:afterafter";

private static SparkTestUtil http;

HttpServletRequest servletRequest;
HttpSession httpSession;
Request request;

RouteMatch match = new RouteMatch(null, "/hi", "/hi", "text/html");
RouteMatch matchWithParams = new RouteMatch(null, "/users/:username", "/users/bob", "text/html");

@Before
public void setup() {
http = new SparkTestUtil(4567);

before(BEFORE_MATCHED_ROUTE, (q, a) -> {
System.out.println("before filter matched");
shouldBeAbleToGetTheMatchedPathInBeforeFilter(q);
});
get(THE_MATCHED_ROUTE, (q,a)-> "Get filter matched");
after(AFTER_MATCHED_ROUTE, (q, a) -> {
System.out.println("after filter matched");
shouldBeAbleToGetTheMatchedPathInAfterFilter(q);
});
afterAfter(AFTERAFTER_MATCHED_ROUTE, (q, a) -> {
System.out.println("afterafter filter matched");
shouldBeAbleToGetTheMatchedPathInAfterAfterFilter(q);
});

awaitInitialization();


servletRequest = mock(HttpServletRequest.class);
httpSession = mock(HttpSession.class);
Expand Down Expand Up @@ -92,6 +120,29 @@ public void shouldBeAbleToGetTheContextPath() {
assertEquals("Should have delegated getting the context path", THE_CONTEXT_PATH, request.contextPath());
}

@Test
public void shouldBeAbleToGetTheMatchedPath() {
Request request = new Request(matchWithParams, servletRequest);
assertEquals("Should have returned the matched route", THE_MATCHED_ROUTE, request.matchedRouteUri());
try {
http.get("/users/bob");
} catch (Exception e) {
e.printStackTrace();
}
}

public void shouldBeAbleToGetTheMatchedPathInBeforeFilter(Request q) {
assertEquals("Should have returned the matched route from the before filter", BEFORE_MATCHED_ROUTE, q.matchedRouteUri());
}

public void shouldBeAbleToGetTheMatchedPathInAfterFilter(Request q) {
assertEquals("Should have returned the matched route from the after filter", AFTER_MATCHED_ROUTE, q.matchedRouteUri());
}

public void shouldBeAbleToGetTheMatchedPathInAfterAfterFilter(Request q) {
assertEquals("Should have returned the matched route from the afterafter filter", AFTERAFTER_MATCHED_ROUTE, q.matchedRouteUri());
}

@Test
public void testSessionNoParams_whenSessionIsNull() {

Expand Down