Polishing

This commit is contained in:
Juergen Hoeller
2018-01-19 21:30:37 +01:00
parent 5520e730f1
commit 572c668726
11 changed files with 110 additions and 109 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -50,7 +50,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.http.MediaType.*;
/**
@@ -89,7 +89,7 @@ public class RestTemplateTests {
@Test
public void varArgsTemplateVariables() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com/hotels/42/bookings/21"), HttpMethod.GET))
given(requestFactory.createRequest(new URI("http://example.com/hotels/42/bookings/21"), GET))
.willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
@@ -97,7 +97,7 @@ public class RestTemplateTests {
given(response.getStatusCode()).willReturn(status);
given(response.getStatusText()).willReturn(status.getReasonPhrase());
template.execute("http://example.com/hotels/{hotel}/bookings/{booking}", HttpMethod.GET, null, null, "42",
template.execute("http://example.com/hotels/{hotel}/bookings/{booking}", GET, null, null, "42",
"21");
verify(response).close();
@@ -105,7 +105,7 @@ public class RestTemplateTests {
@Test
public void varArgsNullTemplateVariable() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com/-foo"), HttpMethod.GET))
given(requestFactory.createRequest(new URI("http://example.com/-foo"), GET))
.willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
@@ -113,14 +113,14 @@ public class RestTemplateTests {
given(response.getStatusCode()).willReturn(status);
given(response.getStatusText()).willReturn(status.getReasonPhrase());
template.execute("http://example.com/{first}-{last}", HttpMethod.GET, null, null, null, "foo");
template.execute("http://example.com/{first}-{last}", GET, null, null, null, "foo");
verify(response).close();
}
@Test
public void mapTemplateVariables() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com/hotels/42/bookings/42"), HttpMethod.GET))
given(requestFactory.createRequest(new URI("http://example.com/hotels/42/bookings/42"), GET))
.willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
@@ -129,14 +129,14 @@ public class RestTemplateTests {
given(response.getStatusText()).willReturn(status.getReasonPhrase());
Map<String, String> vars = Collections.singletonMap("hotel", "42");
template.execute("http://example.com/hotels/{hotel}/bookings/{hotel}", HttpMethod.GET, null, null, vars);
template.execute("http://example.com/hotels/{hotel}/bookings/{hotel}", GET, null, null, vars);
verify(response).close();
}
@Test
public void mapNullTemplateVariable() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com/-foo"), HttpMethod.GET))
given(requestFactory.createRequest(new URI("http://example.com/-foo"), GET))
.willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
@@ -147,7 +147,7 @@ public class RestTemplateTests {
Map<String, String> vars = new HashMap<>(2);
vars.put("first", null);
vars.put("last", "foo");
template.execute("http://example.com/{first}-{last}", HttpMethod.GET, null, null, vars);
template.execute("http://example.com/{first}-{last}", GET, null, null, vars);
verify(response).close();
}
@@ -155,14 +155,14 @@ public class RestTemplateTests {
@Test // SPR-15201
public void uriTemplateWithTrailingSlash() throws Exception {
String url = "http://example.com/spring/";
given(requestFactory.createRequest(new URI(url), HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(new URI(url), GET)).willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
HttpStatus status = HttpStatus.OK;
given(response.getStatusCode()).willReturn(status);
given(response.getStatusText()).willReturn(status.getReasonPhrase());
template.execute(url, HttpMethod.GET, null, null);
template.execute(url, GET, null, null);
verify(response).close();
}
@@ -170,16 +170,16 @@ public class RestTemplateTests {
@Test
public void errorHandling() throws Exception {
URI uri = new URI("http://example.com");
given(requestFactory.createRequest(uri, HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(uri, GET)).willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(true);
given(response.getStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR);
given(response.getStatusText()).willReturn("Internal Server Error");
willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR))
.given(errorHandler).handleError(uri, HttpMethod.GET, response);
.given(errorHandler).handleError(uri, GET, response);
try {
template.execute("http://example.com", HttpMethod.GET, null, null);
template.execute("http://example.com", GET, null, null);
fail("HttpServerErrorException expected");
}
catch (HttpServerErrorException ex) {
@@ -194,7 +194,7 @@ public class RestTemplateTests {
given(converter.canRead(String.class, null)).willReturn(true);
MediaType textPlain = new MediaType("text", "plain");
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(textPlain));
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), GET)).willReturn(request);
HttpHeaders requestHeaders = new HttpHeaders();
given(request.getHeaders()).willReturn(requestHeaders);
given(request.execute()).willReturn(response);
@@ -224,7 +224,7 @@ public class RestTemplateTests {
given(converter.canRead(String.class, null)).willReturn(true);
MediaType supportedMediaType = new MediaType("foo", "bar");
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(supportedMediaType));
given(requestFactory.createRequest(new URI("http://example.com/resource"), HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com/resource"), GET)).willReturn(request);
HttpHeaders requestHeaders = new HttpHeaders();
given(request.getHeaders()).willReturn(requestHeaders);
given(request.execute()).willReturn(response);
@@ -258,7 +258,7 @@ public class RestTemplateTests {
given(converter.canRead(String.class, null)).willReturn(true);
MediaType textPlain = new MediaType("text", "plain");
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(textPlain));
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), GET)).willReturn(request);
HttpHeaders requestHeaders = new HttpHeaders();
given(request.getHeaders()).willReturn(requestHeaders);
given(request.execute()).willReturn(response);
@@ -289,7 +289,7 @@ public class RestTemplateTests {
template.setUriTemplateHandler(uriTemplateHandler);
URI expectedUri = new URI("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150");
given(requestFactory.createRequest(expectedUri, HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(expectedUri, GET)).willReturn(request);
given(request.getHeaders()).willReturn(new HttpHeaders());
given(request.execute()).willReturn(response);
@@ -312,7 +312,7 @@ public class RestTemplateTests {
@Test
public void headForHeaders() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.HEAD)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), HEAD)).willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
HttpHeaders responseHeaders = new HttpHeaders();
@@ -574,7 +574,7 @@ public class RestTemplateTests {
@Test
public void put() throws Exception {
given(converter.canWrite(String.class, null)).willReturn(true);
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.PUT)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), PUT)).willReturn(request);
String helloWorld = "Hello World";
converter.write(helloWorld, null, request);
given(request.execute()).willReturn(response);
@@ -590,7 +590,7 @@ public class RestTemplateTests {
@Test
public void putNull() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.PUT)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), PUT)).willReturn(request);
HttpHeaders requestHeaders = new HttpHeaders();
given(request.getHeaders()).willReturn(requestHeaders);
given(request.execute()).willReturn(response);
@@ -610,7 +610,7 @@ public class RestTemplateTests {
MediaType textPlain = new MediaType("text", "plain");
given(converter.canRead(Integer.class, null)).willReturn(true);
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(textPlain));
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.PATCH)).willReturn(this.request);
given(requestFactory.createRequest(new URI("http://example.com"), PATCH)).willReturn(this.request);
HttpHeaders requestHeaders = new HttpHeaders();
given(this.request.getHeaders()).willReturn(requestHeaders);
String request = "Hello World";
@@ -643,7 +643,7 @@ public class RestTemplateTests {
MediaType textPlain = new MediaType("text", "plain");
given(converter.canRead(Integer.class, null)).willReturn(true);
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(textPlain));
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.PATCH)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), PATCH)).willReturn(request);
HttpHeaders requestHeaders = new HttpHeaders();
given(request.getHeaders()).willReturn(requestHeaders);
given(request.execute()).willReturn(response);
@@ -671,7 +671,7 @@ public class RestTemplateTests {
@Test
public void delete() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.DELETE)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), DELETE)).willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
HttpStatus status = HttpStatus.OK;
@@ -685,11 +685,11 @@ public class RestTemplateTests {
@Test
public void optionsForAllow() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.OPTIONS)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), OPTIONS)).willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
HttpHeaders responseHeaders = new HttpHeaders();
EnumSet<HttpMethod> expected = EnumSet.of(HttpMethod.GET, POST);
EnumSet<HttpMethod> expected = EnumSet.of(GET, POST);
responseHeaders.setAllow(expected);
given(response.getHeaders()).willReturn(responseHeaders);
HttpStatus status = HttpStatus.OK;
@@ -702,14 +702,14 @@ public class RestTemplateTests {
verify(response).close();
}
@Test // Issue: SPR-9325, SPR-13860
@Test // SPR-9325, SPR-13860
public void ioException() throws Exception {
String url = "http://example.com/resource?access_token=123";
given(converter.canRead(String.class, null)).willReturn(true);
MediaType mediaType = new MediaType("foo", "bar");
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(mediaType));
given(requestFactory.createRequest(new URI(url), HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(new URI(url), GET)).willReturn(request);
given(request.getHeaders()).willReturn(new HttpHeaders());
given(request.execute()).willThrow(new IOException("Socket failure"));
@@ -724,7 +724,7 @@ public class RestTemplateTests {
}
}
@Test // SPR-15900
@Test // SPR-15900
public void ioExceptionWithEmptyQueryString() throws Exception {
// http://example.com/resource?
@@ -732,7 +732,7 @@ public class RestTemplateTests {
given(converter.canRead(String.class, null)).willReturn(true);
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(parseMediaType("foo/bar")));
given(requestFactory.createRequest(uri, HttpMethod.GET)).willReturn(request);
given(requestFactory.createRequest(uri, GET)).willReturn(request);
given(request.getHeaders()).willReturn(new HttpHeaders());
given(request.execute()).willThrow(new IOException("Socket failure"));
@@ -825,7 +825,7 @@ public class RestTemplateTests {
verify(response).close();
}
@Test // SPR-15066
@Test // SPR-15066
public void requestInterceptorCanAddExistingHeaderValueWithoutBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
@@ -833,7 +833,7 @@ public class RestTemplateTests {
};
template.setInterceptors(Collections.singletonList(interceptor));
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.POST)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), POST)).willReturn(request);
HttpHeaders requestHeaders = new HttpHeaders();
given(request.getHeaders()).willReturn(requestHeaders);
given(request.execute()).willReturn(response);
@@ -845,13 +845,13 @@ public class RestTemplateTests {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders);
template.exchange("http://example.com", HttpMethod.POST, entity, Void.class);
template.exchange("http://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
verify(response).close();
}
@Test // SPR-15066
@Test // SPR-15066
public void requestInterceptorCanAddExistingHeaderValueWithBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
@@ -861,7 +861,7 @@ public class RestTemplateTests {
MediaType contentType = MediaType.TEXT_PLAIN;
given(converter.canWrite(String.class, contentType)).willReturn(true);
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.POST)).willReturn(request);
given(requestFactory.createRequest(new URI("http://example.com"), POST)).willReturn(request);
String helloWorld = "Hello World";
HttpHeaders requestHeaders = new HttpHeaders();
given(request.getHeaders()).willReturn(requestHeaders);
@@ -876,7 +876,7 @@ public class RestTemplateTests {
entityHeaders.setContentType(contentType);
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
template.exchange("http://example.com", HttpMethod.POST, entity, Void.class);
template.exchange("http://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
verify(response).close();

View File

@@ -185,7 +185,7 @@ public class UriComponentsBuilderTests {
}
@Test // SPR-9832
public void fromUriStringQueryParamWithReservedCharInValue() throws URISyntaxException {
public void fromUriStringQueryParamWithReservedCharInValue() {
String uri = "http://www.google.com/ig/calculator?q=1USD=?EUR";
UriComponents result = UriComponentsBuilder.fromUriString(uri).build();
@@ -194,7 +194,7 @@ public class UriComponentsBuilderTests {
}
@Test // SPR-14828
public void fromUriStringQueryParamEncodedAndContainingPlus() throws Exception {
public void fromUriStringQueryParamEncodedAndContainingPlus() {
String httpUrl = "http://localhost:8080/test/print?value=%EA%B0%80+%EB%82%98";
URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl).build(true).toUri();
@@ -207,10 +207,8 @@ public class UriComponentsBuilderTests {
assertEquals("https", UriComponentsBuilder.fromHttpUrl("HTTPS://www.google.com").build().getScheme());
}
@Test(expected = IllegalArgumentException.class) // SPR-10539
public void fromHttpUrlStringInvalidIPv6Host() throws URISyntaxException {
@Test(expected = IllegalArgumentException.class) // SPR-10539
public void fromHttpUrlStringInvalidIPv6Host() {
UriComponentsBuilder.fromHttpUrl("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource").build().encode();
}
@@ -256,7 +254,7 @@ public class UriComponentsBuilderTests {
}
@Test // SPR-12771
public void fromHttpRequestResetsPortBeforeSettingIt() throws Exception {
public void fromHttpRequestResetsPortBeforeSettingIt() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("X-Forwarded-Proto", "https");
request.addHeader("X-Forwarded-Host", "84.198.58.199");
@@ -497,7 +495,7 @@ public class UriComponentsBuilderTests {
}
@Test // SPR-12742
public void fromHttpRequestWithTrailingSlash() throws Exception {
public void fromHttpRequestWithTrailingSlash() {
UriComponents before = UriComponentsBuilder.fromPath("/foo/").build();
UriComponents after = UriComponentsBuilder.newInstance().uriComponents(before).build();
assertEquals("/foo/", after.getPath());
@@ -666,21 +664,21 @@ public class UriComponentsBuilderTests {
}
@Test
public void queryParamWithValueWithEquals() throws Exception {
public void queryParamWithValueWithEquals() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=baz").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar=baz"));
assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo("baz"));
}
@Test
public void queryParamWithoutValueWithEquals() throws Exception {
public void queryParamWithoutValueWithEquals() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar="));
assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo(""));
}
@Test
public void queryParamWithoutValueWithoutEquals() throws Exception {
public void queryParamWithoutValueWithoutEquals() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar"));
@@ -689,7 +687,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void relativeUrls() throws Exception {
public void relativeUrls() {
String baseUrl = "http://example.com";
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toString(),
equalTo(baseUrl + "/foo/../bar"));
@@ -712,7 +710,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void emptySegments() throws Exception {
public void emptySegments() {
String baseUrl = "http://example.com/abc/";
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/y/z").build().toString(),
equalTo("http://example.com/abc/x/y/z"));
@@ -761,7 +759,7 @@ public class UriComponentsBuilderTests {
}
@Test // SPR-11856
public void fromHttpRequestForwardedHeader() throws Exception {
public void fromHttpRequestForwardedHeader() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
request.setScheme("http");
@@ -777,7 +775,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void fromHttpRequestForwardedHeaderQuoted() throws Exception {
public void fromHttpRequestForwardedHeaderQuoted() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=\"https\"; host=\"84.198.58.199\"");
request.setScheme("http");
@@ -793,7 +791,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void fromHttpRequestMultipleForwardedHeader() throws Exception {
public void fromHttpRequestMultipleForwardedHeader() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "host=84.198.58.199;proto=https");
request.addHeader("Forwarded", "proto=ftp; host=1.2.3.4");
@@ -810,7 +808,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void fromHttpRequestMultipleForwardedHeaderComma() throws Exception {
public void fromHttpRequestMultipleForwardedHeaderComma() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "host=84.198.58.199 ;proto=https, proto=ftp; host=1.2.3.4");
request.setScheme("http");
@@ -826,7 +824,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void fromHttpRequestForwardedHeaderWithHostPortAndWithoutServerPort() throws Exception {
public void fromHttpRequestForwardedHeaderWithHostPortAndWithoutServerPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=https; host=84.198.58.199:9090");
request.setScheme("http");
@@ -844,7 +842,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void fromHttpRequestForwardedHeaderWithHostPortAndServerPort() throws Exception {
public void fromHttpRequestForwardedHeaderWithHostPortAndServerPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=https; host=84.198.58.199:9090");
request.setScheme("http");
@@ -863,7 +861,7 @@ public class UriComponentsBuilderTests {
}
@Test
public void fromHttpRequestForwardedHeaderWithoutHostPortAndWithServerPort() throws Exception {
public void fromHttpRequestForwardedHeaderWithoutHostPortAndWithServerPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
request.setScheme("http");
@@ -882,7 +880,7 @@ public class UriComponentsBuilderTests {
}
@Test // SPR-16262
public void fromHttpRequestForwardedHeaderWithProtoAndServerPort() throws Exception {
public void fromHttpRequestForwardedHeaderWithProtoAndServerPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=https");
request.setScheme("http");