Skip to content

Commit

Permalink
added context path
Browse files Browse the repository at this point in the history
  • Loading branch information
perwendel committed Aug 14, 2013
1 parent 2223fdc commit 12ab370
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/spark/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ public String servletPath() {
return servletRequest.getServletPath();
}

/**
* Returns the context path
*/
public String contextPath() {
return servletRequest.getContextPath();
}

/**
* Returns the URL string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spark/webserver/RequestWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public String pathInfo() {
public String servletPath() {
return delegate.servletPath();
}

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

@Override
public String contentType() {
Expand Down
15 changes: 14 additions & 1 deletion src/test/java/spark/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
public class RequestTest {

private static final String THE_SERVLET_PATH = "/the/servlet/path";

private static final String THE_CONTEXT_PATH = "/the/context/path";

RouteMatch match = new RouteMatch(HttpMethod.get,null,"/hi","/hi", "text/html");

@Test
Expand Down Expand Up @@ -69,6 +70,18 @@ public String getServletPath() {
assertEquals("Should have delegated getting the servlet path", THE_SERVLET_PATH, request.servletPath());
}

@Test
public void shouldBeAbleToGetTheContextPath() {
HttpServletRequest servletRequest = new MockedHttpServletRequest(new HashMap<String, String[]>()) {
@Override
public String getContextPath() {
return THE_CONTEXT_PATH;
}
};
Request request = new Request(match, servletRequest);
assertEquals("Should have delegated getting the context path", THE_CONTEXT_PATH, request.contextPath());
}

public static class MockedHttpServletRequest implements HttpServletRequest {
private Map<String, String[]> params;

Expand Down

0 comments on commit 12ab370

Please sign in to comment.