Skip to content

Commit

Permalink
Polishing in MockServerHttpRequest
Browse files Browse the repository at this point in the history
Closes gh-25109
  • Loading branch information
midumitrescu authored and rstoyanchev committed May 29, 2020
1 parent 30351c2 commit b31b8ce
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 81 deletions.
Expand Up @@ -44,6 +44,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;

/**
Expand All @@ -55,11 +56,10 @@
*/
public final class MockServerHttpRequest extends AbstractServerHttpRequest {

@Nullable
private final HttpMethod httpMethod;

@Nullable
private final String customHttpMethod;
/**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
*/
private final String httpMethodValue;

private final MultiValueMap<String, HttpCookie> cookies;

Expand All @@ -74,16 +74,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {

private final Flux<DataBuffer> body;


private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String customHttpMethod,
private MockServerHttpRequest(String httpMethodValue,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {

super(uri, contextPath, headers);
Assert.isTrue(httpMethod != null || customHttpMethod != null, "HTTP method must not be null");
this.httpMethod = httpMethod;
this.customHttpMethod = customHttpMethod;
this.httpMethodValue = httpMethodValue;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
Expand All @@ -93,14 +90,15 @@ private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String


@Override
@Nullable
public HttpMethod getMethod() {
return this.httpMethod;
return HttpMethod.resolve(httpMethodValue);
}

@Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() {
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
return httpMethodValue;
}

@Override
Expand Down Expand Up @@ -232,24 +230,23 @@ public static BodyBuilder method(HttpMethod method, URI url) {
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
}

/**
* Create a builder with a raw HTTP method value that is outside the range
* Create a builder with a raw HTTP methodValue value that is outside the range
* of {@link HttpMethod} enum values.
* @param method the HTTP method value
* @param methodValue the HTTP methodValue value
* @param urlTemplate the URL template
* @param vars variables to expand into the template
* @return the created builder
* @throws IllegalArgumentException if methodValue is null, empty String or contains only white characters
* @since 5.2.7
*/
public static BodyBuilder method(String method, String urlTemplate, Object... vars) {
public static BodyBuilder method(String methodValue, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
return new DefaultBodyBuilder(methodValue, url);
}


Expand Down Expand Up @@ -431,12 +428,7 @@ private static class DefaultBodyBuilder implements BodyBuilder {

private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();


@Nullable
private final HttpMethod method;

@Nullable
private final String customMethod;
private final String methodValue;

private final URI url;

Expand All @@ -458,23 +450,18 @@ private static class DefaultBodyBuilder implements BodyBuilder {
@Nullable
private SslInfo sslInfo;


DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.customMethod = null;
protected DefaultBodyBuilder(String methodValue, URI url) {
Assert.isTrue(StringUtils.hasLength(methodValue) &&
StringUtils.hasLength(methodValue.trim()), "HttpMethod is required. " +
"Please initialize it to non empty value");
this.methodValue = methodValue.trim();
this.url = url;
}

DefaultBodyBuilder(String method, URI url) {
HttpMethod resolved = HttpMethod.resolve(method);
if (resolved != null) {
this.method = resolved;
this.customMethod = null;
}
else {
this.method = null;
this.customMethod = method;
}
protected DefaultBodyBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
this.methodValue = method.name();
this.url = url;
}

Expand Down Expand Up @@ -611,7 +598,7 @@ private Charset getCharset() {
@Override
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
applyCookiesIfNecessary();
return new MockServerHttpRequest(this.method, this.customMethod, getUrlToUse(), this.contextPath,
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
}

Expand Down
Expand Up @@ -17,11 +17,18 @@
package org.springframework.mock.http.server.reactive;

import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.util.UriComponentsBuilder;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -56,4 +63,22 @@ void queryParams() throws Exception {
assertThat(request.getURI().toString()).isEqualTo("/foo%20bar?a=b&name%20A=value%20A1&name%20A=value%20A2&name%20B=value%20B1");
}

@ParameterizedTest
@MethodSource("invalidMockServerHttpRequestBuilds")
void httpMethodNotNullOrEmpty(Executable executable) {
IllegalArgumentException expectedIllegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class,
executable);
assertThat(expectedIllegalArgumentException.getMessage()).contains("HttpMethod is required.");
}

static Stream<Executable> invalidMockServerHttpRequestBuilds() {
String uriTemplate = "/foo bar?a=b";
return Stream.of(
() -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build(),
() -> MockServerHttpRequest.method((HttpMethod) null, uriTemplate).build(),
() -> MockServerHttpRequest.method((String) null, uriTemplate).build(),
() -> MockServerHttpRequest.method("", uriTemplate).build(),
() -> MockServerHttpRequest.method(" ", uriTemplate).build()
);
}
}
Expand Up @@ -44,6 +44,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;

/**
Expand All @@ -55,11 +56,10 @@
*/
public final class MockServerHttpRequest extends AbstractServerHttpRequest {

@Nullable
private final HttpMethod httpMethod;

@Nullable
private final String customHttpMethod;
/**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
*/
private final String httpMethodValue;

private final MultiValueMap<String, HttpCookie> cookies;

Expand All @@ -74,16 +74,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {

private final Flux<DataBuffer> body;


private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String customHttpMethod,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
private MockServerHttpRequest(String httpMethodValue,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {

super(uri, contextPath, headers);
Assert.isTrue(httpMethod != null || customHttpMethod != null, "HTTP method must not be null");
this.httpMethod = httpMethod;
this.customHttpMethod = customHttpMethod;
this.httpMethodValue = httpMethodValue;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
Expand All @@ -93,14 +90,15 @@ private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String


@Override
@Nullable
public HttpMethod getMethod() {
return this.httpMethod;
return HttpMethod.resolve(httpMethodValue);
}

@Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() {
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
return httpMethodValue;
}

@Override
Expand Down Expand Up @@ -232,24 +230,23 @@ public static BodyBuilder method(HttpMethod method, URI url) {
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
}

/**
* Create a builder with a raw HTTP method value that is outside the range
* Create a builder with a raw HTTP methodValue value that is outside the range
* of {@link HttpMethod} enum values.
* @param method the HTTP method value
* @param methodValue the HTTP methodValue value
* @param urlTemplate the URL template
* @param vars variables to expand into the template
* @return the created builder
* @throws IllegalArgumentException if methodValue is null, empty String or contains only white characters
* @since 5.2.7
*/
public static BodyBuilder method(String method, String urlTemplate, Object... vars) {
public static BodyBuilder method(String methodValue, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
return new DefaultBodyBuilder(methodValue, url);
}


Expand Down Expand Up @@ -431,12 +428,7 @@ private static class DefaultBodyBuilder implements BodyBuilder {

private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();


@Nullable
private final HttpMethod method;

@Nullable
private final String customMethod;
private final String methodValue;

private final URI url;

Expand All @@ -458,23 +450,18 @@ private static class DefaultBodyBuilder implements BodyBuilder {
@Nullable
private SslInfo sslInfo;


DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.customMethod = null;
protected DefaultBodyBuilder(String methodValue, URI url) {
Assert.isTrue(StringUtils.hasLength(methodValue) &&
StringUtils.hasLength(methodValue.trim()), "HttpMethod is required. " +
"Please initialize it to non empty value");
this.methodValue = methodValue.trim();
this.url = url;
}

DefaultBodyBuilder(String method, URI url) {
HttpMethod resolved = HttpMethod.resolve(method);
if (resolved != null) {
this.method = resolved;
this.customMethod = null;
}
else {
this.method = null;
this.customMethod = method;
}
protected DefaultBodyBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
this.methodValue = method.name();
this.url = url;
}

Expand Down Expand Up @@ -611,7 +598,7 @@ private Charset getCharset() {
@Override
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
applyCookiesIfNecessary();
return new MockServerHttpRequest(this.method, this.customMethod, getUrlToUse(), this.contextPath,
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
}

Expand Down

0 comments on commit b31b8ce

Please sign in to comment.