Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring for MockServerHttpRequest #25148

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still can assert the httpMethodValue is not null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rstoyanchev I added some easy tests, testing all possible build paths.

Should I anyways add the assertion here?

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,17 @@ 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()), "HTTP methodValue must not be empty");
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 +597,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