UriUtils { public static String parsePath(String uri) { if (uri == null) { return null; } int schemePosition = uri.indexOf(": if (schemePosition == 0) { return null; } String schemeLess; if (schemePosition != -1) { schemeLess = uri.substring(schemePosition + 3); } else { schemeLess = uri; } String authority = parseAuthority(uri); String path; if (authority == null) { int beginningOfQuery = schemeLess.indexOf("?"); int beginningOfFragment = schemeLess.indexOf("#"); if (beginningOfQuery != -1) { path = schemeLess.substring(0, beginningOfQuery); } else if (beginningOfFragment != -1) { path = schemeLess.substring(0, beginningOfFragment); } else { path = schemeLess; } } else { String authorityLess = schemeLess.replace(authority, ""); int beginningOfQuery = authorityLess.indexOf("?"); int beginningOfFragment = authorityLess.indexOf("#"); if (beginningOfQuery != -1) { path = authorityLess.substring(0, beginningOfQuery); } else if (beginningOfFragment != -1) { path = authorityLess.substring(0, beginningOfFragment); } else { path = authorityLess; } } return path; } private  UriUtils(); static List<String> parseSegments(String uri); static List<InetSocketAddress> parseInetSocketAddresses(String uri); static List<InetSocketAddress> parseInetSocketAddresses(String uri, int defaultPort); static String parseScheme(String uri); static String parseAuthority(String uri); static String parseUserInfo(String uri); static String parsePath(String uri); static String parseQuery(String uri); static String appendSegmentToPath(String path, String segment);  }
@Test public void testParsePath_relativePathWithQueryAndFragment() { String input = "path/to/resource?simulation-freq=10&max-servers=2&timeout=2#frag"; String path = UriUtils.parsePath(input); String expectedPath = URI.create(input).getPath(); assertThat(input, path, equalTo(expectedPath)); }
@Test public void testParsePath_noPath_1() { String input = "scheme: String path = UriUtils.parsePath(input); String expectedPath = URI.create(input).getPath(); assertThat(input, path, equalTo(expectedPath)); }
@Test public void testParsePath_noPath_2() { String input = "scheme: String path = UriUtils.parsePath(input); String expectedPath = URI.create(input).getPath(); assertThat(input, path, equalTo(expectedPath)); }
@Test public void testParsePath_ok_1() { String input = "stripe: String path = UriUtils.parsePath(input); String expectedPath = URI.create(input).getPath(); assertThat(input, path, equalTo(expectedPath)); }
@Test public void testParsePath_ok_2() { String input = "stripe: String path = UriUtils.parsePath(input); String expectedPath = URI.create(input).getPath(); assertThat(input, path, equalTo(expectedPath)); }
@Test public void testParsePath_absolutePathWithQuery() { String input = "/path/to/resource?simulation-freq=10&max-servers=2&timeout=2"; String path = UriUtils.parsePath(input); String expectedPath = URI.create(input).getPath(); assertThat(input, path, equalTo(expectedPath)); }
@Test public void testParsePath_absolutePathWithFragment() { String input = "/path/to/resource#frag"; String path = UriUtils.parsePath(input); String expectedPath = URI.create(input).getPath(); assertThat(input, path, equalTo(expectedPath)); }