From 809189be1a251bd84c2bd390d33b45564998a6b2 Mon Sep 17 00:00:00 2001 From: Drummond Dawson Date: Mon, 24 Jul 2017 21:07:47 -0400 Subject: [PATCH 1/2] URI variables with MockRestRequestMatchers requestToUriTemplate Issue: SPR-15819 --- .../web/client/match/MockRestRequestMatchers.java | 12 ++++++++++++ .../client/match/MockRestRequestMatchersTests.java | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java index 48ff16d7e3a..0838f26c1d5 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java @@ -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 diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java index da8360146f9..c2d7f9c6808 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java @@ -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")); From ea2864c73eb9262b203766102f3e7b535f760c2f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 15 Aug 2017 15:08:23 +0200 Subject: [PATCH 2/2] Polish --- .../client/match/MockRestRequestMatchers.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java index 0838f26c1d5..d67c6762dd9 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java @@ -31,9 +31,10 @@ import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; import org.springframework.web.util.UriComponentsBuilder; -import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertNotNull; -import static org.springframework.test.util.AssertionErrors.*; +import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.AssertionErrors.assertTrue; /** * Static factory methods for {@link RequestMatcher} classes. Typically used to @@ -77,7 +78,7 @@ public abstract class MockRestRequestMatchers { } /** - * Assert the request URI string. + * Assert the request URI matches the given string. * @param expectedUri the expected URI * @return the request matcher */ @@ -87,15 +88,17 @@ public abstract class MockRestRequestMatchers { } /** - * Assert the request URI string. - * @param expectedUri the expected URI + * Variant of {@link #requestTo(URI)} that prepares the URI from a URI + * template plus optional variables via {@link UriComponentsBuilder} + * including encoding. + * @param expectedUri the expected URI template * @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()); + URI uri = UriComponentsBuilder.fromUriString(expectedUri).buildAndExpand(uriVars).encode().toUri(); + return requestTo(uri); } /**