Polishing
This commit is contained in:
parent
47e9360d62
commit
eae079ac2b
|
@ -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.mock.http;
|
package org.springframework.mock.http;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
@ -37,14 +38,15 @@ public class MockHttpInputMessage implements HttpInputMessage {
|
||||||
|
|
||||||
|
|
||||||
public MockHttpInputMessage(byte[] contents) {
|
public MockHttpInputMessage(byte[] contents) {
|
||||||
this.body = (contents != null) ? new ByteArrayInputStream(contents) : null;
|
this.body = (contents != null ? new ByteArrayInputStream(contents) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MockHttpInputMessage(InputStream body) {
|
public MockHttpInputMessage(InputStream body) {
|
||||||
Assert.notNull(body, "'body' must not be null");
|
Assert.notNull(body, "InputStream must not be null");
|
||||||
this.body = body;
|
this.body = body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpHeaders getHeaders() {
|
public HttpHeaders getHeaders() {
|
||||||
return this.headers;
|
return this.headers;
|
||||||
|
@ -54,4 +56,5 @@ public class MockHttpInputMessage implements HttpInputMessage {
|
||||||
public InputStream getBody() throws IOException {
|
public InputStream getBody() throws IOException {
|
||||||
return this.body;
|
return this.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,14 +33,14 @@ import org.springframework.mock.http.MockHttpOutputMessage;
|
||||||
*/
|
*/
|
||||||
public class MockClientHttpRequest extends MockHttpOutputMessage implements ClientHttpRequest {
|
public class MockClientHttpRequest extends MockHttpOutputMessage implements ClientHttpRequest {
|
||||||
|
|
||||||
private URI uri;
|
|
||||||
|
|
||||||
private HttpMethod httpMethod;
|
private HttpMethod httpMethod;
|
||||||
|
|
||||||
private boolean executed = false;
|
private URI uri;
|
||||||
|
|
||||||
private ClientHttpResponse clientHttpResponse;
|
private ClientHttpResponse clientHttpResponse;
|
||||||
|
|
||||||
|
private boolean executed = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
|
@ -56,13 +56,9 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public URI getURI() {
|
|
||||||
return this.uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setURI(URI uri) {
|
public void setMethod(HttpMethod httpMethod) {
|
||||||
this.uri = uri;
|
this.httpMethod = httpMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -70,8 +66,13 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
|
||||||
return this.httpMethod;
|
return this.httpMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMethod(HttpMethod httpMethod) {
|
public void setURI(URI uri) {
|
||||||
this.httpMethod = httpMethod;
|
this.uri = uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URI getURI() {
|
||||||
|
return this.uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setResponse(ClientHttpResponse clientHttpResponse) {
|
public void setResponse(ClientHttpResponse clientHttpResponse) {
|
||||||
|
@ -96,7 +97,6 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
|
||||||
/**
|
/**
|
||||||
* The default implementation returns the configured
|
* The default implementation returns the configured
|
||||||
* {@link #setResponse(ClientHttpResponse) response}.
|
* {@link #setResponse(ClientHttpResponse) response}.
|
||||||
*
|
|
||||||
* <p>Override this method to execute the request and provide a response,
|
* <p>Override this method to execute the request and provide a response,
|
||||||
* potentially different than the configured response.
|
* potentially different than the configured response.
|
||||||
*/
|
*/
|
||||||
|
@ -104,6 +104,7 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
|
||||||
return this.clientHttpResponse;
|
return this.clientHttpResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
@ -114,7 +115,7 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
|
||||||
sb.append(" ").append(this.uri);
|
sb.append(" ").append(this.uri);
|
||||||
}
|
}
|
||||||
if (!getHeaders().isEmpty()) {
|
if (!getHeaders().isEmpty()) {
|
||||||
sb.append(", headers : ").append(getHeaders());
|
sb.append(", headers: ").append(getHeaders());
|
||||||
}
|
}
|
||||||
if (sb.length() == 0) {
|
if (sb.length() == 0) {
|
||||||
sb.append("Not yet initialized");
|
sb.append("Not yet initialized");
|
||||||
|
|
|
@ -53,7 +53,7 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||||
* Use static factory methods in {@link MockRestResponseCreators}.
|
* Use static factory methods in {@link MockRestResponseCreators}.
|
||||||
*/
|
*/
|
||||||
protected DefaultResponseCreator(HttpStatus statusCode) {
|
protected DefaultResponseCreator(HttpStatus statusCode) {
|
||||||
Assert.notNull(statusCode);
|
Assert.notNull(statusCode, "HttpStatus must not be null");
|
||||||
this.statusCode = statusCode;
|
this.statusCode = statusCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||||
@Override
|
@Override
|
||||||
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
|
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
|
||||||
MockClientHttpResponse response;
|
MockClientHttpResponse response;
|
||||||
if (this.contentResource != null ){
|
if (this.contentResource != null) {
|
||||||
InputStream stream = this.contentResource.getInputStream();
|
InputStream stream = this.contentResource.getInputStream();
|
||||||
response = new MockClientHttpResponse(stream, this.statusCode);
|
response = new MockClientHttpResponse(stream, this.statusCode);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
||||||
|
@ -35,12 +35,14 @@ public class XpathRequestMatchersTests {
|
||||||
|
|
||||||
private MockClientHttpRequest request;
|
private MockClientHttpRequest request;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws IOException {
|
public void setUp() throws IOException {
|
||||||
this.request = new MockClientHttpRequest();
|
this.request = new MockClientHttpRequest();
|
||||||
this.request.getBody().write(RESPONSE_CONTENT.getBytes());
|
this.request.getBody().write(RESPONSE_CONTENT.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNodeMatcher() throws Exception {
|
public void testNodeMatcher() throws Exception {
|
||||||
new XpathRequestMatchers("/foo/bar", null).node(Matchers.notNullValue()).match(this.request);
|
new XpathRequestMatchers("/foo/bar", null).node(Matchers.notNullValue()).match(this.request);
|
||||||
|
|
|
@ -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.net.URI;
|
import java.net.URI;
|
||||||
|
|
|
@ -39,7 +39,6 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
|
||||||
* Examples of defining expectations on request content and content type.
|
* Examples of defining expectations on request content and content type.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
*
|
|
||||||
* @see JsonPathRequestMatchersIntegrationTests
|
* @see JsonPathRequestMatchersIntegrationTests
|
||||||
* @see XmlContentRequestMatchersIntegrationTests
|
* @see XmlContentRequestMatchersIntegrationTests
|
||||||
* @see XpathRequestMatchersIntegrationTests
|
* @see XpathRequestMatchersIntegrationTests
|
||||||
|
@ -50,6 +49,7 @@ public class ContentRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
List<HttpMessageConverter<?>> converters = new ArrayList<>();
|
List<HttpMessageConverter<?>> converters = new ArrayList<>();
|
||||||
|
@ -62,6 +62,7 @@ public class ContentRequestMatchersIntegrationTests {
|
||||||
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contentType() throws Exception {
|
public void contentType() throws Exception {
|
||||||
this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess());
|
this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess());
|
||||||
|
|
|
@ -44,10 +44,12 @@ public class HeaderRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
private static final String RESPONSE_BODY = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
|
private static final String RESPONSE_BODY = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
|
||||||
|
|
||||||
|
|
||||||
private MockRestServiceServer mockServer;
|
private MockRestServiceServer mockServer;
|
||||||
|
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
List<HttpMessageConverter<?>> converters = new ArrayList<>();
|
List<HttpMessageConverter<?>> converters = new ArrayList<>();
|
||||||
|
@ -60,9 +62,9 @@ public class HeaderRequestMatchersIntegrationTests {
|
||||||
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testString() throws Exception {
|
public void testString() throws Exception {
|
||||||
|
|
||||||
this.mockServer.expect(requestTo("/person/1"))
|
this.mockServer.expect(requestTo("/person/1"))
|
||||||
.andExpect(header("Accept", "application/json, application/*+json"))
|
.andExpect(header("Accept", "application/json, application/*+json"))
|
||||||
.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
|
.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
|
||||||
|
@ -73,7 +75,6 @@ public class HeaderRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStringContains() throws Exception {
|
public void testStringContains() throws Exception {
|
||||||
|
|
||||||
this.mockServer.expect(requestTo("/person/1"))
|
this.mockServer.expect(requestTo("/person/1"))
|
||||||
.andExpect(header("Accept", containsString("json")))
|
.andExpect(header("Accept", containsString("json")))
|
||||||
.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
|
.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
|
||||||
|
|
|
@ -19,6 +19,7 @@ package org.springframework.test.web.client.samples.matchers;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -56,7 +57,9 @@ public class JsonPathRequestMatchersIntegrationTests {
|
||||||
people.add("performers", new Person("Yehudi Menuhin"));
|
people.add("performers", new Person("Yehudi Menuhin"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private final RestTemplate restTemplate = new RestTemplate(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
|
||||||
|
private final RestTemplate restTemplate =
|
||||||
|
new RestTemplate(Collections.singletonList(new MappingJackson2HttpMessageConverter()));
|
||||||
|
|
||||||
private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ public class XmlContentRequestMatchersIntegrationTests {
|
||||||
"<composer><name>Robert Schumann</name><someBoolean>false</someBoolean><someDouble>NaN</someDouble></composer>" +
|
"<composer><name>Robert Schumann</name><someBoolean>false</someBoolean><someDouble>NaN</someDouble></composer>" +
|
||||||
"</composers></people>";
|
"</composers></people>";
|
||||||
|
|
||||||
|
|
||||||
private MockRestServiceServer mockServer;
|
private MockRestServiceServer mockServer;
|
||||||
|
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
@ -66,7 +67,6 @@ public class XmlContentRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
|
|
||||||
List<Person> composers = Arrays.asList(
|
List<Person> composers = Arrays.asList(
|
||||||
new Person("Johann Sebastian Bach").setSomeDouble(21),
|
new Person("Johann Sebastian Bach").setSomeDouble(21),
|
||||||
new Person("Johannes Brahms").setSomeDouble(.0025),
|
new Person("Johannes Brahms").setSomeDouble(.0025),
|
||||||
|
@ -97,7 +97,6 @@ public class XmlContentRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHamcrestNodeMatcher() throws Exception {
|
public void testHamcrestNodeMatcher() throws Exception {
|
||||||
|
|
||||||
this.mockServer.expect(requestTo("/composers"))
|
this.mockServer.expect(requestTo("/composers"))
|
||||||
.andExpect(content().contentType("application/xml"))
|
.andExpect(content().contentType("application/xml"))
|
||||||
.andExpect(content().node(hasXPath("/people/composers/composer[1]")))
|
.andExpect(content().node(hasXPath("/people/composers/composer[1]")))
|
||||||
|
@ -128,4 +127,5 @@ public class XmlContentRequestMatchersIntegrationTests {
|
||||||
return this.composers;
|
return this.composers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,15 +53,16 @@ public class XpathRequestMatchersIntegrationTests {
|
||||||
private static final Map<String, String> NS =
|
private static final Map<String, String> NS =
|
||||||
Collections.singletonMap("ns", "http://example.org/music/people");
|
Collections.singletonMap("ns", "http://example.org/music/people");
|
||||||
|
|
||||||
|
|
||||||
private MockRestServiceServer mockServer;
|
private MockRestServiceServer mockServer;
|
||||||
|
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
private PeopleWrapper people;
|
private PeopleWrapper people;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
|
|
||||||
List<Person> composers = Arrays.asList(
|
List<Person> composers = Arrays.asList(
|
||||||
new Person("Johann Sebastian Bach").setSomeDouble(21),
|
new Person("Johann Sebastian Bach").setSomeDouble(21),
|
||||||
new Person("Johannes Brahms").setSomeDouble(.0025),
|
new Person("Johannes Brahms").setSomeDouble(.0025),
|
||||||
|
@ -83,9 +84,9 @@ public class XpathRequestMatchersIntegrationTests {
|
||||||
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExists() throws Exception {
|
public void testExists() throws Exception {
|
||||||
|
|
||||||
String composer = "/ns:people/composers/composer[%s]";
|
String composer = "/ns:people/composers/composer[%s]";
|
||||||
String performer = "/ns:people/performers/performer[%s]";
|
String performer = "/ns:people/performers/performer[%s]";
|
||||||
|
|
||||||
|
@ -105,7 +106,6 @@ public class XpathRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDoesNotExist() throws Exception {
|
public void testDoesNotExist() throws Exception {
|
||||||
|
|
||||||
String composer = "/ns:people/composers/composer[%s]";
|
String composer = "/ns:people/composers/composer[%s]";
|
||||||
String performer = "/ns:people/performers/performer[%s]";
|
String performer = "/ns:people/performers/performer[%s]";
|
||||||
|
|
||||||
|
@ -123,7 +123,6 @@ public class XpathRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testString() throws Exception {
|
public void testString() throws Exception {
|
||||||
|
|
||||||
String composerName = "/ns:people/composers/composer[%s]/name";
|
String composerName = "/ns:people/composers/composer[%s]/name";
|
||||||
String performerName = "/ns:people/performers/performer[%s]/name";
|
String performerName = "/ns:people/performers/performer[%s]/name";
|
||||||
|
|
||||||
|
@ -146,7 +145,6 @@ public class XpathRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNumber() throws Exception {
|
public void testNumber() throws Exception {
|
||||||
|
|
||||||
String composerDouble = "/ns:people/composers/composer[%s]/someDouble";
|
String composerDouble = "/ns:people/composers/composer[%s]/someDouble";
|
||||||
|
|
||||||
this.mockServer.expect(requestTo("/composers"))
|
this.mockServer.expect(requestTo("/composers"))
|
||||||
|
@ -180,7 +178,6 @@ public class XpathRequestMatchersIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNodeCount() throws Exception {
|
public void testNodeCount() throws Exception {
|
||||||
|
|
||||||
this.mockServer.expect(requestTo("/composers"))
|
this.mockServer.expect(requestTo("/composers"))
|
||||||
.andExpect(content().contentType("application/xml"))
|
.andExpect(content().contentType("application/xml"))
|
||||||
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(4))
|
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(4))
|
||||||
|
@ -223,4 +220,5 @@ public class XpathRequestMatchersIntegrationTests {
|
||||||
return this.performers;
|
return this.performers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue