提交 3a66927b 编写于 作者: J Juergen Hoeller

Polishing

上级 c58da710
......@@ -66,7 +66,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
public static final int DEFAULT_CACHE_LIMIT = 1024;
/** Static evaluation context to reuse. */
private static EvaluationContext messageEvalContext =
private static final EvaluationContext messageEvalContext =
SimpleEvaluationContext.forPropertyAccessors(new SimpMessageHeaderPropertyAccessor()).build();
......@@ -130,7 +130,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
* @since 4.2
*/
public void setSelectorHeaderName(@Nullable String selectorHeaderName) {
this.selectorHeaderName = StringUtils.hasText(selectorHeaderName) ? selectorHeaderName : null;
this.selectorHeaderName = (StringUtils.hasText(selectorHeaderName) ? selectorHeaderName : null);
}
/**
......
......@@ -1017,9 +1017,10 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
}
/**
* Return the value of the required {@code Host} header.
* <p>If the header value does not contain a port, the returned
* {@linkplain InetSocketAddress#getPort() port} will be {@code 0}.
* Return the value of the {@code Host} header, if available.
* <p>If the header value does not contain a port, the
* {@linkplain InetSocketAddress#getPort() port} in the returned address will
* be {@code 0}.
* @since 5.0
*/
@Nullable
......
......@@ -90,7 +90,7 @@ public interface ServerWebExchange {
@SuppressWarnings("unchecked")
default <T> T getRequiredAttribute(String name) {
T value = getAttribute(name);
Assert.notNull(value, () -> "Required attribute '" + name + "' is missing.");
Assert.notNull(value, () -> "Required attribute '" + name + "' is missing");
return value;
}
......@@ -124,7 +124,6 @@ public interface ServerWebExchange {
/**
* Return the form data from the body of the request if the Content-Type is
* {@code "application/x-www-form-urlencoded"} or an empty map otherwise.
*
* <p><strong>Note:</strong> calling this method causes the request body to
* be read and parsed in full and the resulting {@code MultiValueMap} is
* cached so that this method is safe to call more than once.
......@@ -134,7 +133,6 @@ public interface ServerWebExchange {
/**
* Return the parts of a multipart request if the Content-Type is
* {@code "multipart/form-data"} or an empty map otherwise.
*
* <p><strong>Note:</strong> calling this method causes the request body to
* be read and parsed in full and the resulting {@code MultiValueMap} is
* cached so that this method is safe to call more than once.
......@@ -150,10 +148,9 @@ public interface ServerWebExchange {
/**
* Return the {@link ApplicationContext} associated with the web application,
* if it was initialized with one via
* {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext
* WebHttpHandlerBuilder#applicationContext}.
* {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext}.
* @since 5.0.3
* @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext(ApplicationContext)
* @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext
*/
@Nullable
ApplicationContext getApplicationContext();
......
......@@ -99,6 +99,8 @@ public abstract class BodyInserters {
public static <T, P extends Publisher<T>> BodyInserter<P, ReactiveHttpOutputMessage> fromPublisher(
P publisher, Class<T> elementClass) {
Assert.notNull(publisher, "Publisher must not be null");
Assert.notNull(elementClass, "Element Class must not be null");
return (message, context) ->
writeWithMessageWriters(message, context, publisher, ResolvableType.forClass(elementClass));
}
......@@ -109,16 +111,18 @@ public abstract class BodyInserters {
* {@link org.springframework.web.reactive.function.client.WebClient WebClient} and
* {@link org.springframework.web.reactive.function.server.ServerResponse ServerResponse}.
* @param publisher the publisher to write with
* @param typeRef the type of elements contained in the publisher
* @param typeReference the type of elements contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @param <P> the {@code Publisher} type
* @return the inserter to write a {@code Publisher}
*/
public static <T, P extends Publisher<T>> BodyInserter<P, ReactiveHttpOutputMessage> fromPublisher(
P publisher, ParameterizedTypeReference<T> typeRef) {
P publisher, ParameterizedTypeReference<T> typeReference) {
Assert.notNull(publisher, "Publisher must not be null");
Assert.notNull(typeReference, "ParameterizedTypeReference must not be null");
return (message, context) ->
writeWithMessageWriters(message, context, publisher, ResolvableType.forType(typeRef.getType()));
writeWithMessageWriters(message, context, publisher, ResolvableType.forType(typeReference.getType()));
}
/**
......@@ -130,6 +134,7 @@ public abstract class BodyInserters {
* @return the inserter to write a {@code Publisher}
*/
public static <T extends Resource> BodyInserter<T, ReactiveHttpOutputMessage> fromResource(T resource) {
Assert.notNull(resource, "Resource must not be null");
return (outputMessage, context) -> {
ResolvableType elementType = RESOURCE_TYPE;
HttpMessageWriter<Resource> writer = findWriter(context, elementType, null);
......@@ -151,6 +156,7 @@ public abstract class BodyInserters {
public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(
S eventsPublisher) {
Assert.notNull(eventsPublisher, "Publisher must not be null");
return (serverResponse, context) -> {
ResolvableType elmentType = SSE_TYPE;
MediaType mediaType = MediaType.TEXT_EVENT_STREAM;
......@@ -163,13 +169,11 @@ public abstract class BodyInserters {
* Return a {@link FormInserter} to write the given {@code MultiValueMap}
* as URL-encoded form data. The returned inserter allows for additional
* entries to be added via {@link FormInserter#with(String, Object)}.
*
* <p>Note that you can also use the {@code syncBody(Object)} method in the
* request builders of both the {@code WebClient} and {@code WebTestClient}.
* In that case the setting of the request content type is also not required,
* just be sure the map contains String values only or otherwise it would be
* interpreted as a multipart request.
*
* @param formData the form data to write to the output message
* @return the inserter that allows adding more form data
*/
......
......@@ -106,8 +106,7 @@ public interface ClientRequest {
String logPrefix();
/**
* Writes this request to the given {@link ClientHttpRequest}.
*
* Write this request to the given {@link ClientHttpRequest}.
* @param request the client http request to write to
* @param strategies the strategies to use when writing
* @return {@code Mono<Void>} to indicate when writing is complete
......
......@@ -64,13 +64,6 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
private BodyInserter<?, ? super ClientHttpRequest> body = BodyInserters.empty();
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod must not be null");
Assert.notNull(url, "URI must not be null");
this.method = method;
this.url = url;
}
public DefaultClientRequestBuilder(ClientRequest other) {
Assert.notNull(other, "ClientRequest must not be null");
this.method = other.method();
......@@ -81,17 +74,24 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
body(other.body());
}
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod must not be null");
Assert.notNull(url, "URI must not be null");
this.method = method;
this.url = url;
}
@Override
public ClientRequest.Builder method(HttpMethod method) {
Assert.notNull(method, "'method' must not be null");
Assert.notNull(method, "HttpMethod must not be null");
this.method = method;
return this;
}
@Override
public ClientRequest.Builder url(URI url) {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(url, "URI must not be null");
this.url = url;
return this;
}
......@@ -126,9 +126,6 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
@Override
public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class<S> elementClass) {
Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(elementClass, "'elementClass' must not be null");
this.body = BodyInserters.fromPublisher(publisher, elementClass);
return this;
}
......@@ -137,9 +134,6 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
public <S, P extends Publisher<S>> ClientRequest.Builder body(
P publisher, ParameterizedTypeReference<S> typeReference) {
Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(typeReference, "'typeReference' must not be null");
this.body = BodyInserters.fromPublisher(publisher, typeReference);
return this;
}
......@@ -184,7 +178,6 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
private final String logPrefix;
public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
MultiValueMap<String, String> cookies, BodyInserter<?, ? super ClientHttpRequest> body,
Map<String, Object> attributes) {
......@@ -200,7 +193,6 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
this.logPrefix = "[" + id + "] ";
}
@Override
public HttpMethod method() {
return this.method;
......
......@@ -116,9 +116,9 @@ public abstract class ExchangeFilterFunctions {
@Deprecated
public static ExchangeFilterFunction basicAuthentication() {
return (request, next) -> {
Credentials cred = (Credentials) request
.attribute(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE).orElse(null);
if (cred != null) {
Object attr = request.attributes().get(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE);
if (attr instanceof Credentials) {
Credentials cred = (Credentials) attr;
return next.exchange(ClientRequest.from(request)
.headers(headers -> headers.setBasicAuth(cred.username, cred.password))
.build());
......
......@@ -140,7 +140,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
@Override
public ServerRequest.Builder body(Flux<DataBuffer> body) {
Assert.notNull(body, "'body' must not be null");
Assert.notNull(body, "Body must not be null");
releaseBody();
this.body = body;
return this;
......@@ -148,7 +148,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
@Override
public ServerRequest.Builder body(String body) {
Assert.notNull(body, "'body' must not be null");
Assert.notNull(body, "Body must not be null");
releaseBody();
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
this.body = Flux.just(body).
......@@ -165,7 +165,6 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
@Override
public ServerRequest.Builder attribute(String name, Object value) {
Assert.notNull(name, "'name' must not be null");
this.attributes.put(name, value);
return this;
}
......
......@@ -39,6 +39,7 @@ import static org.mockito.Mockito.*;
/**
* Unit tests for {@link DefaultWebClient}.
*
* @author Rossen Stoyanchev
*/
public class DefaultWebClientTests {
......
......@@ -38,6 +38,7 @@ import static org.mockito.Mockito.*;
/**
* Unit tests for {@link ExchangeFilterFunctions}.
*
* @author Arjen Poutsma
*/
public class ExchangeFilterFunctionsTests {
......
......@@ -42,6 +42,7 @@ import static org.junit.Assert.*;
/**
* WebClient integration tests focusing on data buffer management.
*
* @author Rossen Stoyanchev
*/
public class WebClientDataBufferAllocatingTests extends AbstractDataBufferAllocatingTestCase {
......
......@@ -75,13 +75,13 @@ public class WebClientIntegrationTests {
@Parameterized.Parameters(name = "webClient [{0}]")
public static Object[][] arguments() {
return new Object[][] {
{new JettyClientHttpConnector()},
{new ReactorClientHttpConnector()}
};
}
@Before
public void setup() {
this.server = new MockWebServer();
......
......@@ -62,6 +62,7 @@ public class DefaultServerResponseBuilderTests {
}
};
@Test
public void from() {
ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block();
......@@ -383,5 +384,4 @@ public class DefaultServerResponseBuilderTests {
.verify();
}
}
......@@ -43,9 +43,8 @@ import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import org.springframework.web.util.pattern.PathPattern;
import static org.junit.Assert.*;
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.BodyInserters.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
/**
* Tests the use of {@link HandlerFunction} and {@link RouterFunction} in a
......@@ -134,7 +133,6 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
return new AttributesHandler();
}
@Bean
public RouterFunction<EntityResponse<Person>> monoRouterFunction(PersonHandler personHandler) {
return route(RequestPredicates.GET("/mono"), personHandler::mono);
......@@ -150,7 +148,6 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
return nest(RequestPredicates.GET("/attributes"),
route(RequestPredicates.GET("/{foo}"), attributesHandler::attributes));
}
}
......@@ -167,16 +164,15 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
return ServerResponse.ok().body(
fromPublisher(Flux.just(person1, person2), Person.class));
}
}
private static class AttributesHandler {
@SuppressWarnings("unchecked")
public Mono<ServerResponse> attributes(ServerRequest request) {
assertTrue(request.attributes().containsKey(RouterFunctions.REQUEST_ATTRIBUTE));
assertTrue(request.attributes()
.containsKey(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
assertTrue(request.attributes().containsKey(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
Map<String, String> pathVariables =
(Map<String, String>) request.attributes().get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
......@@ -203,9 +199,9 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
return ServerResponse.ok().build();
}
}
@Controller
public static class PersonController {
......@@ -216,6 +212,7 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
}
}
private static class Person {
private String name;
......
/*
* 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.
......@@ -44,21 +44,6 @@ public class LocaleContextResolverIntegrationTests extends AbstractRouterFunctio
private final WebClient webClient = WebClient.create();
@Test
public void fixedLocale() {
Mono<ClientResponse> result = webClient
.get()
.uri("http://localhost:" + this.port + "/")
.exchange();
StepVerifier
.create(result)
.consumeNextWith(response -> {
assertEquals(HttpStatus.OK, response.statusCode());
assertEquals(Locale.GERMANY, response.headers().asHttpHeaders().getContentLanguage());
})
.verifyComplete();
}
@Override
protected RouterFunction<?> routerFunction() {
......@@ -77,6 +62,24 @@ public class LocaleContextResolverIntegrationTests extends AbstractRouterFunctio
.build();
}
@Test
public void fixedLocale() {
Mono<ClientResponse> result = webClient
.get()
.uri("http://localhost:" + this.port + "/")
.exchange();
StepVerifier
.create(result)
.consumeNextWith(response -> {
assertEquals(HttpStatus.OK, response.statusCode());
assertEquals(Locale.GERMANY, response.headers().asHttpHeaders().getContentLanguage());
})
.verifyComplete();
}
private static class DummyViewResolver implements ViewResolver {
@Override
......@@ -85,6 +88,7 @@ public class LocaleContextResolverIntegrationTests extends AbstractRouterFunctio
}
}
private static class DummyView implements View {
private final Locale locale;
......@@ -105,4 +109,5 @@ public class LocaleContextResolverIntegrationTests extends AbstractRouterFunctio
return Mono.empty();
}
}
}
/*
* 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.
......@@ -69,9 +69,9 @@ public class RenderingResponseIntegrationTests extends AbstractRouterFunctionInt
return HandlerStrategies.builder()
.viewResolver(new DummyViewResolver())
.build();
}
@Test
public void normal() {
ResponseEntity<String> result =
......@@ -109,6 +109,7 @@ public class RenderingResponseIntegrationTests extends AbstractRouterFunctionInt
return result;
}
private static class RenderingResponseHandler {
public Mono<RenderingResponse> render(ServerRequest request) {
......@@ -116,7 +117,6 @@ public class RenderingResponseIntegrationTests extends AbstractRouterFunctionInt
.modelAttribute("bar", "baz")
.build();
}
}
private static class DummyViewResolver implements ViewResolver {
......
/*
* 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.
......
......@@ -209,7 +209,6 @@ public class RouterFunctionBuilderTests {
StepVerifier.create(barResponseMono)
.expectNext(500)
.verifyComplete();
}
}
\ No newline at end of file
}
......@@ -35,8 +35,8 @@ import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import static org.junit.Assert.*;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
/**
* @author Arjen Poutsma
......@@ -45,6 +45,7 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
private final RestTemplate restTemplate = new RestTemplate();
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
......@@ -54,6 +55,7 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
@Test
public void nested() {
ResponseEntity<String> result = this.restTemplate
......@@ -83,6 +85,7 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
}
}
static class Handler {
public Mono<ServerResponse> handle(ServerRequest request) {
......@@ -90,7 +93,4 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
}
}
}
\ No newline at end of file
......@@ -21,7 +21,6 @@ import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
......@@ -44,15 +43,10 @@ import static org.mockito.Mockito.*;
*/
public class ServerRequestWrapperTests {
private ServerRequest mockRequest;
private final ServerRequest mockRequest = mock(ServerRequest.class);
private ServerRequestWrapper wrapper;
private final ServerRequestWrapper wrapper = new ServerRequestWrapper(mockRequest);
@Before
public void createWrapper() {
mockRequest = mock(ServerRequest.class);
wrapper = new ServerRequestWrapper(mockRequest);
}
@Test
public void request() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册