@Test
public void buildUri() throws URISyntaxException {

    URI baseUri = new URI("http://localhost:8000/something");
    URI baseHttpsUri = new URI("https://localhost:8000/something");
    String pathSegment = UUID.randomUUID().toString();
    String pathSegment2 = UUID.randomUUID().toString();
    URI u = UriUtils.buildUri(baseUri, pathSegment);
    assertEquals("http", u.getScheme());
    assertEquals("/" + pathSegment, u.getPath());
    assertTrue(u.getQuery() == null);
    u = UriUtils.buildUri(baseUri, pathSegment, pathSegment2);
    assertEquals("http", u.getScheme());
    assertEquals("/" + pathSegment + "/" + pathSegment2, u.getPath());
    assertTrue(u.getQuery() == null);
    u = UriUtils.buildUri(baseHttpsUri, pathSegment);
    assertEquals("https", u.getScheme());
    assertEquals("/" + pathSegment, u.getPath());
    assertTrue(u.getQuery() == null);
    assertEquals(8000, u.getPort());
    String query = "key=value&key2=value2";
    String pathWithQuery = pathSegment + "?" + query;
    u = UriUtils.buildUri(baseUri, pathWithQuery, pathSegment2);
    assertEquals("/" + pathSegment + "/" + pathSegment2, u.getPath());
    assertEquals(query, u.getQuery());
    pathWithQuery = pathSegment + "?" + query;
    String pathWithQuery2 = pathSegment2 + "?" + query;
    String combinedQuery = query + query;
    u = UriUtils.buildUri(baseUri, pathWithQuery, pathWithQuery2);
    assertEquals("/" + pathSegment + "/" + pathSegment2, u.getPath());
    assertEquals(combinedQuery, u.getQuery());
    final String queryContainingQueryChar = "key=value&url=/url/with?query=true";
    String pathWithMutlipleQueryChars = pathSegment + "?" + queryContainingQueryChar;
    u = UriUtils.buildUri(baseUri, pathWithMutlipleQueryChars);
    assertEquals("/" + pathSegment, u.getPath());
    assertEquals(queryContainingQueryChar, u.getQuery());
    String pathEndingWithQueryChar = pathSegment + "?";
    u = UriUtils.buildUri(baseUri, pathEndingWithQueryChar);
    assertEquals("/" + pathSegment, u.getPath());
    assertEquals(null, u.getQuery());
    String queryContainingOnlyQueryChars = "??";
    String pathEndingWithQueryChars = pathSegment + "?" + queryContainingOnlyQueryChars;
    u = UriUtils.buildUri(baseUri, pathEndingWithQueryChars);
    assertEquals("/" + pathSegment, u.getPath());
    assertEquals(queryContainingOnlyQueryChars, u.getQuery());
    URI abNormalUri = new URI("http://localhost:8000/foo/./../bar");
    u = UriUtils.buildUri(abNormalUri, abNormalUri.getPath());
    assertEquals("/bar", u.getPath());
    abNormalUri = new URI("http://localhost:8000/foo/./bar");
    u = UriUtils.buildUri(abNormalUri, abNormalUri.getPath());
    assertEquals("/foo/bar", u.getPath());
}