Merge branch '5.2.x'
# Conflicts: # build.gradle # spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java # spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java # spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java
This commit is contained in:
commit
a34f1e3759
|
@ -151,7 +151,7 @@ configure(allprojects) { project ->
|
|||
dependency("org.apache.httpcomponents:httpasyncclient:4.1.4") {
|
||||
exclude group: "commons-logging", name: "commons-logging"
|
||||
}
|
||||
dependency "org.eclipse.jetty:jetty-reactive-httpclient:1.1.2"
|
||||
dependency "org.eclipse.jetty:jetty-reactive-httpclient:1.1.3"
|
||||
dependency 'org.apache.httpcomponents.client5:httpclient5:5.0'
|
||||
dependency 'org.apache.httpcomponents.core5:httpcore5-reactive:5.0'
|
||||
|
||||
|
|
|
@ -76,15 +76,15 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
|
||||
private MockServerHttpRequest(String httpMethod,
|
||||
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
||||
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
|
||||
@Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress,
|
||||
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
|
||||
|
||||
super(uri, contextPath, headers);
|
||||
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
|
||||
this.httpMethod = httpMethod;
|
||||
this.cookies = cookies;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.localAddress = localAddress;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.sslInfo = sslInfo;
|
||||
this.body = Flux.from(body);
|
||||
}
|
||||
|
@ -382,9 +382,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
* @see BodyBuilder#body(String)
|
||||
*/
|
||||
MockServerHttpRequest build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A builder that adds a body to the request.
|
||||
*/
|
||||
|
@ -423,7 +423,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
* @return the built request entity
|
||||
*/
|
||||
MockServerHttpRequest body(String body);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -597,7 +596,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||
applyCookiesIfNecessary();
|
||||
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
|
||||
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
|
||||
this.headers, this.cookies, this.localAddress, this.remoteAddress, this.sslInfo, body);
|
||||
}
|
||||
|
||||
private void applyCookiesIfNecessary() {
|
||||
|
@ -610,11 +609,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,6 +64,8 @@ import org.springframework.web.util.UriTemplateHandler;
|
|||
* @param <T> the body type
|
||||
* @see #getMethod()
|
||||
* @see #getUrl()
|
||||
* @see org.springframework.web.client.RestOperations#exchange(RequestEntity, Class)
|
||||
* @see ResponseEntity
|
||||
*/
|
||||
public class RequestEntity<T> extends HttpEntity<T> {
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
@ -70,6 +70,10 @@ import org.springframework.util.ObjectUtils;
|
|||
* @since 3.0.2
|
||||
* @param <T> the body type
|
||||
* @see #getStatusCode()
|
||||
* @see org.springframework.web.client.RestOperations#getForEntity(String, Class, Object...)
|
||||
* @see org.springframework.web.client.RestOperations#getForEntity(String, Class, java.util.Map)
|
||||
* @see org.springframework.web.client.RestOperations#getForEntity(URI, Class)
|
||||
* @see RequestEntity
|
||||
*/
|
||||
public class ResponseEntity<T> extends HttpEntity<T> {
|
||||
|
||||
|
@ -217,19 +221,6 @@ public class ResponseEntity<T> extends HttpEntity<T> {
|
|||
return new DefaultBuilder(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* A shortcut for creating a {@code ResponseEntity} with the given body
|
||||
* and the {@linkplain HttpStatus#OK OK} status, or an empty body and a
|
||||
* {@linkplain HttpStatus#NOT_FOUND NOT FOUND} status in case of an
|
||||
* {@linkplain Optional#empty()} parameter.
|
||||
* @return the created {@code ResponseEntity}
|
||||
* @since 5.1
|
||||
*/
|
||||
public static <T> ResponseEntity<T> of(Optional<T> body) {
|
||||
Assert.notNull(body, "Body must not be null");
|
||||
return body.map(ResponseEntity::ok).orElse(notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder with the status set to {@linkplain HttpStatus#OK OK}.
|
||||
* @return the created builder
|
||||
|
@ -246,8 +237,20 @@ public class ResponseEntity<T> extends HttpEntity<T> {
|
|||
* @since 4.1
|
||||
*/
|
||||
public static <T> ResponseEntity<T> ok(T body) {
|
||||
BodyBuilder builder = ok();
|
||||
return builder.body(body);
|
||||
return ok().body(body);
|
||||
}
|
||||
|
||||
/**
|
||||
* A shortcut for creating a {@code ResponseEntity} with the given body
|
||||
* and the {@linkplain HttpStatus#OK OK} status, or an empty body and a
|
||||
* {@linkplain HttpStatus#NOT_FOUND NOT FOUND} status in case of an
|
||||
* {@linkplain Optional#empty()} parameter.
|
||||
* @return the created {@code ResponseEntity}
|
||||
* @since 5.1
|
||||
*/
|
||||
public static <T> ResponseEntity<T> of(Optional<T> body) {
|
||||
Assert.notNull(body, "Body must not be null");
|
||||
return body.map(ResponseEntity::ok).orElseGet(() -> notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -258,8 +261,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
|
|||
* @since 4.1
|
||||
*/
|
||||
public static BodyBuilder created(URI location) {
|
||||
BodyBuilder builder = status(HttpStatus.CREATED);
|
||||
return builder.location(location);
|
||||
return status(HttpStatus.CREATED).location(location);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -76,15 +76,15 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
|
||||
private MockServerHttpRequest(String httpMethod,
|
||||
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
||||
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
|
||||
@Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress,
|
||||
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
|
||||
|
||||
super(uri, contextPath, headers);
|
||||
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
|
||||
this.httpMethod = httpMethod;
|
||||
this.cookies = cookies;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.localAddress = localAddress;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.sslInfo = sslInfo;
|
||||
this.body = Flux.from(body);
|
||||
}
|
||||
|
@ -382,9 +382,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
* @see BodyBuilder#body(String)
|
||||
*/
|
||||
MockServerHttpRequest build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A builder that adds a body to the request.
|
||||
*/
|
||||
|
@ -423,7 +423,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
* @return the built request entity
|
||||
*/
|
||||
MockServerHttpRequest body(String body);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -597,7 +596,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||
applyCookiesIfNecessary();
|
||||
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
|
||||
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
|
||||
this.headers, this.cookies, this.localAddress, this.remoteAddress, this.sslInfo, body);
|
||||
}
|
||||
|
||||
private void applyCookiesIfNecessary() {
|
||||
|
@ -610,11 +609,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -273,6 +273,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
|||
ServerCodecConfigurer serverCodecConfigurer,
|
||||
@Qualifier("webFluxConversionService") FormattingConversionService conversionService,
|
||||
@Qualifier("webFluxValidator") Validator validator) {
|
||||
|
||||
RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
|
||||
adapter.setMessageReaders(serverCodecConfigurer.getReaders());
|
||||
adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer(conversionService, validator));
|
||||
|
|
|
@ -417,26 +417,23 @@ class DefaultWebClient implements WebClient {
|
|||
|
||||
private static class DefaultResponseSpec implements ResponseSpec {
|
||||
|
||||
private static final IntPredicate STATUS_CODE_ERROR = value -> value >= 400;
|
||||
private static final IntPredicate STATUS_CODE_ERROR = (value -> value >= 400);
|
||||
|
||||
private static final StatusHandler DEFAULT_STATUS_HANDLER =
|
||||
new StatusHandler(STATUS_CODE_ERROR, ClientResponse::createException);
|
||||
|
||||
|
||||
private final Mono<ClientResponse> responseMono;
|
||||
|
||||
private final Supplier<HttpRequest> requestSupplier;
|
||||
|
||||
private final List<StatusHandler> statusHandlers = new ArrayList<>(1);
|
||||
|
||||
|
||||
DefaultResponseSpec(Mono<ClientResponse> responseMono, Supplier<HttpRequest> requestSupplier) {
|
||||
this.responseMono = responseMono;
|
||||
this.requestSupplier = requestSupplier;
|
||||
this.statusHandlers.add(DEFAULT_STATUS_HANDLER);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
|
||||
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
|
||||
|
|
|
@ -106,6 +106,7 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
|||
this.defaultUriVariables = (other.defaultUriVariables != null ?
|
||||
new LinkedHashMap<>(other.defaultUriVariables) : null);
|
||||
this.uriBuilderFactory = other.uriBuilderFactory;
|
||||
|
||||
if (other.defaultHeaders != null) {
|
||||
this.defaultHeaders = new HttpHeaders();
|
||||
this.defaultHeaders.putAll(other.defaultHeaders);
|
||||
|
@ -113,13 +114,16 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
|||
else {
|
||||
this.defaultHeaders = null;
|
||||
}
|
||||
|
||||
this.defaultCookies = (other.defaultCookies != null ?
|
||||
new LinkedMultiValueMap<>(other.defaultCookies) : null);
|
||||
this.defaultRequest = other.defaultRequest;
|
||||
this.filters = other.filters != null ? new ArrayList<>(other.filters) : null;
|
||||
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
|
||||
|
||||
this.connector = other.connector;
|
||||
this.strategies = other.strategies;
|
||||
this.strategiesConfigurers = other.strategiesConfigurers != null ? new ArrayList<>(other.strategiesConfigurers) : null;
|
||||
this.strategiesConfigurers = (other.strategiesConfigurers != null ?
|
||||
new ArrayList<>(other.strategiesConfigurers) : null);
|
||||
this.exchangeFunction = other.exchangeFunction;
|
||||
}
|
||||
|
||||
|
@ -288,10 +292,10 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
|||
|
||||
private ExchangeStrategies initExchangeStrategies() {
|
||||
if (CollectionUtils.isEmpty(this.strategiesConfigurers)) {
|
||||
return this.strategies != null ? this.strategies : ExchangeStrategies.withDefaults();
|
||||
return (this.strategies != null ? this.strategies : ExchangeStrategies.withDefaults());
|
||||
}
|
||||
ExchangeStrategies.Builder builder =
|
||||
this.strategies != null ? this.strategies.mutate() : ExchangeStrategies.builder();
|
||||
(this.strategies != null ? this.strategies.mutate() : ExchangeStrategies.builder());
|
||||
this.strategiesConfigurers.forEach(configurer -> configurer.accept(builder));
|
||||
return builder.build();
|
||||
}
|
||||
|
@ -300,8 +304,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
|||
if (this.uriBuilderFactory != null) {
|
||||
return this.uriBuilderFactory;
|
||||
}
|
||||
DefaultUriBuilderFactory factory = this.baseUrl != null ?
|
||||
new DefaultUriBuilderFactory(this.baseUrl) : new DefaultUriBuilderFactory();
|
||||
DefaultUriBuilderFactory factory = (this.baseUrl != null ?
|
||||
new DefaultUriBuilderFactory(this.baseUrl) : new DefaultUriBuilderFactory());
|
||||
factory.setDefaultUriVariables(this.defaultUriVariables);
|
||||
return factory;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue