Backported refinements and polishing

This commit is contained in:
Juergen Hoeller
2016-07-20 21:46:25 +02:00
parent 503d65d570
commit 36e1c82ef5
70 changed files with 271 additions and 288 deletions

View File

@@ -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.
@@ -18,6 +18,7 @@ package org.springframework.test.web.client;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.List;
import org.springframework.http.HttpHeaders;
@@ -43,6 +44,8 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
*/
public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private final MockMvc mockMvc;
@@ -50,6 +53,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
this.mockMvc = mockMvc;
}
@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
return new MockClientHttpRequest(httpMethod, uri) {
@@ -73,7 +77,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
return clientResponse;
}
catch (Exception ex) {
byte[] body = ex.toString().getBytes("UTF-8");
byte[] body = ex.toString().getBytes(UTF8_CHARSET);
return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@@ -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,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.client.response;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
@@ -38,6 +39,9 @@ import org.springframework.util.Assert;
*/
public class DefaultResponseCreator implements ResponseCreator {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private byte[] content;
private Resource contentResource;
@@ -56,6 +60,7 @@ public class DefaultResponseCreator implements ResponseCreator {
this.statusCode = statusCode;
}
@Override
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
MockClientHttpResponse response;
@@ -74,13 +79,7 @@ public class DefaultResponseCreator implements ResponseCreator {
* Set the body as a UTF-8 String.
*/
public DefaultResponseCreator body(String content) {
try {
this.content = content.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
// should not happen, UTF-8 is always supported
throw new IllegalStateException(e);
}
this.content = content.getBytes(UTF8_CHARSET);
return this;
}

View File

@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
@@ -78,6 +79,9 @@ import org.springframework.web.util.UriUtils;
public class MockHttpServletRequestBuilder
implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private final String method;
private final URI url;
@@ -272,12 +276,7 @@ public class MockHttpServletRequestBuilder
* @param content the body content
*/
public MockHttpServletRequestBuilder content(String content) {
try {
this.content = content.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
// should never happen
}
this.content = content.getBytes(UTF8_CHARSET);
return this;
}