StdRestxRequestMatcher implements RestxRequestMatcher { @Override public Optional<? extends RestxRequestMatch> match(String method, String path) { if (!this.endpoint.getMethod().equals(method)) { return Optional.absent(); } Matcher m = pattern.matcher(path); if (!m.matches()) { return Optional.absent(); } ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); for (int i = 0; i < m.groupCount() && i < groupNames.size(); i++) { params.put(groupNames.get(i), m.group(i + 1)); } return Optional.of(new StdRestxRequestMatch(this.endpoint.getPathPattern(), path, params.build())); } StdRestxRequestMatcher(Endpoint endpoint);  StdRestxRequestMatcher(String method, String pathPattern); @Override Optional<? extends RestxRequestMatch> match(String method, String path); @Override String toString(); String getMethod(); String getPathPattern(); String getStdPathPattern(); ImmutableList<String> getPathParamNames();  }
@Test public void should_matcher_with_no_path_param_match_not_match() throws Exception { StdRestxRequestMatcher matcher = new StdRestxRequestMatcher("GET", "/user"); Optional<? extends RestxRequestMatch> match = matcher.match("GET", "/user"); assertThat(match.isPresent()).isTrue(); assertThat(match.get().getPathParams()).isEmpty(); match = matcher.match("POST", "/user"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/johndoe"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/use"); assertThat(match.isPresent()).isFalse(); }
@Test public void should_matcher_with_one_path_param_match_not_match() throws Exception { StdRestxRequestMatcher matcher = new StdRestxRequestMatcher("GET", "/user/{name}"); Optional<? extends RestxRequestMatch> match = matcher.match("GET", "/user/johndoe"); assertThat(match.isPresent()).isTrue(); assertThat(match.get().getPathParams()).isEqualTo(ImmutableMap.of("name", "johndoe")); assertThat(match.get().getPathParam("name")).isEqualTo("johndoe"); match = matcher.match("POST", "/user/johndoe"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/johndoe/details"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/users/johndoe"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/"); assertThat(match.isPresent()).isFalse(); }
@Test public void should_matcher_with_several_path_params_match_not_match() throws Exception { StdRestxRequestMatcher matcher = new StdRestxRequestMatcher("GET", "/user/{name}/children/{child1}/{child2}"); Optional<? extends RestxRequestMatch> match = matcher.match("GET", "/user/johndoe/children/bobby/drake"); assertThat(match.isPresent()).isTrue(); assertThat(match.get().getPathParams()).isEqualTo(ImmutableMap.of("name", "johndoe", "child1", "bobby", "child2", "drake")); match = matcher.match("GET", "/user/johndoe"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/johndoe/children/"); assertThat(match.isPresent()).isFalse(); }
@Test public void should_matcher_with_several_path_params_and_custom_regex_match_not_match() throws Exception { StdRestxRequestMatcher matcher = new StdRestxRequestMatcher("GET", "/user/{name:[A-Z]{1,3}\\d}/children/{child:.+}"); Optional<? extends RestxRequestMatch> match; match = matcher.match("GET", "/user/johndoe/children/bobby"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/JOHNDOE/children/bobby"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/JOH1/children/bobby"); assertThat(match.isPresent()).isTrue(); assertThat(match.get().getPathParams()).isEqualTo(ImmutableMap.of("name", "JOH1", "child", "bobby")); match = matcher.match("GET", "/user/JOH2/children/bobby/and/the/rest"); assertThat(match.isPresent()).isTrue(); assertThat(match.get().getPathParams()).isEqualTo(ImmutableMap.of("name", "JOH2", "child", "bobby/and/the/rest")); match = matcher.match("GET", "/user/johndoe"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/johndoe/children/"); assertThat(match.isPresent()).isFalse(); }
@Test public void should_matcher_with_several_path_params_with_column_notation_match_not_match() throws Exception { StdRestxRequestMatcher matcher; Optional<? extends RestxRequestMatch> match; matcher = new StdRestxRequestMatcher("GET", "/user/:name/children/:child1/:child2"); match = matcher.match("GET", "/user/johndoe/children/bobby/drake"); assertThat(match.isPresent()).isTrue(); assertThat(match.get().getPathParams()).isEqualTo(ImmutableMap.of("name", "johndoe", "child1", "bobby", "child2", "drake")); match = matcher.match("GET", "/user/johndoe"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user"); assertThat(match.isPresent()).isFalse(); match = matcher.match("GET", "/user/johndoe/children/"); assertThat(match.isPresent()).isFalse(); }