PathTrie { public static <T> Builder<T> builder() { return new Builder<>(true); } private  PathTrie(Builder<T> builder); Result<T> resolve(HttpMethod method, String path); static Builder<T> builder(); static Builder<T> builder(boolean throwOnConflict);  }
@Test public void simple() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "explorer", 1234) .build(); assertSuccessfulGetResolution(trie, "explorer", 1234); assertFailedGetResolution(trie, HttpMethod.PUT, "explorer"); assertFailedGetResolution(trie, ""); assertFailedGetResolution(trie, "test"); }
@Test public void sharedPrefix() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "discovery/v1/rest", 1234) .add(HttpMethod.GET, "discovery/v2/rest", 4321) .build(); assertSuccessfulGetResolution(trie, "discovery/v1/rest", 1234); assertSuccessfulGetResolution(trie, "discovery/v2/rest", 4321); assertFailedGetResolution(trie, ""); assertFailedGetResolution(trie, "discovery"); assertFailedGetResolution(trie, "discovery/v1"); }
@Test public void prefix() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "discovery", 1234) .add(HttpMethod.GET, "discovery/v1", 4321) .build(); assertSuccessfulGetResolution(trie, "discovery", 1234); assertSuccessfulGetResolution(trie, "discovery/v1", 4321); assertFailedGetResolution(trie, ""); }
@Test public void parameter() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "discovery/{version}/rest", 1234) .build(); assertSuccessfulGetResolution( trie, "discovery/v1/rest", 1234, ImmutableMap.of("version", "v1")); }
@Test public void multipleParameters() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "discovery/{discovery_version}/apis/{api}/{format}", 1234) .build(); assertSuccessfulGetResolution(trie, "discovery/v1/apis/test/rest", 1234, ImmutableMap.of("discovery_version", "v1", "api", "test", "format", "rest")); }
@Test public void sharedParameterPrefix() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "discovery/{version}/rest", 1234) .add(HttpMethod.GET, "discovery/{version}/rpc", 4321) .build(); assertSuccessfulGetResolution( trie, "discovery/v1/rest", 1234, ImmutableMap.of("version", "v1")); assertSuccessfulGetResolution( trie, "discovery/v1/rpc", 4321, ImmutableMap.of("version", "v1")); }
@Test public void encodedParameter() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "{value}", 1234) .build(); assertSuccessfulGetResolution( trie, "%E4%B8%AD%E6%96%87", 1234, ImmutableMap.of("value", "中文")); }
@Test public void testResolveParameterAfterLiteral() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "{one}/three", 1234) .add(HttpMethod.GET, "one/two", 4321) .build(); assertSuccessfulGetResolution(trie, "one/three", 1234, ImmutableMap.of("one", "one")); assertSuccessfulGetResolution(trie, "one/two", 4321); }
@Test public void testResolveBacktrack() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "{one}/{two}/three/{four}", 1234) .add(HttpMethod.GET, "one/two/{three}/four", 4321) .build(); assertSuccessfulGetResolution(trie, "one/two/three/five", 1234, ImmutableMap.of("one", "one", "two", "two", "four", "five")); assertSuccessfulGetResolution( trie, "one/two/three/four", 4321, ImmutableMap.of("three", "three")); }
@Test public void pathMethodsWithDifferentParameterNames() { PathTrie<Integer> trie = PathTrie.<Integer>builder() .add(HttpMethod.GET, "test/{one}", 1234) .add(HttpMethod.PUT, "test/{two}", 4321) .build(); assertSuccessfulResolution( trie, HttpMethod.GET, "test/foo", 1234, ImmutableMap.of("one", "foo")); assertSuccessfulResolution( trie, HttpMethod.PUT, "test/foo", 4321, ImmutableMap.of("two", "foo")); }
@Test public void builderNullPath() { try { PathTrie.<Integer>builder().add(HttpMethod.GET, null, 1234); fail("expected NullPointerException"); } catch (NullPointerException e) { } }
@Test public void builderNullValue() { try { PathTrie.<Integer>builder().add(HttpMethod.GET, "throws/an/exception", null); fail("expected NullPointerException"); } catch (NullPointerException e) { } }
@Test public void invalidParameterName() { try { PathTrie.<Integer>builder().add(HttpMethod.GET, "bad/{[test}", 1234); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } }
@Test public void invalidPathParameterSyntax() { try { PathTrie.<Integer> builder().add(HttpMethod.GET, "bad/{test", 1234); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } }
@Test public void invalidParameterSegment() { String invalids = "?#[]{}"; for (char c : invalids.toCharArray()) { try { PathTrie.<Integer> builder().add(HttpMethod.GET, "bad/" + c, 1234); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } } }