Deprecated ClientRequest.method in favor of ClientRequest.create

The former method clashed with the ClientRequest.method() getter.
This commit is contained in:
Arjen Poutsma
2018-03-15 10:58:08 +01:00
parent 04c2a2990d
commit b31d55dfce
6 changed files with 60 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -33,7 +33,7 @@ import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.reactive.function.client.ExchangeFunctions;
import static java.time.Duration.ofMillis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Unit tests for {@link WiretapConnector}.
@@ -44,12 +44,12 @@ import static org.junit.Assert.assertEquals;
public class WebTestClientConnectorTests {
@Test
public void captureAndClaim() throws Exception {
public void captureAndClaim() {
ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/test");
ClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));
ClientRequest clientRequest = ClientRequest.method(HttpMethod.GET, URI.create("/test"))
ClientRequest clientRequest = ClientRequest.create(HttpMethod.GET, URI.create("/test"))
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();
WiretapConnector wiretapConnector = new WiretapConnector(connector);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -111,11 +111,7 @@ public interface ClientRequest {
*/
static Builder from(ClientRequest other) {
Assert.notNull(other, "'other' must not be null");
return new DefaultClientRequestBuilder(other.method(), other.url())
.headers(headers -> headers.addAll(other.headers()))
.cookies(cookies -> cookies.addAll(other.cookies()))
.attributes(attributes -> attributes.putAll(other.attributes()))
.body(other.body());
return new DefaultClientRequestBuilder(other);
}
/**
@@ -123,11 +119,23 @@ public interface ClientRequest {
* @param method the HTTP method (GET, POST, etc)
* @param url the URL
* @return the created builder
* @deprecated in favor of {@link #create(HttpMethod, URI)}
*/
@Deprecated
static Builder method(HttpMethod method, URI url) {
return new DefaultClientRequestBuilder(method, url);
}
/**
* Create a request builder with the given method and url.
* @param method the HTTP method (GET, POST, etc)
* @param url the URL
* @return the created builder
*/
static Builder create(HttpMethod method, URI url) {
return new DefaultClientRequestBuilder(method, url);
}
/**
* Defines a builder for a request.
@@ -235,8 +243,8 @@ public interface ClientRequest {
Builder attributes(Consumer<Map<String, Object>> attributesConsumer);
/**
* Builds the request entity with no body.
* @return the request entity
* Builds the request.
* @return the request
*/
ClientRequest build();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -67,6 +67,14 @@ class DefaultClientRequestBuilder implements ClientRequest.Builder {
this.url = url;
}
public DefaultClientRequestBuilder(ClientRequest other) {
this(other.method(), other.url());
headers(headers -> headers.addAll(other.headers()));
cookies(cookies -> cookies.addAll(other.cookies()));
attributes(attributes -> attributes.putAll(other.attributes()));
body(other.body());
}
@Override
public ClientRequest.Builder method(HttpMethod method) {
Assert.notNull(method, "'method' must not be null");

View File

@@ -324,7 +324,7 @@ class DefaultWebClient implements WebClient {
private ClientRequest.Builder initRequestBuilder() {
URI uri = this.uri != null ? this.uri : uriBuilderFactory.expand("");
return ClientRequest.method(this.httpMethod, uri)
return ClientRequest.create(this.httpMethod, uri)
.headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies()))
.attributes(attributes -> attributes.putAll(this.attributes));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -50,7 +50,7 @@ public class DefaultClientRequestBuilderTests {
@Test
public void from() throws Exception {
ClientRequest other = ClientRequest.method(GET, URI.create("http://example.com"))
ClientRequest other = ClientRequest.create(GET, URI.create("http://example.com"))
.header("foo", "bar")
.cookie("baz", "qux").build();
ClientRequest result = ClientRequest.from(other)
@@ -68,7 +68,7 @@ public class DefaultClientRequestBuilderTests {
@Test
public void method() throws Exception {
URI url = new URI("http://example.com");
ClientRequest.Builder builder = ClientRequest.method(DELETE, url);
ClientRequest.Builder builder = ClientRequest.create(DELETE, url);
assertEquals(DELETE, builder.build().method());
builder.method(OPTIONS);
@@ -79,7 +79,7 @@ public class DefaultClientRequestBuilderTests {
public void url() throws Exception {
URI url1 = new URI("http://example.com/foo");
URI url2 = new URI("http://example.com/bar");
ClientRequest.Builder builder = ClientRequest.method(DELETE, url1);
ClientRequest.Builder builder = ClientRequest.create(DELETE, url1);
assertEquals(url1, builder.build().url());
builder.url(url2);
@@ -87,15 +87,15 @@ public class DefaultClientRequestBuilderTests {
}
@Test
public void cookie() throws Exception {
ClientRequest result = ClientRequest.method(GET, URI.create("http://example.com"))
public void cookie() {
ClientRequest result = ClientRequest.create(GET, URI.create("http://example.com"))
.cookie("foo", "bar").build();
assertEquals("bar", result.cookies().getFirst("foo"));
}
@Test
public void build() throws Exception {
ClientRequest result = ClientRequest.method(GET, URI.create("http://example.com"))
public void build() {
ClientRequest result = ClientRequest.create(GET, URI.create("http://example.com"))
.header("MyKey", "MyValue")
.cookie("foo", "bar")
.build();
@@ -111,7 +111,7 @@ public class DefaultClientRequestBuilderTests {
}
@Test
public void bodyInserter() throws Exception {
public void bodyInserter() {
String body = "foo";
BodyInserter<String, ClientHttpRequest> inserter =
(response, strategies) -> {
@@ -121,7 +121,7 @@ public class DefaultClientRequestBuilderTests {
return response.writeWith(Mono.just(buffer));
};
ClientRequest result = ClientRequest.method(POST, URI.create("http://example.com"))
ClientRequest result = ClientRequest.create(POST, URI.create("http://example.com"))
.body(inserter).build();
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
@@ -140,10 +140,10 @@ public class DefaultClientRequestBuilderTests {
}
@Test
public void bodyClass() throws Exception {
public void bodyClass() {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
ClientRequest result = ClientRequest.method(POST, URI.create("http://example.com"))
ClientRequest result = ClientRequest.create(POST, URI.create("http://example.com"))
.body(publisher, String.class).build();
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
@@ -162,11 +162,11 @@ public class DefaultClientRequestBuilderTests {
}
@Test
public void bodyParameterizedTypeReference() throws Exception {
public void bodyParameterizedTypeReference() {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
ClientRequest result = ClientRequest.method(POST, URI.create("http://example.com"))
ClientRequest result = ClientRequest.create(POST, URI.create("http://example.com"))
.body(publisher, typeReference).build();
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -36,8 +36,8 @@ import static org.springframework.web.reactive.function.client.ExchangeFilterFun
public class ExchangeFilterFunctionsTests {
@Test
public void andThen() throws Exception {
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
public void andThen() {
ClientRequest request = ClientRequest.create(GET, URI.create("http://example.com")).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> Mono.just(response);
@@ -66,8 +66,8 @@ public class ExchangeFilterFunctionsTests {
}
@Test
public void apply() throws Exception {
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
public void apply() {
ClientRequest request = ClientRequest.create(GET, URI.create("http://example.com")).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> Mono.just(response);
@@ -85,8 +85,8 @@ public class ExchangeFilterFunctionsTests {
}
@Test
public void basicAuthenticationUsernamePassword() throws Exception {
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
public void basicAuthenticationUsernamePassword() {
ClientRequest request = ClientRequest.create(GET, URI.create("http://example.com")).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> {
@@ -102,14 +102,14 @@ public class ExchangeFilterFunctionsTests {
}
@Test(expected = IllegalArgumentException.class)
public void basicAuthenticationInvalidCharacters() throws Exception {
public void basicAuthenticationInvalidCharacters() {
ExchangeFilterFunctions.basicAuthentication("foo", "\ud83d\udca9");
}
@Test
public void basicAuthenticationAttributes() throws Exception {
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com"))
public void basicAuthenticationAttributes() {
ClientRequest request = ClientRequest.create(GET, URI.create("http://example.com"))
.attributes(basicAuthenticationCredentials("foo", "bar"))
.build();
ClientResponse response = mock(ClientResponse.class);
@@ -127,8 +127,8 @@ public class ExchangeFilterFunctionsTests {
}
@Test
public void basicAuthenticationAbsentAttributes() throws Exception {
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
public void basicAuthenticationAbsentAttributes() {
ClientRequest request = ClientRequest.create(GET, URI.create("http://example.com")).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> {
@@ -143,8 +143,8 @@ public class ExchangeFilterFunctionsTests {
}
@Test
public void statusHandlerMatch() throws Exception {
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
public void statusHandlerMatch() {
ClientRequest request = ClientRequest.create(GET, URI.create("http://example.com")).build();
ClientResponse response = mock(ClientResponse.class);
when(response.statusCode()).thenReturn(HttpStatus.NOT_FOUND);
@@ -161,8 +161,8 @@ public class ExchangeFilterFunctionsTests {
}
@Test
public void statusHandlerNoMatch() throws Exception {
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
public void statusHandlerNoMatch() {
ClientRequest request = ClientRequest.create(GET, URI.create("http://example.com")).build();
ClientResponse response = mock(ClientResponse.class);
when(response.statusCode()).thenReturn(HttpStatus.NOT_FOUND);