Browse Source

Polishing

pull/1155/head
Juergen Hoeller 10 years ago
parent
commit
9475c06eb5
  1. 9
      spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java
  2. 4
      spring-test/src/main/java/org/springframework/test/web/client/RequestMatcher.java
  3. 4
      spring-test/src/main/java/org/springframework/test/web/client/RequestMatcherClientHttpRequest.java
  4. 12
      spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
  5. 9
      spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java
  6. 6
      spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java
  7. 39
      spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java
  8. 6
      spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java

9
spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.util.Assert;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@ -47,9 +48,11 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
public MockMvcClientHttpRequestFactory(MockMvc mockMvc) { public MockMvcClientHttpRequestFactory(MockMvc mockMvc) {
Assert.notNull(mockMvc, "MockMvc must not be null");
this.mockMvc = mockMvc; this.mockMvc = mockMvc;
} }
@Override @Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException { public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
return new MockClientHttpRequest(httpMethod, uri) { return new MockClientHttpRequest(httpMethod, uri) {
@ -59,17 +62,13 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString()); MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString());
requestBuilder.content(getBodyAsBytes()); requestBuilder.content(getBodyAsBytes());
requestBuilder.headers(getHeaders()); requestBuilder.headers(getHeaders());
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn(); MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse servletResponse = mvcResult.getResponse(); MockHttpServletResponse servletResponse = mvcResult.getResponse();
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
byte[] body = servletResponse.getContentAsByteArray(); byte[] body = servletResponse.getContentAsByteArray();
HttpHeaders headers = getResponseHeaders(servletResponse); HttpHeaders headers = getResponseHeaders(servletResponse);
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
clientResponse.getHeaders().putAll(headers); clientResponse.getHeaders().putAll(headers);
return clientResponse; return clientResponse;
} }
catch (Exception ex) { catch (Exception ex) {

4
spring-test/src/main/java/org/springframework/test/web/client/RequestMatcher.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,7 +29,7 @@ import org.springframework.http.client.ClientHttpRequest;
public interface RequestMatcher { 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 * @param request the request to make assertions on
* @throws IOException in case of I/O errors * @throws IOException in case of I/O errors
* @throws AssertionError if expectations are not met * @throws AssertionError if expectations are not met

4
spring-test/src/main/java/org/springframework/test/web/client/RequestMatcherClientHttpRequest.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,12 +65,10 @@ class RequestMatcherClientHttpRequest extends MockAsyncClientHttpRequest impleme
if (this.requestMatchers.isEmpty()) { if (this.requestMatchers.isEmpty()) {
throw new AssertionError("No request expectations to execute"); throw new AssertionError("No request expectations to execute");
} }
if (this.responseCreator == null) { if (this.responseCreator == null) {
throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, " + throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, " +
"e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())"); "e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())");
} }
for (RequestMatcher requestMatcher : this.requestMatchers) { for (RequestMatcher requestMatcher : this.requestMatchers) {
requestMatcher.match(this); requestMatcher.match(this);
} }

12
spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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(); this.xmlHelper = new XmlExpectationsHelper();
} }
/** /**
* Assert the request content type as a String. * Assert the request content type as a String.
*/ */
@ -140,10 +141,8 @@ public class ContentRequestMatchers {
* Parse the request body and the given String as XML and assert that the * 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 * two are "similar" - i.e. they contain the same elements and attributes
* regardless of order. * regardless of order.
*
* <p>Use of this matcher assumes the * <p>Use of this matcher assumes the
* <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available. * <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
*
* @param expectedXmlContent the expected XML content * @param expectedXmlContent the expected XML content
*/ */
public RequestMatcher xml(final String expectedXmlContent) { public RequestMatcher xml(final String expectedXmlContent) {
@ -180,6 +179,7 @@ public class ContentRequestMatchers {
}; };
} }
/** /**
* Abstract base class for XML {@link RequestMatcher}'s. * Abstract base class for XML {@link RequestMatcher}'s.
*/ */
@ -191,12 +191,12 @@ public class ContentRequestMatchers {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
matchInternal(mockRequest); matchInternal(mockRequest);
} }
catch (Exception e) { catch (Exception ex) {
throw new AssertionError("Failed to parse expected or actual XML request content: " + e.getMessage()); throw new AssertionError("Failed to parse expected or actual XML request content: " + ex.getMessage());
} }
} }
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception; protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
} }
} }

9
spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.test.web.client.match;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import com.jayway.jsonpath.JsonPath;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpRequest;
@ -26,8 +27,6 @@ import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.test.util.JsonPathExpectationsHelper; import org.springframework.test.util.JsonPathExpectationsHelper;
import org.springframework.test.web.client.RequestMatcher; import org.springframework.test.web.client.RequestMatcher;
import com.jayway.jsonpath.JsonPath;
/** /**
* Factory for assertions on the request content using * Factory for assertions on the request content using
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions. * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions.
@ -235,8 +234,8 @@ public class JsonPathRequestMatchers {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
matchInternal(mockRequest); matchInternal(mockRequest);
} }
catch (ParseException e) { catch (ParseException ex) {
throw new AssertionError("Failed to parse JSON request content: " + e.getMessage()); throw new AssertionError("Failed to parse JSON request content: " + ex.getMessage());
} }
} }

6
spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -46,12 +46,10 @@ public class XpathRequestMatchers {
* Class constructor, not for direct instantiation. Use * Class constructor, not for direct instantiation. Use
* {@link MockRestRequestMatchers#xpath(String, Object...)} or * {@link MockRestRequestMatchers#xpath(String, Object...)} or
* {@link MockRestRequestMatchers#xpath(String, Map, Object...)}. * {@link MockRestRequestMatchers#xpath(String, Map, Object...)}.
*
* @param expression the XPath expression * @param expression the XPath expression
* @param namespaces XML namespaces referenced in the XPath expression, or {@code null} * @param namespaces XML namespaces referenced in the XPath expression, or {@code null}
* @param args arguments to parameterize the XPath expression with using the * @param args arguments to parameterize the XPath expression with using the
* formatting specifiers defined in {@link String#format(String, Object...)} * formatting specifiers defined in {@link String#format(String, Object...)}
*
* @throws XPathExpressionException * @throws XPathExpressionException
*/ */
protected XpathRequestMatchers(String expression, Map<String, String> namespaces, Object ... args) protected XpathRequestMatchers(String expression, Map<String, String> namespaces, Object ... args)
@ -60,6 +58,7 @@ public class XpathRequestMatchers {
this.xpathHelper = new XpathExpectationsHelper(expression, namespaces, args); this.xpathHelper = new XpathExpectationsHelper(expression, namespaces, args);
} }
/** /**
* Apply the XPath and assert it with the given {@code Matcher<Node>}. * Apply the XPath and assert it with the given {@code Matcher<Node>}.
*/ */
@ -199,7 +198,6 @@ public class XpathRequestMatchers {
} }
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception; protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
} }
} }

39
spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.test.web.client.response; package org.springframework.test.web.client.response;
import java.io.IOException; import java.io.IOException;
@ -38,14 +39,14 @@ import org.springframework.util.Assert;
*/ */
public class DefaultResponseCreator implements ResponseCreator { public class DefaultResponseCreator implements ResponseCreator {
private HttpStatus statusCode;
private byte[] content; private byte[] content;
private Resource contentResource; private Resource contentResource;
private final HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
private HttpStatus statusCode;
/** /**
* Protected constructor. * Protected constructor.
@ -56,19 +57,6 @@ public class DefaultResponseCreator implements ResponseCreator {
this.statusCode = statusCode; this.statusCode = statusCode;
} }
@Override
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
MockClientHttpResponse response;
if (this.contentResource != null) {
InputStream stream = this.contentResource.getInputStream();
response = new MockClientHttpResponse(stream, this.statusCode);
}
else {
response = new MockClientHttpResponse(this.content, this.statusCode);
}
response.getHeaders().putAll(this.headers);
return response;
}
/** /**
* Set the body as a UTF-8 String. * Set the body as a UTF-8 String.
@ -77,9 +65,9 @@ public class DefaultResponseCreator implements ResponseCreator {
try { try {
this.content = content.getBytes("UTF-8"); this.content = content.getBytes("UTF-8");
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException ex) {
// should not happen, UTF-8 is always supported // should not happen, UTF-8 is always supported
throw new IllegalStateException(e); throw new IllegalStateException(ex);
} }
return this; return this;
} }
@ -130,4 +118,19 @@ public class DefaultResponseCreator implements ResponseCreator {
return this; return this;
} }
@Override
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
MockClientHttpResponse response;
if (this.contentResource != null) {
InputStream stream = this.contentResource.getInputStream();
response = new MockClientHttpResponse(stream, this.statusCode);
}
else {
response = new MockClientHttpResponse(this.content, this.statusCode);
}
response.getHeaders().putAll(this.headers);
return response;
}
} }

6
spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,10 +33,6 @@ import org.springframework.test.web.client.ResponseCreator;
*/ */
public abstract class MockRestResponseCreators { public abstract class MockRestResponseCreators {
private MockRestResponseCreators() {
}
/** /**
* {@code ResponseCreator} for a 200 response (OK). * {@code ResponseCreator} for a 200 response (OK).
*/ */

Loading…
Cancel
Save