From ba826f1026b337ce7739d45f7e12af4b61c8cfea Mon Sep 17 00:00:00 2001 From: schjan79 Date: Wed, 7 Dec 2016 23:56:30 +0100 Subject: [PATCH 1/2] MockRestRequestMatchers can match query params Issue: SPR-14995 --- .../client/match/MockRestRequestMatchers.java | 77 +++++++++++++++- .../match/MockRestRequestMatchersTests.java | 87 +++++++++++++++++-- 2 files changed, 155 insertions(+), 9 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 d9d78bb9d9..d7835118e9 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 @@ -18,6 +18,7 @@ package org.springframework.test.web.client.match; import java.io.IOException; import java.net.URI; +import java.nio.charset.Charset; import java.util.List; import java.util.Map; import javax.xml.xpath.XPathExpressionException; @@ -31,7 +32,11 @@ import org.springframework.test.util.AssertionErrors; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.test.web.client.RequestMatcher; import org.springframework.util.Assert; +import org.springframework.util.MultiValueMap; +import org.springframework.web.util.UriComponentsBuilder; +import org.springframework.web.util.UriUtils; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; @@ -153,9 +158,75 @@ public abstract class MockRestRequestMatchers { } private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) { - List actualValues = headers.get(name); - AssertionErrors.assertTrue("Expected header <" + name + ">", actualValues != null); - AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount + + assertMultiValueMapCount("header", name, headers, expectedCount); + } + + + /** + * Assert request query parameter values with the given Hamcrest matcher. + */ + @SafeVarargs + public static RequestMatcher queryParameter(final String name, final Matcher... matchers) { + return queryParameter(name, UTF_8, matchers); + } + + /** + * Assert request query parameter values with the given Hamcrest matcher. + */ + @SafeVarargs + public static RequestMatcher queryParameter(final String name, final Charset charset, + final Matcher... matchers) { + return new RequestMatcher() { + @Override + public void match(ClientHttpRequest request) { + MultiValueMap queryParameters = getQueryParameters(request.getURI().toString(), charset); + assertQueryParameterValueCount(name, queryParameters, matchers.length); + for (int i = 0 ; i < matchers.length; i++) { + assertThat("Request query parameter", queryParameters.get(name).get(i), matchers[i]); + } + } + }; + } + + /** + * Assert request query parameter values. + */ + public static RequestMatcher queryParameter(final String name, final String... expectedValues) { + return queryParameter(name, UTF_8, expectedValues); + } + + /** + * Assert request query parameter values. + */ + public static RequestMatcher queryParameter(final String name, final Charset charset, final String... expectedValues) { + return new RequestMatcher() { + @Override + public void match(ClientHttpRequest request) { + MultiValueMap queryParameters = getQueryParameters(request.getURI().toString(), charset); + assertQueryParameterValueCount(name, queryParameters, expectedValues.length); + for (int i = 0 ; i < expectedValues.length; i++) { + assertEquals("Request query parameter + [" + name + "]", + expectedValues[i], queryParameters.get(name).get(i)); + } + } + }; + } + + private static MultiValueMap getQueryParameters(String uri, Charset charset) { + String decodeUri = UriUtils.decode(uri, charset); + MultiValueMap queryParameters = UriComponentsBuilder.fromUriString(decodeUri) + .build().getQueryParams(); + return queryParameters; + } + + private static void assertQueryParameterValueCount(final String name, MultiValueMap queryParameters, int expectedCount) { + assertMultiValueMapCount("query parameter", name, queryParameters, expectedCount); + } + + private static void assertMultiValueMapCount(String type, final String name, MultiValueMap multiValueMap, int expectedCount) { + List actualValues = multiValueMap.get(name); + assertTrue("Expected " + type + " <" + name + ">", actualValues != null); + assertTrue("Expected " + type + " <" + name + "> to have at least <" + expectedCount + "> values but found " + actualValues, expectedCount <= actualValues.size()); } 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 58cad79c66..03339d88fb 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 @@ -16,15 +16,20 @@ package org.springframework.test.web.client.match; +import org.junit.Test; +import org.springframework.http.HttpMethod; +import org.springframework.mock.http.client.MockClientHttpRequest; +import org.springframework.test.web.client.RequestMatcher; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.util.UriComponentsBuilder; + import java.net.URI; import java.util.Arrays; -import org.junit.Test; - -import org.springframework.http.HttpMethod; -import org.springframework.mock.http.client.MockClientHttpRequest; - -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; /** * Unit tests for {@link MockRestRequestMatchers}. @@ -129,4 +134,74 @@ public class MockRestRequestMatchersTests { MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); } + @Test + public void queryParameter() throws Exception { + this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); + + RequestMatcher requestMatcher = MockRestRequestMatchers.queryParameter("foo", "bar", "baz"); + assertThat("Factory method did not create any request matcher", requestMatcher, notNullValue()); + requestMatcher.match(this.request); + } + + @Test(expected = AssertionError.class) + public void queryParameterMissing() throws Exception { + this.request.setURI(UriComponentsBuilder + .fromUriString("http://foo.com") + .path("/bar") + .build().encode() + .toUri()); + + MockRestRequestMatchers.queryParameter("foo", "bar").match(this.request); + } + + @Test(expected = AssertionError.class) + public void queryParameterMissingValue() throws Exception { + this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); + + MockRestRequestMatchers.queryParameter("foo", "bad").match(this.request); + } + + @Test + public void queryParameterContains() throws Exception { + this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); + + RequestMatcher requestMatcher = MockRestRequestMatchers.queryParameter("foo", containsString("ba")); + assertThat("Factory method did not create any request matcher", requestMatcher, notNullValue()); + requestMatcher.match(this.request); + } + + @Test(expected = AssertionError.class) + public void queryParameterContainsWithMissingValue() throws Exception { + this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); + + MockRestRequestMatchers.queryParameter("foo", containsString("bx")).match(this.request); + } + + @Test + public void queryParameters() throws Exception { + this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); + + RequestMatcher requestMatcher = MockRestRequestMatchers.queryParameter("foo", "bar", "baz"); + assertThat("Factory method did not create any request matcher", requestMatcher, notNullValue()); + requestMatcher.match(this.request); + } + + @Test(expected = AssertionError.class) + public void queryParametersWithMissingValue() throws Exception { + this.request.setURI(createUriWithQueryParameters("foo", "bar")); + + MockRestRequestMatchers.queryParameter("foo", "bar", "baz").match(this.request); + } + + + private URI createUriWithQueryParameters(String key, String ... values) { + MultiValueMap params = new LinkedMultiValueMap<>(); + params.put(key, Arrays.asList(values)); + return UriComponentsBuilder + .fromUriString("http://foo.com") + .path("/bar") + .queryParams(params) + .build().encode().toUri(); + } + } \ No newline at end of file From 128a1f8eedfc78c6281ffa3f9fb1d2d0c4ec728a Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 13 Dec 2016 15:57:41 -0500 Subject: [PATCH 2/2] Polish MockRestRequestMatchers query param support Issue: SPR-14995 --- .../client/match/MockRestRequestMatchers.java | 159 ++++++++---------- .../match/MockRestRequestMatchersTests.java | 90 +++------- 2 files changed, 91 insertions(+), 158 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 d7835118e9..ae454f2b45 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 @@ -19,13 +19,13 @@ package org.springframework.test.web.client.match; import java.io.IOException; import java.net.URI; import java.nio.charset.Charset; +import java.util.Arrays; import java.util.List; import java.util.Map; import javax.xml.xpath.XPathExpressionException; import org.hamcrest.Matcher; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; import org.springframework.test.util.AssertionErrors; @@ -38,6 +38,7 @@ import org.springframework.web.util.UriUtils; import static java.nio.charset.StandardCharsets.UTF_8; import static org.hamcrest.MatcherAssert.*; +import static org.junit.Assert.assertNotNull; import static org.springframework.test.util.AssertionErrors.*; /** @@ -65,6 +66,21 @@ public abstract class MockRestRequestMatchers { }; } + /** + * Assert the {@link HttpMethod} of the request. + * @param method the HTTP method + * @return the request matcher + */ + public static RequestMatcher method(final HttpMethod method) { + Assert.notNull(method, "'method' must not be null"); + return new RequestMatcher() { + @Override + public void match(ClientHttpRequest request) throws AssertionError { + AssertionErrors.assertEquals("Unexpected HttpMethod", method, request.getMethod()); + } + }; + } + /** * Assert the request URI string with the given matcher. * @param matcher String matcher for the expected URI @@ -95,21 +111,6 @@ public abstract class MockRestRequestMatchers { }; } - /** - * Assert the {@link HttpMethod} of the request. - * @param method the HTTP method - * @return the request matcher - */ - public static RequestMatcher method(final HttpMethod method) { - Assert.notNull(method, "'method' must not be null"); - return new RequestMatcher() { - @Override - public void match(ClientHttpRequest request) throws AssertionError { - AssertionErrors.assertEquals("Unexpected HttpMethod", method, request.getMethod()); - } - }; - } - /** * Expect a request to the given URI. * @param uri the expected URI @@ -125,6 +126,55 @@ public abstract class MockRestRequestMatchers { }; } + /** + * Assert request query parameter values with the given Hamcrest matcher. + */ + @SafeVarargs + public static RequestMatcher queryParam(final String name, final Matcher... matchers) { + return new RequestMatcher() { + @Override + public void match(ClientHttpRequest request) { + MultiValueMap params = getQueryParams(request); + assertValueCount("query param", name, params, matchers.length); + for (int i = 0 ; i < matchers.length; i++) { + assertThat("Query param", params.get(name).get(i), matchers[i]); + } + } + }; + } + + /** + * Assert request query parameter values. + */ + public static RequestMatcher queryParam(final String name, final String... expectedValues) { + return new RequestMatcher() { + @Override + public void match(ClientHttpRequest request) { + MultiValueMap params = getQueryParams(request); + assertValueCount("query param", name, params, expectedValues.length); + for (int i = 0 ; i < expectedValues.length; i++) { + assertEquals("Query param + [" + name + "]", expectedValues[i], params.get(name).get(i)); + } + } + }; + } + + private static MultiValueMap getQueryParams(ClientHttpRequest request) { + return UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams(); + } + + private static void assertValueCount(String valueType, final String name, + MultiValueMap map, int count) { + + List values = map.get(name); + + String message = "Expected " + valueType + " <" + name + ">"; + assertNotNull(message, values); + + assertTrue(message + " to have at least <" + count + "> values but found " + values, + count <= values.size()); + } + /** * Assert request header values with the given Hamcrest matcher. */ @@ -133,7 +183,7 @@ public abstract class MockRestRequestMatchers { return new RequestMatcher() { @Override public void match(ClientHttpRequest request) { - assertHeaderValueCount(name, request.getHeaders(), matchers.length); + assertValueCount("header", name, request.getHeaders(), matchers.length); for (int i = 0 ; i < matchers.length; i++) { assertThat("Request header", request.getHeaders().get(name).get(i), matchers[i]); } @@ -148,7 +198,7 @@ public abstract class MockRestRequestMatchers { return new RequestMatcher() { @Override public void match(ClientHttpRequest request) { - assertHeaderValueCount(name, request.getHeaders(), expectedValues.length); + assertValueCount("header", name, request.getHeaders(), expectedValues.length); for (int i = 0 ; i < expectedValues.length; i++) { assertEquals("Request header + [" + name + "]", expectedValues[i], request.getHeaders().get(name).get(i)); @@ -157,79 +207,6 @@ public abstract class MockRestRequestMatchers { }; } - private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) { - assertMultiValueMapCount("header", name, headers, expectedCount); - } - - - /** - * Assert request query parameter values with the given Hamcrest matcher. - */ - @SafeVarargs - public static RequestMatcher queryParameter(final String name, final Matcher... matchers) { - return queryParameter(name, UTF_8, matchers); - } - - /** - * Assert request query parameter values with the given Hamcrest matcher. - */ - @SafeVarargs - public static RequestMatcher queryParameter(final String name, final Charset charset, - final Matcher... matchers) { - return new RequestMatcher() { - @Override - public void match(ClientHttpRequest request) { - MultiValueMap queryParameters = getQueryParameters(request.getURI().toString(), charset); - assertQueryParameterValueCount(name, queryParameters, matchers.length); - for (int i = 0 ; i < matchers.length; i++) { - assertThat("Request query parameter", queryParameters.get(name).get(i), matchers[i]); - } - } - }; - } - - /** - * Assert request query parameter values. - */ - public static RequestMatcher queryParameter(final String name, final String... expectedValues) { - return queryParameter(name, UTF_8, expectedValues); - } - - /** - * Assert request query parameter values. - */ - public static RequestMatcher queryParameter(final String name, final Charset charset, final String... expectedValues) { - return new RequestMatcher() { - @Override - public void match(ClientHttpRequest request) { - MultiValueMap queryParameters = getQueryParameters(request.getURI().toString(), charset); - assertQueryParameterValueCount(name, queryParameters, expectedValues.length); - for (int i = 0 ; i < expectedValues.length; i++) { - assertEquals("Request query parameter + [" + name + "]", - expectedValues[i], queryParameters.get(name).get(i)); - } - } - }; - } - - private static MultiValueMap getQueryParameters(String uri, Charset charset) { - String decodeUri = UriUtils.decode(uri, charset); - MultiValueMap queryParameters = UriComponentsBuilder.fromUriString(decodeUri) - .build().getQueryParams(); - return queryParameters; - } - - private static void assertQueryParameterValueCount(final String name, MultiValueMap queryParameters, int expectedCount) { - assertMultiValueMapCount("query parameter", name, queryParameters, expectedCount); - } - - private static void assertMultiValueMapCount(String type, final String name, MultiValueMap multiValueMap, int expectedCount) { - List actualValues = multiValueMap.get(name); - assertTrue("Expected " + type + " <" + name + ">", actualValues != null); - assertTrue("Expected " + type + " <" + name + "> to have at least <" + expectedCount + - "> values but found " + actualValues, expectedCount <= actualValues.size()); - } - /** * Access to request body matchers. */ 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 03339d88fb..da8360146f 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,20 +16,16 @@ package org.springframework.test.web.client.match; -import org.junit.Test; -import org.springframework.http.HttpMethod; -import org.springframework.mock.http.client.MockClientHttpRequest; -import org.springframework.test.web.client.RequestMatcher; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.util.UriComponentsBuilder; - import java.net.URI; import java.util.Arrays; +import java.util.Collections; + +import org.junit.Test; + +import org.springframework.http.HttpMethod; +import org.springframework.mock.http.client.MockClientHttpRequest; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; /** * Unit tests for {@link MockRestRequestMatchers}. @@ -129,79 +125,39 @@ public class MockRestRequestMatchersTests { @Test(expected = AssertionError.class) public void headersWithMissingValue() throws Exception { - this.request.getHeaders().put("foo", Arrays.asList("bar")); + this.request.getHeaders().put("foo", Collections.singletonList("bar")); MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); } @Test - public void queryParameter() throws Exception { - this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); - - RequestMatcher requestMatcher = MockRestRequestMatchers.queryParameter("foo", "bar", "baz"); - assertThat("Factory method did not create any request matcher", requestMatcher, notNullValue()); - requestMatcher.match(this.request); + public void queryParam() throws Exception { + this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); + MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request); } @Test(expected = AssertionError.class) - public void queryParameterMissing() throws Exception { - this.request.setURI(UriComponentsBuilder - .fromUriString("http://foo.com") - .path("/bar") - .build().encode() - .toUri()); - - MockRestRequestMatchers.queryParameter("foo", "bar").match(this.request); + public void queryParamMissing() throws Exception { + this.request.setURI(new URI("http://foo.com/a")); + MockRestRequestMatchers.queryParam("foo", "bar").match(this.request); } @Test(expected = AssertionError.class) - public void queryParameterMissingValue() throws Exception { - this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); - - MockRestRequestMatchers.queryParameter("foo", "bad").match(this.request); + public void queryParamMissingValue() throws Exception { + this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); + MockRestRequestMatchers.queryParam("foo", "bad").match(this.request); } @Test - public void queryParameterContains() throws Exception { - this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); - - RequestMatcher requestMatcher = MockRestRequestMatchers.queryParameter("foo", containsString("ba")); - assertThat("Factory method did not create any request matcher", requestMatcher, notNullValue()); - requestMatcher.match(this.request); + public void queryParamContains() throws Exception { + this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); + MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request); } @Test(expected = AssertionError.class) - public void queryParameterContainsWithMissingValue() throws Exception { - this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); - - MockRestRequestMatchers.queryParameter("foo", containsString("bx")).match(this.request); - } - - @Test - public void queryParameters() throws Exception { - this.request.setURI(createUriWithQueryParameters("foo", "bar", "baz")); - - RequestMatcher requestMatcher = MockRestRequestMatchers.queryParameter("foo", "bar", "baz"); - assertThat("Factory method did not create any request matcher", requestMatcher, notNullValue()); - requestMatcher.match(this.request); - } - - @Test(expected = AssertionError.class) - public void queryParametersWithMissingValue() throws Exception { - this.request.setURI(createUriWithQueryParameters("foo", "bar")); - - MockRestRequestMatchers.queryParameter("foo", "bar", "baz").match(this.request); - } - - - private URI createUriWithQueryParameters(String key, String ... values) { - MultiValueMap params = new LinkedMultiValueMap<>(); - params.put(key, Arrays.asList(values)); - return UriComponentsBuilder - .fromUriString("http://foo.com") - .path("/bar") - .queryParams(params) - .build().encode().toUri(); + public void queryParamContainsWithMissingValue() throws Exception { + this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); + MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request); } } \ No newline at end of file