Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed May 29, 2020
1 parent b31b8ce commit 8d44947
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 68 deletions.
Expand Up @@ -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>).
*/
private final String httpMethodValue;
private final String httpMethod;

private final MultiValueMap<String, HttpCookie> cookies;

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

private final Flux<DataBuffer> body;

private MockServerHttpRequest(String httpMethodValue,
private MockServerHttpRequest(String httpMethod,
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);
this.httpMethodValue = httpMethodValue;
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
this.httpMethod = httpMethod;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
Expand All @@ -92,13 +93,12 @@ private MockServerHttpRequest(String httpMethodValue,
@Override
@Nullable
public HttpMethod getMethod() {
return HttpMethod.resolve(httpMethodValue);
return HttpMethod.resolve(this.httpMethod);
}

@Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() {
return httpMethodValue;
return this.httpMethod;
}

@Override
Expand Down Expand Up @@ -217,36 +217,39 @@ public static BaseBuilder<?> options(String urlTemplate, Object... uriVars) {
* @return the created builder
*/
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);
}

/**
* Alternative to {@link #method(HttpMethod, URI)} that accepts a URI template.
* The given URI may contain query parameters, or those may be added later via
* {@link BaseBuilder#queryParam queryParam} builder methods.
* @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
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
public static BodyBuilder method(HttpMethod method, String uri, Object... vars) {
return method(method, toUri(uri, vars));
}

/**
* Create a builder with a raw HTTP methodValue value that is outside the range
* of {@link HttpMethod} enum values.
* @param methodValue the HTTP methodValue value
* @param urlTemplate the URL template
* Create a builder with a raw HTTP method value value that is outside the
* range of {@link HttpMethod} enum values.
* @param httpMethod the HTTP methodValue value
* @param uri the URI template for target the URL
* @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 methodValue, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(methodValue, url);
public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
}

private static URI toUri(String uri, Object[] vars) {
return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
}


Expand Down Expand Up @@ -450,18 +453,13 @@ private static class DefaultBodyBuilder implements BodyBuilder {
@Nullable
private SslInfo sslInfo;

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();
DefaultBodyBuilder(String method, URI url) {
this.methodValue = method;
this.url = url;
}

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();
DefaultBodyBuilder(HttpMethod httpMethod, URI url) {
this.methodValue = httpMethod.name();
this.url = url;
}

Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -21,10 +21,10 @@

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;
Expand All @@ -39,7 +39,7 @@
class MockServerHttpRequestTests {

@Test
void cookieHeaderSet() throws Exception {
void cookieHeaderSet() {
HttpCookie foo11 = new HttpCookie("foo1", "bar1");
HttpCookie foo12 = new HttpCookie("foo1", "bar2");
HttpCookie foo21 = new HttpCookie("foo2", "baz1");
Expand All @@ -54,7 +54,7 @@ void cookieHeaderSet() throws Exception {
}

@Test
void queryParams() throws Exception {
void queryParams() {
MockServerHttpRequest request = MockServerHttpRequest.get("/foo bar?a=b")
.queryParam("name A", "value A1", "value A2")
.queryParam("name B", "value B1")
Expand All @@ -64,14 +64,13 @@ void queryParams() throws Exception {
}

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

static Stream<Executable> invalidMockServerHttpRequestBuilds() {
static Stream<Executable> httpMethodNotNullOrEmpty() {
String uriTemplate = "/foo bar?a=b";
return Stream.of(
() -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build(),
Expand Down
Expand Up @@ -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>).
*/
private final String httpMethodValue;
private final String httpMethod;

private final MultiValueMap<String, HttpCookie> cookies;

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

private final Flux<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) {
private MockServerHttpRequest(String httpMethod,
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);
this.httpMethodValue = httpMethodValue;
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
this.httpMethod = httpMethod;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
Expand All @@ -92,13 +93,12 @@ private MockServerHttpRequest(String httpMethodValue,
@Override
@Nullable
public HttpMethod getMethod() {
return HttpMethod.resolve(httpMethodValue);
return HttpMethod.resolve(this.httpMethod);
}

@Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() {
return httpMethodValue;
return this.httpMethod;
}

@Override
Expand Down Expand Up @@ -217,36 +217,39 @@ public static BaseBuilder<?> options(String urlTemplate, Object... uriVars) {
* @return the created builder
*/
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);
}

/**
* Alternative to {@link #method(HttpMethod, URI)} that accepts a URI template.
* The given URI may contain query parameters, or those may be added later via
* {@link BaseBuilder#queryParam queryParam} builder methods.
* @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
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
public static BodyBuilder method(HttpMethod method, String uri, Object... vars) {
return method(method, toUri(uri, vars));
}

/**
* Create a builder with a raw HTTP methodValue value that is outside the range
* of {@link HttpMethod} enum values.
* @param methodValue the HTTP methodValue value
* @param urlTemplate the URL template
* Create a builder with a raw HTTP method value value that is outside the
* range of {@link HttpMethod} enum values.
* @param httpMethod the HTTP methodValue value
* @param uri the URI template for target the URL
* @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 methodValue, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(methodValue, url);
public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
}

private static URI toUri(String uri, Object[] vars) {
return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
}


Expand Down Expand Up @@ -450,18 +453,13 @@ private static class DefaultBodyBuilder implements BodyBuilder {
@Nullable
private SslInfo sslInfo;

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();
DefaultBodyBuilder(String method, URI url) {
this.methodValue = method;
this.url = url;
}

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();
DefaultBodyBuilder(HttpMethod httpMethod, URI url) {
this.methodValue = httpMethod.name();
this.url = url;
}

Expand Down

0 comments on commit 8d44947

Please sign in to comment.