Skip to content

Commit

Permalink
Added support for dot segments in RelativeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbodart committed Sep 21, 2016
1 parent 2315357 commit e9a2d2c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
12 changes: 11 additions & 1 deletion src/com/googlecode/utterlyidle/html/RelativeUrlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ public Uri calculateUrl(Uri uri) {
String absolutePath = currentUri.mergePath(result.path()).path();
result = result.mergePath(absolutePath);
}
return result;
return result.removeDotSegments();
}

@Deprecated
public Uri getCurrentUri() {
return currentUri;
}

public Uri currentUri() {
return currentUri;
}

public RelativeUrlHandler currentUri(Uri uri) {
currentUri = uri;
return this;
}
}
41 changes: 24 additions & 17 deletions test/com/googlecode/utterlyidle/html/RelativeUrlHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,59 @@
import static org.hamcrest.MatcherAssert.assertThat;

public class RelativeUrlHandlerTest {
RecordRequestHandler delegate = new RecordRequestHandler();
RelativeUrlHandler handler = new RelativeUrlHandler(delegate);
RelativeUrlHandler handler = new RelativeUrlHandler(req -> null);

@Test
public void supportsRelativeUrlWithDotSegments() throws Exception {
String fullyQualified = "http://localhost:1234/foo/bar";
handler.handle(get(fullyQualified));
handler.handle(get("../baz/index.html"));
assertThat(handler.currentUri().toString(), is("http://localhost:1234/baz/index.html"));
}

@Test
public void ifTheFirstRequestIsRelativeTreatItRelativeToTheRoot() throws Exception {
String relative = "foo";
handler.handle(Request.get(relative));
assertThat(delegate.lastUriReceived.toString(), is("/foo"));
handler.handle(get(relative));
assertThat(handler.currentUri().toString(), is("/foo"));
}

@Test
public void preservesSchemeAndAuthority() throws Exception {
String fullyQualified = "http://localhost:1234/foo/bar?q=123";
handler.handle(Request.get(fullyQualified));
handler.handle(get(fullyQualified));
handler.handle(Request.post("/bar"));
assertThat(delegate.lastUriReceived.toString(), is("http://localhost:1234/bar"));
assertThat(handler.currentUri().toString(), is("http://localhost:1234/bar"));
}

@Test
public void preserveQueryParameters() throws Exception {
String urlWithQueryParameter = "/foo/bar?q=123";
handler.handle(Request.get(urlWithQueryParameter));
handler.handle(get(urlWithQueryParameter));
handler.handle(Request.post(""));
assertThat(handler.getCurrentUri().toString(), is(urlWithQueryParameter));
assertThat(handler.currentUri().toString(), is(urlWithQueryParameter));
}

@Test
public void preserveAbsolutePath() throws Exception {
handler.handle(Request.get("/foo/bar"));
assertThat(handler.getCurrentUri().toString(), is("/foo/bar"));
handler.handle(Request.get("baz"));
assertThat(handler.getCurrentUri().toString(), is("/foo/baz"));
handler.handle(get("/foo/bar"));
assertThat(handler.currentUri().toString(), is("/foo/bar"));
handler.handle(get("baz"));
assertThat(handler.currentUri().toString(), is("/foo/baz"));
}

@Test
public void rebuildsRelativeUrlWithQuery() throws Exception {
handler.handle(Request.get("/foo/bar"));
handler.handle(Request.get("", query("some", "param")));
assertThat(delegate.lastUriReceived, is(uri("/foo/bar?some=param")));
handler.handle(get("/foo/bar"));
handler.handle(get("", query("some", "param")));
assertThat(handler.currentUri(), is(uri("/foo/bar?some=param")));
}

@Test
public void shouldAddQueryParameterWhenPostToNoUrl() throws Exception {
handler.handle(Request.get("/foo/bar"));
handler.handle(get("/foo/bar"));
handler.handle(Request.post("", query("some", "param")));
assertThat(delegate.lastUriReceived, is(uri("/foo/bar?some=param")));
assertThat(handler.currentUri(), is(uri("/foo/bar?some=param")));
}

private static class RecordRequestHandler implements HttpHandler {
Expand Down

0 comments on commit e9a2d2c

Please sign in to comment.