URI variables with MockRestRequestMatchers requestToUriTemplate

Issue: SPR-15819
This commit is contained in:
Drummond Dawson 2017-07-24 21:07:47 -04:00 committed by Rossen Stoyanchev
parent 181f002ddf
commit 809189be1a
2 changed files with 19 additions and 0 deletions

View File

@ -86,6 +86,18 @@ public abstract class MockRestRequestMatchers {
return request -> assertEquals("Request URI", expectedUri, request.getURI().toString());
}
/**
* Assert the request URI string.
* @param expectedUri the expected URI
* @param uriVars zero or more URI variables to populate the expected URI
* @return the request matcher
*/
public static RequestMatcher requestToUriTemplate(final String expectedUri, final Object... uriVars) {
Assert.notNull(expectedUri, "'uri' must not be null");
String uri = UriComponentsBuilder.fromUriString(expectedUri).buildAndExpand(uriVars).encode().toString();
return request -> assertEquals("Request URI", uri, request.getURI().toString());
}
/**
* Expect a request to the given URI.
* @param uri the expected URI

View File

@ -45,6 +45,13 @@ public class MockRestRequestMatchersTests {
MockRestRequestMatchers.requestTo("http://foo.com/bar").match(this.request);
}
@Test // SPR-15819
public void requestToUriTemplate() throws Exception {
this.request.setURI(new URI("http://foo.com/bar"));
MockRestRequestMatchers.requestToUriTemplate("http://foo.com/{bar}", "bar").match(this.request);
}
@Test(expected = AssertionError.class)
public void requestToNoMatch() throws Exception {
this.request.setURI(new URI("http://foo.com/bar"));