diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java index 914f949d0c..0361095c2e 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.client; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request; @@ -33,6 +34,7 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.util.Assert; /** * A {@link ClientHttpRequestFactory} for requests executed via {@link MockMvc}. @@ -46,29 +48,26 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory public MockMvcClientHttpRequestFactory(MockMvc mockMvc) { + Assert.notNull(mockMvc, "MockMvc must not be null"); this.mockMvc = mockMvc; } + public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException { return new MockClientHttpRequest(httpMethod, uri) { - @Override public ClientHttpResponse executeInternal() throws IOException { try { MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString()); requestBuilder.content(getBodyAsBytes()); requestBuilder.headers(getHeaders()); - MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn(); - MockHttpServletResponse servletResponse = mvcResult.getResponse(); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); byte[] body = servletResponse.getContentAsByteArray(); HttpHeaders headers = getResponseHeaders(servletResponse); - MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); clientResponse.getHeaders().putAll(headers); - return clientResponse; } catch (Exception ex) { diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcher.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcher.java index e59d48b5f2..a698627714 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcher.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -28,8 +28,7 @@ import org.springframework.http.client.ClientHttpRequest; public interface RequestMatcher { /** - * Match the given request against some expectations. - * + * Match the given request against specific expectations. * @param request the request to make assertions on * @throws IOException in case of I/O errors * @throws AssertionError if expectations are not met diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcherClientHttpRequest.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcherClientHttpRequest.java index a0553cfd8d..9e2217a216 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcherClientHttpRequest.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/RequestMatcherClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -45,6 +45,7 @@ class RequestMatcherClientHttpRequest extends MockClientHttpRequest implements R this.requestMatchers.add(requestMatcher); } + public ResponseActions andExpect(RequestMatcher requestMatcher) { Assert.notNull(requestMatcher, "RequestMatcher is required"); this.requestMatchers.add(requestMatcher); @@ -57,22 +58,17 @@ class RequestMatcherClientHttpRequest extends MockClientHttpRequest implements R } public ClientHttpResponse executeInternal() throws IOException { - if (this.requestMatchers.isEmpty()) { throw new AssertionError("No request expectations to execute"); } - if (this.responseCreator == null) { - throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, " - + "e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())"); + throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, " + + "e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())"); } - for (RequestMatcher requestMatcher : this.requestMatchers) { requestMatcher.match(this); } - setResponse(this.responseCreator.createResponse(this)); - return super.executeInternal(); } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java index 1bcb49a5b9..c1f856cb0b 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 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. @@ -52,6 +52,7 @@ public class ContentRequestMatchers { this.xmlHelper = new XmlExpectationsHelper(); } + /** * Assert the request content type as a String. */ @@ -135,10 +136,8 @@ public class ContentRequestMatchers { * Parse the request body and the given String as XML and assert that the * two are "similar" - i.e. they contain the same elements and attributes * regardless of order. - * *
Use of this matcher assumes the
* XMLUnit library is available.
- *
* @param expectedXmlContent the expected XML content
*/
public RequestMatcher xml(final String expectedXmlContent) {
@@ -175,6 +174,7 @@ public class ContentRequestMatchers {
};
}
+
/**
* Abstract base class for XML {@link RequestMatcher}'s.
*/
@@ -191,6 +191,6 @@ public class ContentRequestMatchers {
}
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
-
}
+
}
diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java
index d4da756185..0582929ce5 100644
--- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java
+++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2012 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.
@@ -25,8 +25,8 @@ import org.springframework.test.util.JsonPathExpectationsHelper;
import org.springframework.test.web.client.RequestMatcher;
/**
- * Factory methods for request content {@code RequestMatcher}'s using a JSONPath expression.
+ * Factory methods for request content {@code RequestMatcher}'s using a
+ * JSONPath expression.
* An instance of this class is typically accessed via
* {@code RequestMatchers.jsonPath(..)}.
*
@@ -42,7 +42,6 @@ public class JsonPathRequestMatchers {
* Class constructor, not for direct instantiation. Use
* {@link MockRestRequestMatchers#jsonPath(String, Matcher)} or
* {@link MockRestRequestMatchers#jsonPath(String, Object...)}.
- *
* @param expression the JSONPath expression
* @param args arguments to parameterize the JSONPath expression with using
* the formatting specifiers defined in
@@ -123,12 +122,12 @@ public class JsonPathRequestMatchers {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
matchInternal(mockRequest);
}
- catch (ParseException e) {
- throw new AssertionError("Failed to parse JSON request content: " + e.getMessage());
+ catch (ParseException ex) {
+ throw new AssertionError("Failed to parse JSON request content: " + ex.getMessage());
}
}
protected abstract void matchInternal(MockClientHttpRequest request) throws IOException, ParseException;
-
}
+
}
diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java
index ce3c7fb80d..292052b929 100644
--- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java
+++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2012 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.
@@ -47,13 +47,6 @@ import org.springframework.util.Assert;
*/
public abstract class MockRestRequestMatchers {
-
- /**
- * Private class constructor.
- */
- private MockRestRequestMatchers() {
- }
-
/**
* Match to any request.
*/
@@ -66,7 +59,6 @@ public abstract class MockRestRequestMatchers {
/**
* Assert the request URI string with the given matcher.
- *
* @param matcher String matcher for the expected URI
* @return the request matcher
*/
@@ -81,7 +73,6 @@ public abstract class MockRestRequestMatchers {
/**
* Assert the request URI string.
- *
* @param expectedUri the expected URI
* @return the request matcher
*/
@@ -96,7 +87,6 @@ public abstract class MockRestRequestMatchers {
/**
* Assert the {@link HttpMethod} of the request.
- *
* @param method the HTTP method
* @return the request matcher
*/
@@ -111,7 +101,6 @@ public abstract class MockRestRequestMatchers {
/**
* Expect a request to the given URI.
- *
* @param uri the expected URI
* @return the request matcher
*/
@@ -156,8 +145,8 @@ public abstract class MockRestRequestMatchers {
private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) {
List Override this method to execute the request and provide a response,
* potentially different than the configured response.
*/
@@ -101,6 +101,7 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
return this.clientHttpResponse;
}
+
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
@@ -111,7 +112,7 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
sb.append(" ").append(this.uri);
}
if (!getHeaders().isEmpty()) {
- sb.append(", headers : ").append(getHeaders());
+ sb.append(", headers: ").append(getHeaders());
}
if (sb.length() == 0) {
sb.append("Not yet initialized");
diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java
index 9ed40774f1..5b7a363207 100644
--- a/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java
+++ b/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2012 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.springframework.mock.http.client;
import java.io.IOException;
@@ -39,7 +40,7 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
*/
public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
super(body);
- Assert.notNull(statusCode, "statisCode is required");
+ Assert.notNull(statusCode, "HttpStatus is required");
this.status = statusCode;
}
@@ -48,10 +49,11 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
*/
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
super(body);
- Assert.notNull(statusCode, "statisCode is required");
+ Assert.notNull(statusCode, "HttpStatus is required");
this.status = statusCode;
}
+
public HttpStatus getStatusCode() throws IOException {
return this.status;
}