Polishing contribution

See gh-25148
This commit is contained in:
Rossen Stoyanchev 2020-05-29 17:37:43 +01:00
parent b31b8ce6c1
commit 8d449471c7
3 changed files with 63 additions and 68 deletions

View File

@ -59,7 +59,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
/** /**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>). * String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
*/ */
private final String httpMethodValue; private final String httpMethod;
private final MultiValueMap<String, HttpCookie> cookies; private final MultiValueMap<String, HttpCookie> cookies;
@ -74,13 +74,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final Flux<DataBuffer> body; private final Flux<DataBuffer> body;
private MockServerHttpRequest(String httpMethodValue, private MockServerHttpRequest(String httpMethod,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies, URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) { @Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
super(uri, contextPath, headers); super(uri, contextPath, headers);
this.httpMethodValue = httpMethodValue; Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
this.httpMethod = httpMethod;
this.cookies = cookies; this.cookies = cookies;
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
this.localAddress = localAddress; this.localAddress = localAddress;
@ -92,13 +93,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override @Override
@Nullable @Nullable
public HttpMethod getMethod() { public HttpMethod getMethod() {
return HttpMethod.resolve(httpMethodValue); return HttpMethod.resolve(this.httpMethod);
} }
@Override @Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() { public String getMethodValue() {
return httpMethodValue; return this.httpMethod;
} }
@Override @Override
@ -217,7 +217,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* @return the created builder * @return the created builder
*/ */
public static BodyBuilder method(HttpMethod method, URI url) { public static BodyBuilder method(HttpMethod method, URI url) {
return new DefaultBodyBuilder(method, url); Assert.notNull(method, "HTTP method is required. " +
"For a custom HTTP method, please provide a String HTTP method value.");
return new DefaultBodyBuilder(method.name(), url);
} }
/** /**
@ -225,28 +227,29 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* The given URI may contain query parameters, or those may be added later via * The given URI may contain query parameters, or those may be added later via
* {@link BaseBuilder#queryParam queryParam} builder methods. * {@link BaseBuilder#queryParam queryParam} builder methods.
* @param method the HTTP method (GET, POST, etc) * @param method the HTTP method (GET, POST, etc)
* @param urlTemplate the URL template * @param uri the URI template for the target URL
* @param vars variables to expand into the template * @param vars variables to expand into the template
* @return the created builder * @return the created builder
*/ */
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) { public static BodyBuilder method(HttpMethod method, String uri, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri(); return method(method, toUri(uri, vars));
return new DefaultBodyBuilder(method, url);
} }
/** /**
* Create a builder with a raw HTTP methodValue value that is outside the range * Create a builder with a raw HTTP method value value that is outside the
* of {@link HttpMethod} enum values. * range of {@link HttpMethod} enum values.
* @param methodValue the HTTP methodValue value * @param httpMethod the HTTP methodValue value
* @param urlTemplate the URL template * @param uri the URI template for target the URL
* @param vars variables to expand into the template * @param vars variables to expand into the template
* @return the created builder * @return the created builder
* @throws IllegalArgumentException if methodValue is null, empty String or contains only white characters
* @since 5.2.7 * @since 5.2.7
*/ */
public static BodyBuilder method(String methodValue, String urlTemplate, Object... vars) { public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri(); return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
return new DefaultBodyBuilder(methodValue, url); }
private static URI toUri(String uri, Object[] vars) {
return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
} }
@ -450,18 +453,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable @Nullable
private SslInfo sslInfo; private SslInfo sslInfo;
protected DefaultBodyBuilder(String methodValue, URI url) { DefaultBodyBuilder(String method, URI url) {
Assert.isTrue(StringUtils.hasLength(methodValue) && this.methodValue = method;
StringUtils.hasLength(methodValue.trim()), "HttpMethod is required. " +
"Please initialize it to non empty value");
this.methodValue = methodValue.trim();
this.url = url; this.url = url;
} }
protected DefaultBodyBuilder(HttpMethod method, URI url) { DefaultBodyBuilder(HttpMethod httpMethod, URI url) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " + this.methodValue = httpMethod.name();
"please use the variant that accepts a String based HTTP method.");
this.methodValue = method.name();
this.url = url; this.url = url;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 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.
@ -21,10 +21,10 @@ import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpCookie; import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class MockServerHttpRequestTests { class MockServerHttpRequestTests {
@Test @Test
void cookieHeaderSet() throws Exception { void cookieHeaderSet() {
HttpCookie foo11 = new HttpCookie("foo1", "bar1"); HttpCookie foo11 = new HttpCookie("foo1", "bar1");
HttpCookie foo12 = new HttpCookie("foo1", "bar2"); HttpCookie foo12 = new HttpCookie("foo1", "bar2");
HttpCookie foo21 = new HttpCookie("foo2", "baz1"); HttpCookie foo21 = new HttpCookie("foo2", "baz1");
@ -54,7 +54,7 @@ class MockServerHttpRequestTests {
} }
@Test @Test
void queryParams() throws Exception { void queryParams() {
MockServerHttpRequest request = MockServerHttpRequest.get("/foo bar?a=b") MockServerHttpRequest request = MockServerHttpRequest.get("/foo bar?a=b")
.queryParam("name A", "value A1", "value A2") .queryParam("name A", "value A1", "value A2")
.queryParam("name B", "value B1") .queryParam("name B", "value B1")
@ -64,14 +64,13 @@ class MockServerHttpRequestTests {
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("invalidMockServerHttpRequestBuilds") @MethodSource
void httpMethodNotNullOrEmpty(Executable executable) { void httpMethodNotNullOrEmpty(Executable executable) {
IllegalArgumentException expectedIllegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class, Exception ex = Assertions.assertThrows(IllegalArgumentException.class, executable);
executable); assertThat(ex.getMessage()).contains("HTTP method is required.");
assertThat(expectedIllegalArgumentException.getMessage()).contains("HttpMethod is required.");
} }
static Stream<Executable> invalidMockServerHttpRequestBuilds() { static Stream<Executable> httpMethodNotNullOrEmpty() {
String uriTemplate = "/foo bar?a=b"; String uriTemplate = "/foo bar?a=b";
return Stream.of( return Stream.of(
() -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build(), () -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build(),

View File

@ -59,7 +59,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
/** /**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>). * String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
*/ */
private final String httpMethodValue; private final String httpMethod;
private final MultiValueMap<String, HttpCookie> cookies; private final MultiValueMap<String, HttpCookie> cookies;
@ -74,13 +74,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final Flux<DataBuffer> body; private final Flux<DataBuffer> body;
private MockServerHttpRequest(String httpMethodValue, private MockServerHttpRequest(String httpMethod,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies, URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) { @Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
super(uri, contextPath, headers); super(uri, contextPath, headers);
this.httpMethodValue = httpMethodValue; Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
this.httpMethod = httpMethod;
this.cookies = cookies; this.cookies = cookies;
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
this.localAddress = localAddress; this.localAddress = localAddress;
@ -92,13 +93,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override @Override
@Nullable @Nullable
public HttpMethod getMethod() { public HttpMethod getMethod() {
return HttpMethod.resolve(httpMethodValue); return HttpMethod.resolve(this.httpMethod);
} }
@Override @Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() { public String getMethodValue() {
return httpMethodValue; return this.httpMethod;
} }
@Override @Override
@ -217,7 +217,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* @return the created builder * @return the created builder
*/ */
public static BodyBuilder method(HttpMethod method, URI url) { public static BodyBuilder method(HttpMethod method, URI url) {
return new DefaultBodyBuilder(method, url); Assert.notNull(method, "HTTP method is required. " +
"For a custom HTTP method, please provide a String HTTP method value.");
return new DefaultBodyBuilder(method.name(), url);
} }
/** /**
@ -225,28 +227,29 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* The given URI may contain query parameters, or those may be added later via * The given URI may contain query parameters, or those may be added later via
* {@link BaseBuilder#queryParam queryParam} builder methods. * {@link BaseBuilder#queryParam queryParam} builder methods.
* @param method the HTTP method (GET, POST, etc) * @param method the HTTP method (GET, POST, etc)
* @param urlTemplate the URL template * @param uri the URI template for the target URL
* @param vars variables to expand into the template * @param vars variables to expand into the template
* @return the created builder * @return the created builder
*/ */
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) { public static BodyBuilder method(HttpMethod method, String uri, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri(); return method(method, toUri(uri, vars));
return new DefaultBodyBuilder(method, url);
} }
/** /**
* Create a builder with a raw HTTP methodValue value that is outside the range * Create a builder with a raw HTTP method value value that is outside the
* of {@link HttpMethod} enum values. * range of {@link HttpMethod} enum values.
* @param methodValue the HTTP methodValue value * @param httpMethod the HTTP methodValue value
* @param urlTemplate the URL template * @param uri the URI template for target the URL
* @param vars variables to expand into the template * @param vars variables to expand into the template
* @return the created builder * @return the created builder
* @throws IllegalArgumentException if methodValue is null, empty String or contains only white characters
* @since 5.2.7 * @since 5.2.7
*/ */
public static BodyBuilder method(String methodValue, String urlTemplate, Object... vars) { public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri(); return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
return new DefaultBodyBuilder(methodValue, url); }
private static URI toUri(String uri, Object[] vars) {
return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
} }
@ -450,18 +453,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable @Nullable
private SslInfo sslInfo; private SslInfo sslInfo;
protected DefaultBodyBuilder(String methodValue, URI url) { DefaultBodyBuilder(String method, URI url) {
Assert.isTrue(StringUtils.hasLength(methodValue) && this.methodValue = method;
StringUtils.hasLength(methodValue.trim()), "HttpMethod is required. " +
"Please initialize it to non empty value");
this.methodValue = methodValue.trim();
this.url = url; this.url = url;
} }
protected DefaultBodyBuilder(HttpMethod method, URI url) { DefaultBodyBuilder(HttpMethod httpMethod, URI url) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " + this.methodValue = httpMethod.name();
"please use the variant that accepts a String based HTTP method.");
this.methodValue = method.name();
this.url = url; this.url = url;
} }