Merge branch '5.2.x'
This commit is contained in:
commit
b5d9f27ca4
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -40,6 +40,7 @@ import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.server.reactive.AbstractServerHttpRequest;
|
import org.springframework.http.server.reactive.AbstractServerHttpRequest;
|
||||||
import org.springframework.http.server.reactive.SslInfo;
|
import org.springframework.http.server.reactive.SslInfo;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MimeType;
|
import org.springframework.util.MimeType;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
@ -54,8 +55,12 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||||
*/
|
*/
|
||||||
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private final HttpMethod httpMethod;
|
private final HttpMethod httpMethod;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final String customHttpMethod;
|
||||||
|
|
||||||
private final MultiValueMap<String, HttpCookie> cookies;
|
private final MultiValueMap<String, HttpCookie> cookies;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -70,13 +75,15 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
private final Flux<DataBuffer> body;
|
private final Flux<DataBuffer> body;
|
||||||
|
|
||||||
|
|
||||||
private MockServerHttpRequest(HttpMethod httpMethod, URI uri, @Nullable String contextPath,
|
private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String customHttpMethod,
|
||||||
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
||||||
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
|
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
|
||||||
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
|
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
|
||||||
|
|
||||||
super(uri, contextPath, headers);
|
super(uri, contextPath, headers);
|
||||||
|
Assert.isTrue(httpMethod != null || customHttpMethod != null, "HTTP method must not be null");
|
||||||
this.httpMethod = httpMethod;
|
this.httpMethod = httpMethod;
|
||||||
|
this.customHttpMethod = customHttpMethod;
|
||||||
this.cookies = cookies;
|
this.cookies = cookies;
|
||||||
this.remoteAddress = remoteAddress;
|
this.remoteAddress = remoteAddress;
|
||||||
this.localAddress = localAddress;
|
this.localAddress = localAddress;
|
||||||
|
|
@ -91,8 +98,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
public String getMethodValue() {
|
public String getMethodValue() {
|
||||||
return this.httpMethod.name();
|
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -131,30 +139,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
|
|
||||||
// Static builder methods
|
// Static builder methods
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a builder with the given HTTP method and a {@link URI}.
|
|
||||||
* @param method the HTTP method (GET, POST, etc)
|
|
||||||
* @param url the URL
|
|
||||||
* @return the created builder
|
|
||||||
*/
|
|
||||||
public static BodyBuilder method(HttpMethod method, URI url) {
|
|
||||||
return new DefaultBodyBuilder(method, 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 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an HTTP GET builder with the given URI template. The given URI may
|
* Create an HTTP GET builder with the given URI template. The given URI may
|
||||||
* contain query parameters, or those may be added later via
|
* contain query parameters, or those may be added later via
|
||||||
|
|
@ -228,6 +212,44 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
return method(HttpMethod.OPTIONS, urlTemplate, uriVars);
|
return method(HttpMethod.OPTIONS, urlTemplate, uriVars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a builder with the given HTTP method and a {@link URI}.
|
||||||
|
* @param method the HTTP method (GET, POST, etc)
|
||||||
|
* @param url the URL
|
||||||
|
* @return the created builder
|
||||||
|
*/
|
||||||
|
public static BodyBuilder method(HttpMethod method, URI url) {
|
||||||
|
return new DefaultBodyBuilder(method, 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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a builder with a raw HTTP method value that is outside the range
|
||||||
|
* of {@link HttpMethod} enum values.
|
||||||
|
* @param method the HTTP method value
|
||||||
|
* @param urlTemplate the URL template
|
||||||
|
* @param vars variables to expand into the template
|
||||||
|
* @return the created builder
|
||||||
|
* @since 5.2.7
|
||||||
|
*/
|
||||||
|
public static BodyBuilder method(String method, String urlTemplate, Object... vars) {
|
||||||
|
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
|
||||||
|
return new DefaultBodyBuilder(method, url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request builder exposing properties not related to the body.
|
* Request builder exposing properties not related to the body.
|
||||||
|
|
@ -408,8 +430,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
|
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
|
||||||
|
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private final HttpMethod method;
|
private final HttpMethod method;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final String customMethod;
|
||||||
|
|
||||||
private final URI url;
|
private final URI url;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -431,8 +457,22 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
private SslInfo sslInfo;
|
private SslInfo sslInfo;
|
||||||
|
|
||||||
|
|
||||||
public DefaultBodyBuilder(HttpMethod method, URI url) {
|
DefaultBodyBuilder(HttpMethod method, URI url) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
|
this.customMethod = null;
|
||||||
|
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;
|
||||||
|
}
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -569,7 +609,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
@Override
|
@Override
|
||||||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||||
applyCookiesIfNecessary();
|
applyCookiesIfNecessary();
|
||||||
return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath,
|
return new MockServerHttpRequest(this.method, this.customMethod, getUrlToUse(), this.contextPath,
|
||||||
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
|
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -19,6 +19,7 @@ package org.springframework.http.client;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
|
@ -66,6 +67,9 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||||
|
|
||||||
private boolean bufferRequestBody = true;
|
private boolean bufferRequestBody = true;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private BiFunction<HttpMethod, URI, HttpContext> httpContextFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
|
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
|
||||||
|
|
@ -157,6 +161,19 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||||
this.bufferRequestBody = bufferRequestBody;
|
this.bufferRequestBody = bufferRequestBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure a factory to pre-create the {@link HttpContext} for each request.
|
||||||
|
* <p>This may be useful for example in mutual TLS authentication where a
|
||||||
|
* different {@code RestTemplate} for each client certificate such that
|
||||||
|
* all calls made through a given {@code RestTemplate} instance as associated
|
||||||
|
* for the same client identity. {@link HttpClientContext#setUserToken(Object)}
|
||||||
|
* can be used to specify a fixed user token for all requests.
|
||||||
|
* @param httpContextFactory the context factory to use
|
||||||
|
* @since 5.2.7
|
||||||
|
*/
|
||||||
|
public void setHttpContextFactory(BiFunction<HttpMethod, URI, HttpContext> httpContextFactory) {
|
||||||
|
this.httpContextFactory = httpContextFactory;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
|
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
|
||||||
|
|
@ -296,7 +313,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
|
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
|
||||||
return null;
|
return (this.httpContextFactory != null ? this.httpContextFactory.apply(httpMethod, uri) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,14 @@ import org.springframework.util.ObjectUtils;
|
||||||
/**
|
/**
|
||||||
* Spring's default implementation of the {@link ResponseErrorHandler} interface.
|
* Spring's default implementation of the {@link ResponseErrorHandler} interface.
|
||||||
*
|
*
|
||||||
* <p>This error handler checks for the status code on the {@link ClientHttpResponse}:
|
* <p>This error handler checks for the status code on the
|
||||||
* Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}
|
* {@link ClientHttpResponse}. Any code in the 4xx or 5xx series is considered
|
||||||
* or {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be
|
* to be an error. This behavior can be changed by overriding
|
||||||
* an error; this behavior can be changed by overriding the {@link #hasError(HttpStatus)}
|
* {@link #hasError(HttpStatus)}. Unknown status codes will be ignored by
|
||||||
* method. Unknown status codes will be ignored by {@link #hasError(ClientHttpResponse)}.
|
* {@link #hasError(ClientHttpResponse)}.
|
||||||
|
*
|
||||||
|
* <p>See {@link #handleError(ClientHttpResponse)} for more details on specific
|
||||||
|
* exception types.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
|
@ -93,8 +96,18 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
|
* Handle the error in the given response with the given resolved status code.
|
||||||
* response status code.
|
* <p>The default implementation throws:
|
||||||
|
* <ul>
|
||||||
|
* <li>{@link HttpClientErrorException} if the status code is in the 4xx
|
||||||
|
* series, or one of its sub-classes such as
|
||||||
|
* {@link HttpClientErrorException.BadRequest} and others.
|
||||||
|
* <li>{@link HttpServerErrorException} if the status code is in the 5xx
|
||||||
|
* series, or one of its sub-classes such as
|
||||||
|
* {@link HttpServerErrorException.InternalServerError} and others.
|
||||||
|
* <li>{@link UnknownHttpStatusCodeException} for error status codes not in the
|
||||||
|
* {@link HttpStatus} enum range.
|
||||||
|
* </ul>
|
||||||
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
|
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
|
||||||
* @see #handleError(ClientHttpResponse, HttpStatus)
|
* @see #handleError(ClientHttpResponse, HttpStatus)
|
||||||
*/
|
*/
|
||||||
|
|
@ -148,12 +161,13 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle the error in the given response with the given resolved status code.
|
* Handle the error based on the resolved status code.
|
||||||
* <p>The default implementation throws an {@link HttpClientErrorException}
|
*
|
||||||
* if the status code is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR
|
* <p>The default implementation delegates to
|
||||||
* CLIENT_ERROR}, an {@link HttpServerErrorException} if it is
|
* {@link HttpClientErrorException#create} for errors in the 4xx range, to
|
||||||
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR SERVER_ERROR},
|
* {@link HttpServerErrorException#create} for errors in the 5xx range,
|
||||||
* or an {@link UnknownHttpStatusCodeException} in other cases.
|
* or otherwise raises {@link UnknownHttpStatusCodeException}.
|
||||||
|
*
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
* @see HttpClientErrorException#create
|
* @see HttpClientErrorException#create
|
||||||
* @see HttpServerErrorException#create
|
* @see HttpServerErrorException#create
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -41,7 +41,14 @@ import org.springframework.util.StringUtils;
|
||||||
public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enum to represent multiple URI encoding strategies.
|
* Enum to represent multiple URI encoding strategies. The following are
|
||||||
|
* available:
|
||||||
|
* <ul>
|
||||||
|
* <li>{@link #TEMPLATE_AND_VALUES}
|
||||||
|
* <li>{@link #VALUES_ONLY}
|
||||||
|
* <li>{@link #URI_COMPONENT}
|
||||||
|
* <li>{@link #NONE}
|
||||||
|
* </ul>
|
||||||
* @see #setEncodingMode
|
* @see #setEncodingMode
|
||||||
*/
|
*/
|
||||||
public enum EncodingMode {
|
public enum EncodingMode {
|
||||||
|
|
@ -130,16 +137,13 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the encoding mode to use.
|
* Set the {@link EncodingMode encoding mode} to use.
|
||||||
* <p>By default this is set to {@link EncodingMode#TEMPLATE_AND_VALUES
|
* <p>By default this is set to {@link EncodingMode#TEMPLATE_AND_VALUES
|
||||||
* EncodingMode.TEMPLATE_AND_VALUES}.
|
* EncodingMode.TEMPLATE_AND_VALUES}.
|
||||||
* <p><strong>Note:</strong> In 5.1 the default was changed from
|
* <p><strong>Note:</strong> Prior to 5.1 the default was
|
||||||
* {@link EncodingMode#URI_COMPONENT EncodingMode.URI_COMPONENT}.
|
* {@link EncodingMode#URI_COMPONENT EncodingMode.URI_COMPONENT}
|
||||||
* Consequently the {@code WebClient}, which relies on the built-in default
|
* therefore the {@code WebClient} {@code RestTemplate} have switched their
|
||||||
* has also been switched to the new default. The {@code RestTemplate}
|
* default behavior.
|
||||||
* however sets this explicitly to {@link EncodingMode#URI_COMPONENT
|
|
||||||
* EncodingMode.URI_COMPONENT} explicitly for historic and backwards
|
|
||||||
* compatibility reasons.
|
|
||||||
* @param encodingMode the encoding mode to use
|
* @param encodingMode the encoding mode to use
|
||||||
*/
|
*/
|
||||||
public void setEncodingMode(EncodingMode encodingMode) {
|
public void setEncodingMode(EncodingMode encodingMode) {
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -77,22 +77,69 @@ public interface UriBuilder {
|
||||||
UriBuilder port(@Nullable String port);
|
UriBuilder port(@Nullable String port);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append the given path to the existing path of this builder.
|
* Append to the path of this builder.
|
||||||
* The given path may contain URI template variables.
|
* <p>The given value is appended as-is without any checks for slashes other
|
||||||
|
* than to clean up duplicates. For example:
|
||||||
|
* <pre class="code">
|
||||||
|
*
|
||||||
|
* builder.path("/first-").path("value/").path("/{id}").build("123")
|
||||||
|
*
|
||||||
|
* // Results is "/first-value/123"
|
||||||
|
* </pre>
|
||||||
|
* <p>By contrast {@link #pathSegment(String...)} builds the path from
|
||||||
|
* individual path segments and in that case slashes are inserted transparently.
|
||||||
|
* In some cases you may use a combination of both {@code pathSegment} and
|
||||||
|
* {@code path}. For example:
|
||||||
|
* <pre class="code">
|
||||||
|
*
|
||||||
|
* builder.pathSegment("first-value", "second-value").path("/")
|
||||||
|
*
|
||||||
|
* // Results is "/first-value/second-value/"
|
||||||
|
*
|
||||||
|
* </pre>
|
||||||
|
* <p>If a URI variable value contains slashes, whether those are encoded or
|
||||||
|
* not depends on the configured encoding mode. See
|
||||||
|
* {@link UriComponentsBuilder#encode()}, or if using
|
||||||
|
* {@code UriComponentsBuilder} via {@link DefaultUriBuilderFactory}
|
||||||
|
* (e.g. {@code WebClient} or {@code RestTemplate}) see its
|
||||||
|
* {@link DefaultUriBuilderFactory#setEncodingMode encodingMode} property.
|
||||||
|
* Also see the <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#web-uri-encoding">
|
||||||
|
* URI Encoding</a> section of the reference docs.
|
||||||
* @param path the URI path
|
* @param path the URI path
|
||||||
*/
|
*/
|
||||||
UriBuilder path(String path);
|
UriBuilder path(String path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the path of this builder overriding the existing path values.
|
* Override the existing path.
|
||||||
* @param path the URI path, or {@code null} for an empty path
|
* @param path the URI path, or {@code null} for an empty path
|
||||||
*/
|
*/
|
||||||
UriBuilder replacePath(@Nullable String path);
|
UriBuilder replacePath(@Nullable String path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append path segments to the existing path. Each path segment may contain
|
* Append to the path using path segments. For example:
|
||||||
* URI template variables and should not contain any slashes.
|
* <pre class="code">
|
||||||
* Use {@code path("/")} subsequently to ensure a trailing slash.
|
*
|
||||||
|
* builder.pathSegment("first-value", "second-value", "{id}").build("123")
|
||||||
|
*
|
||||||
|
* // Results is "/first-value/second-value/123"
|
||||||
|
*
|
||||||
|
* </pre>
|
||||||
|
* <p>If slashes are present in a path segment, they are encoded:
|
||||||
|
* <pre class="code">
|
||||||
|
*
|
||||||
|
* builder.pathSegment("ba/z", "{id}").build("a/b")
|
||||||
|
*
|
||||||
|
* // Results is "/ba%2Fz/a%2Fb"
|
||||||
|
*
|
||||||
|
* </pre>
|
||||||
|
* To insert a trailing slash, use the {@link #path} builder method:
|
||||||
|
* <pre class="code">
|
||||||
|
*
|
||||||
|
* builder.pathSegment("first-value", "second-value").path("/")
|
||||||
|
*
|
||||||
|
* // Results is "/first-value/second-value/"
|
||||||
|
*
|
||||||
|
* </pre>
|
||||||
* @param pathSegments the URI path segments
|
* @param pathSegments the URI path segments
|
||||||
*/
|
*/
|
||||||
UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException;
|
UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException;
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ import org.springframework.core.io.buffer.DataBufferUtils;
|
||||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpRequest;
|
||||||
import org.springframework.http.client.reactive.AbstractClientHttpRequest;
|
import org.springframework.http.client.reactive.AbstractClientHttpRequest;
|
||||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
@ -46,7 +47,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
public class MockClientHttpRequest extends AbstractClientHttpRequest implements HttpRequest {
|
||||||
|
|
||||||
private final HttpMethod httpMethod;
|
private final HttpMethod httpMethod;
|
||||||
|
|
||||||
|
|
@ -96,6 +97,11 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
return this.httpMethod;
|
return this.httpMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMethodValue() {
|
||||||
|
return this.httpMethod.name();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public URI getURI() {
|
public URI getURI() {
|
||||||
return this.url;
|
return this.url;
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ public class MockClientHttpResponse implements ClientHttpResponse {
|
||||||
public HttpHeaders getHeaders() {
|
public HttpHeaders getHeaders() {
|
||||||
if (!getCookies().isEmpty() && this.headers.get(HttpHeaders.SET_COOKIE) == null) {
|
if (!getCookies().isEmpty() && this.headers.get(HttpHeaders.SET_COOKIE) == null) {
|
||||||
getCookies().values().stream().flatMap(Collection::stream)
|
getCookies().values().stream().flatMap(Collection::stream)
|
||||||
.forEach(cookie -> getHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString()));
|
.forEach(cookie -> this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString()));
|
||||||
}
|
}
|
||||||
return this.headers;
|
return this.headers;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -40,6 +40,7 @@ import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.server.reactive.AbstractServerHttpRequest;
|
import org.springframework.http.server.reactive.AbstractServerHttpRequest;
|
||||||
import org.springframework.http.server.reactive.SslInfo;
|
import org.springframework.http.server.reactive.SslInfo;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MimeType;
|
import org.springframework.util.MimeType;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
@ -54,8 +55,12 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||||
*/
|
*/
|
||||||
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private final HttpMethod httpMethod;
|
private final HttpMethod httpMethod;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final String customHttpMethod;
|
||||||
|
|
||||||
private final MultiValueMap<String, HttpCookie> cookies;
|
private final MultiValueMap<String, HttpCookie> cookies;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -70,13 +75,15 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
private final Flux<DataBuffer> body;
|
private final Flux<DataBuffer> body;
|
||||||
|
|
||||||
|
|
||||||
private MockServerHttpRequest(HttpMethod httpMethod, URI uri, @Nullable String contextPath,
|
private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String customHttpMethod,
|
||||||
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
||||||
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
|
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
|
||||||
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
|
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
|
||||||
|
|
||||||
super(uri, contextPath, headers);
|
super(uri, contextPath, headers);
|
||||||
|
Assert.isTrue(httpMethod != null || customHttpMethod != null, "HTTP method must not be null");
|
||||||
this.httpMethod = httpMethod;
|
this.httpMethod = httpMethod;
|
||||||
|
this.customHttpMethod = customHttpMethod;
|
||||||
this.cookies = cookies;
|
this.cookies = cookies;
|
||||||
this.remoteAddress = remoteAddress;
|
this.remoteAddress = remoteAddress;
|
||||||
this.localAddress = localAddress;
|
this.localAddress = localAddress;
|
||||||
|
|
@ -91,8 +98,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
public String getMethodValue() {
|
public String getMethodValue() {
|
||||||
return this.httpMethod.name();
|
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -131,30 +139,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
|
|
||||||
// Static builder methods
|
// Static builder methods
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a builder with the given HTTP method and a {@link URI}.
|
|
||||||
* @param method the HTTP method (GET, POST, etc)
|
|
||||||
* @param url the URL
|
|
||||||
* @return the created builder
|
|
||||||
*/
|
|
||||||
public static BodyBuilder method(HttpMethod method, URI url) {
|
|
||||||
return new DefaultBodyBuilder(method, 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 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an HTTP GET builder with the given URI template. The given URI may
|
* Create an HTTP GET builder with the given URI template. The given URI may
|
||||||
* contain query parameters, or those may be added later via
|
* contain query parameters, or those may be added later via
|
||||||
|
|
@ -228,6 +212,44 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
return method(HttpMethod.OPTIONS, urlTemplate, uriVars);
|
return method(HttpMethod.OPTIONS, urlTemplate, uriVars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a builder with the given HTTP method and a {@link URI}.
|
||||||
|
* @param method the HTTP method (GET, POST, etc)
|
||||||
|
* @param url the URL
|
||||||
|
* @return the created builder
|
||||||
|
*/
|
||||||
|
public static BodyBuilder method(HttpMethod method, URI url) {
|
||||||
|
return new DefaultBodyBuilder(method, 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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a builder with a raw HTTP method value that is outside the range
|
||||||
|
* of {@link HttpMethod} enum values.
|
||||||
|
* @param method the HTTP method value
|
||||||
|
* @param urlTemplate the URL template
|
||||||
|
* @param vars variables to expand into the template
|
||||||
|
* @return the created builder
|
||||||
|
* @since 5.2.7
|
||||||
|
*/
|
||||||
|
public static BodyBuilder method(String method, String urlTemplate, Object... vars) {
|
||||||
|
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
|
||||||
|
return new DefaultBodyBuilder(method, url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request builder exposing properties not related to the body.
|
* Request builder exposing properties not related to the body.
|
||||||
|
|
@ -408,8 +430,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
|
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
|
||||||
|
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private final HttpMethod method;
|
private final HttpMethod method;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final String customMethod;
|
||||||
|
|
||||||
private final URI url;
|
private final URI url;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -431,8 +457,22 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
private SslInfo sslInfo;
|
private SslInfo sslInfo;
|
||||||
|
|
||||||
|
|
||||||
public DefaultBodyBuilder(HttpMethod method, URI url) {
|
DefaultBodyBuilder(HttpMethod method, URI url) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
|
this.customMethod = null;
|
||||||
|
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;
|
||||||
|
}
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -569,7 +609,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
@Override
|
@Override
|
||||||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||||
applyCookiesIfNecessary();
|
applyCookiesIfNecessary();
|
||||||
return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath,
|
return new MockServerHttpRequest(this.method, this.customMethod, getUrlToUse(), this.contextPath,
|
||||||
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
|
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -208,8 +208,10 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
|
||||||
ClientHttpResponse httpResponse = new BuiltClientHttpResponse(
|
ClientHttpResponse httpResponse = new BuiltClientHttpResponse(
|
||||||
this.statusCode, this.headers, this.cookies, this.body, this.originalResponse);
|
this.statusCode, this.headers, this.cookies, this.body, this.originalResponse);
|
||||||
|
|
||||||
return new DefaultClientResponse(
|
return new DefaultClientResponse(httpResponse, this.strategies,
|
||||||
httpResponse, this.strategies, "", "", () -> this.request);
|
this.originalResponse != null ? this.originalResponse.logPrefix() : "",
|
||||||
|
this.request.getMethodValue() + " " + this.request.getURI(),
|
||||||
|
() -> this.request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,12 @@ import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpRequest;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseCookie;
|
import org.springframework.http.ResponseCookie;
|
||||||
|
import org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest;
|
||||||
|
import org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
@ -65,27 +69,35 @@ public class DefaultClientResponseBuilderTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mutate() {
|
public void mutate() {
|
||||||
|
Flux<DataBuffer> otherBody = Flux.just("foo", "bar")
|
||||||
|
.map(s -> s.getBytes(StandardCharsets.UTF_8))
|
||||||
|
.map(dataBufferFactory::wrap);
|
||||||
|
|
||||||
ClientResponse originalResponse = ClientResponse
|
HttpRequest mockClientHttpRequest = new MockClientHttpRequest(HttpMethod.GET, "/path");
|
||||||
.create(HttpStatus.BAD_REQUEST, ExchangeStrategies.withDefaults())
|
|
||||||
.header("foo", "bar")
|
|
||||||
.header("bar", "baz")
|
|
||||||
.cookie("baz", "qux")
|
|
||||||
.body(Flux.just("foobar".getBytes(StandardCharsets.UTF_8)).map(dataBufferFactory::wrap))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
ClientResponse result = originalResponse.mutate()
|
MockClientHttpResponse httpResponse = new MockClientHttpResponse(HttpStatus.OK);
|
||||||
.statusCode(HttpStatus.OK)
|
httpResponse.getHeaders().add("foo", "bar");
|
||||||
|
httpResponse.getHeaders().add("bar", "baz");
|
||||||
|
httpResponse.getCookies().add("baz", ResponseCookie.from("baz", "qux").build());
|
||||||
|
httpResponse.setBody(otherBody);
|
||||||
|
|
||||||
|
DefaultClientResponse otherResponse = new DefaultClientResponse(
|
||||||
|
httpResponse, ExchangeStrategies.withDefaults(), "my-prefix", "", () -> mockClientHttpRequest);
|
||||||
|
|
||||||
|
ClientResponse result = otherResponse.mutate()
|
||||||
|
.statusCode(HttpStatus.BAD_REQUEST)
|
||||||
.headers(headers -> headers.set("foo", "baar"))
|
.headers(headers -> headers.set("foo", "baar"))
|
||||||
.cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build()))
|
.cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build()))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
assertThat(result.statusCode()).isEqualTo(HttpStatus.OK);
|
|
||||||
assertThat(result.headers().asHttpHeaders().size()).isEqualTo(2);
|
assertThat(result.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||||
|
assertThat(result.headers().asHttpHeaders().size()).isEqualTo(3);
|
||||||
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
|
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
|
||||||
assertThat(result.headers().asHttpHeaders().getFirst("bar")).isEqualTo("baz");
|
assertThat(result.headers().asHttpHeaders().getFirst("bar")).isEqualTo("baz");
|
||||||
assertThat(result.cookies().size()).isEqualTo(1);
|
assertThat(result.cookies().size()).isEqualTo(1);
|
||||||
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
|
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
|
||||||
|
assertThat(result.logPrefix()).isEqualTo("my-prefix");
|
||||||
|
|
||||||
StepVerifier.create(result.bodyToFlux(String.class))
|
StepVerifier.create(result.bodyToFlux(String.class))
|
||||||
.expectNext("foobar")
|
.expectNext("foobar")
|
||||||
|
|
@ -100,5 +112,4 @@ public class DefaultClientResponseBuilderTests {
|
||||||
assertThat(result.rawStatusCode()).isEqualTo(499);
|
assertThat(result.rawStatusCode()).isEqualTo(499);
|
||||||
assertThatIllegalArgumentException().isThrownBy(result::statusCode);
|
assertThatIllegalArgumentException().isThrownBy(result::statusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue