Skip to content

Commit

Permalink
Merge pull request #58 from rabeyta/issue55
Browse files Browse the repository at this point in the history
Add getPathTemplate method to RequestInfo
  • Loading branch information
nicmunroe authored Jun 2, 2017
2 parents bc64185 + 37a9979 commit 2e57e93
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public interface RequestInfo<T> {
* The full URI associated with this request. This will be the raw, potentially encoded, value sent to the server.
* It may include the query string (use {@link #getPath()} if you don't want the query string).
*
* see {@link #getPathTemplate()} if you are looking for the matching template of the request
*
* Will never be null - the empty string will be used if no URI information was provided.
*/
public String getUri();
Expand All @@ -53,6 +55,8 @@ public interface RequestInfo<T> {
*
* The path value returned will be URL decoded before being returned.
*
* see {@link #getPathTemplate()} if you are looking for the matching template of the request
*
* Will never be null - the empty string will be used if no path information could be extracted.
*/
public String getPath();
Expand Down Expand Up @@ -384,4 +388,18 @@ public default String getPathParam(String key) {
* do not need to worry about this issue - it's a problem for the server to solve.
*/
public void releaseMultipartData();

/**
* This will return the path template set by {@link #setPathParamsBasedOnPathTemplate(String)},
* which corresponds to an endpoint {@link com.nike.riposte.util.Matcher}'s path template and is automatically set
* when a request is matched to an endpoint.
*
* <p>Example path template: "/app/{appId}/user/{userId}".
* Note this different than {@link #getPath()}, which in this example might be "/app/foo/user/bar".
*
* <p>Will never be null - an empty string will be used if {@link #setPathParamsBasedOnPathTemplate(String)}
* was never called (should only happen in error cases where an endpoint was not called).
*/
public String getPathTemplate();

}
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,12 @@ public Map<String, Object> getRequestAttributes() {
return attributes;
}

/**
* {@inheritDoc}
*/
@Override
public String getPathTemplate() {
return this.pathTemplate == null ? "" : this.pathTemplate;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ public void releaseMultipartData() {

}

@Override
public String getPathTemplate() {
return null;
}

@Override
public void addRequestAttribute(String attributeName, Object attributeValue) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ public void setPathParamsBasedOnPathTemplate_works_as_expected(String path, Stri
assertThat(requestInfo.getPathParam("param2"), is(expectedParam2));
assertThat(requestInfo.getPathParams().get("param1"), is(expectedParam1));
assertThat(requestInfo.getPathParams().get("param2"), is(expectedParam2));
assertThat(requestInfo.getPathTemplate(), is(pathTemplate));
}

@Test(expected = PathParameterMatchingException.class)
Expand Down Expand Up @@ -914,6 +915,18 @@ public void addRequestAttribute_works_as_expected() {
assertThat(requestInfo.getRequestAttributes().get(attributeName), is(attributeValue));
}

@Test
public void getPathTemplate_returns_empty_string_when_null_value() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();

// when
String pathTemplate = requestInfo.getPathTemplate();

// then
assertThat(pathTemplate, is(""));
}

public static class TestContentObject {
public final String foo;
public final String bar;
Expand Down

0 comments on commit 2e57e93

Please sign in to comment.