Skip to content

Commit

Permalink
Polishing in MockServerHttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
midumitrescu authored and kenny5he committed Jun 21, 2020
1 parent 00571ad commit ea9c98c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 104 deletions.
Expand Up @@ -24,7 +24,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

import org.reactivestreams.Publisher;
Expand All @@ -45,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 @@ -56,57 +56,49 @@
*/
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;

@Nullable
private final InetSocketAddress localAddress;
private final InetSocketAddress remoteAddress;

@Nullable
private final InetSocketAddress remoteAddress;
private final InetSocketAddress localAddress;

@Nullable
private final SslInfo sslInfo;

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 localAddress, @Nullable InetSocketAddress remoteAddress,
@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.localAddress = localAddress;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
this.sslInfo = sslInfo;
this.body = Flux.from(body);
}


@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() : Objects.requireNonNull(this.customHttpMethod));
}

@Override
@Nullable
public InetSocketAddress getLocalAddress() {
return this.localAddress;
return httpMethodValue;
}

@Override
Expand All @@ -115,8 +107,14 @@ public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}

@Nullable
@Override
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}

@Nullable
@Override
protected SslInfo initSslInfo() {
return this.sslInfo;
}
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 @@ -382,8 +379,8 @@ public interface BaseBuilder<B extends BaseBuilder<B>> {
* @see BodyBuilder#body(String)
*/
MockServerHttpRequest build();
}

}

/**
* A builder that adds a body to the request.
Expand Down Expand Up @@ -423,18 +420,15 @@ public interface BodyBuilder extends BaseBuilder<BodyBuilder> {
* @return the built request entity
*/
MockServerHttpRequest body(String body);

}


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 @@ -456,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 @@ -609,8 +598,8 @@ private Charset getCharset() {
@Override
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
applyCookiesIfNecessary();
return new MockServerHttpRequest(this.method, this.customMethod, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.localAddress, this.remoteAddress, this.sslInfo, body);
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
}

private void applyCookiesIfNecessary() {
Expand All @@ -623,9 +612,11 @@ private void applyCookiesIfNecessary() {
private URI getUrlToUse() {
MultiValueMap<String, String> params =
this.queryParamsBuilder.buildAndExpand().encode().getQueryParams();

if (!params.isEmpty()) {
return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri();
}

return this.url;
}
}
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()
);
}
}

0 comments on commit ea9c98c

Please sign in to comment.