Add builder to MockServerWebExchange

Issue: SPR-16772
This commit is contained in:
Rossen Stoyanchev
2018-05-10 09:59:02 -04:00
parent d82b0e37df
commit eef592d901
9 changed files with 242 additions and 185 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.
@@ -58,7 +58,7 @@ public class ServerWebExchangeArgumentResolverTests {
@Test
public void supportsParameter() throws Exception {
public void supportsParameter() {
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(ServerWebExchange.class)));
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(ServerHttpRequest.class)));
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(ServerHttpResponse.class)));
@@ -69,6 +69,7 @@ public class ServerWebExchangeArgumentResolverTests {
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(UriComponentsBuilder.class)));
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(UriBuilder.class)));
assertFalse(this.resolver.supportsParameter(this.testMethod.arg(WebSession.class)));
assertFalse(this.resolver.supportsParameter(this.testMethod.arg(String.class)));
try {
this.resolver.supportsParameter(this.testMethod.arg(Mono.class, ServerWebExchange.class));
@@ -82,7 +83,7 @@ public class ServerWebExchangeArgumentResolverTests {
}
@Test
public void resolveArgument() throws Exception {
public void resolveArgument() {
testResolveArgument(this.testMethod.arg(ServerWebExchange.class), this.exchange);
testResolveArgument(this.testMethod.arg(ServerHttpRequest.class), this.exchange.getRequest());
testResolveArgument(this.testMethod.arg(ServerHttpResponse.class), this.exchange.getResponse());
@@ -97,7 +98,7 @@ public class ServerWebExchangeArgumentResolverTests {
}
@Test
public void resolveUriComponentsBuilder() throws Exception {
public void resolveUriComponentsBuilder() {
MethodParameter param = this.testMethod.arg(UriComponentsBuilder.class);
Object value = this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block();

View File

@@ -31,10 +31,8 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
@@ -42,22 +40,12 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.WebSession;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
import org.springframework.web.server.session.MockWebSessionManager;
import org.springframework.web.server.session.WebSessionManager;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link SessionAttributeMethodArgumentResolver}.
*
* @author Rossen Stoyanchev
*/
public class SessionAttributeMethodArgumentResolverTests {
@@ -73,31 +61,25 @@ public class SessionAttributeMethodArgumentResolverTests {
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
public void setup() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.refresh();
ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
this.session = mock(WebSession.class);
WebSessionManager sessionManager = new MockWebSessionManager(this.session);
ServerHttpRequest request = MockServerHttpRequest.get("/").build();
this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(),
sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).session(this.session).build();
this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null);
}
@Test
public void supportsParameter() throws Exception {
public void supportsParameter() {
assertTrue(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 0)));
assertFalse(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 4)));
}
@Test
public void resolve() throws Exception {
public void resolve() {
MethodParameter param = initMethodParameter(0);
Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
StepVerifier.create(mono).expectError(ServerWebInputException.class).verify();
@@ -109,7 +91,7 @@ public class SessionAttributeMethodArgumentResolverTests {
}
@Test
public void resolveWithName() throws Exception {
public void resolveWithName() {
MethodParameter param = initMethodParameter(1);
Foo foo = new Foo();
when(this.session.getAttribute("specialFoo")).thenReturn(foo);
@@ -118,7 +100,7 @@ public class SessionAttributeMethodArgumentResolverTests {
}
@Test
public void resolveNotRequired() throws Exception {
public void resolveNotRequired() {
MethodParameter param = initMethodParameter(2);
Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
assertNull(mono.block());
@@ -131,7 +113,7 @@ public class SessionAttributeMethodArgumentResolverTests {
@SuppressWarnings("unchecked")
@Test
public void resolveOptional() throws Exception {
public void resolveOptional() {
MethodParameter param = initMethodParameter(3);
Optional<Object> actual = (Optional<Object>) this.resolver
.resolveArgument(param, new BindingContext(), this.exchange).block();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 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.
@@ -17,7 +17,6 @@
package org.springframework.web.reactive.result.method.annotation;
import java.time.Duration;
import java.util.HashSet;
import org.junit.Test;
@@ -28,12 +27,8 @@ import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.server.WebSession;
import org.springframework.web.server.session.InMemoryWebSessionStore;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static java.util.Arrays.*;
import static org.junit.Assert.*;
/**
* Test fixture with {@link SessionAttributesHandler}.
@@ -46,7 +41,7 @@ public class SessionAttributesHandlerTests {
@Test
public void isSessionAttribute() throws Exception {
public void isSessionAttribute() {
assertTrue(this.sessionAttributesHandler.isHandlerSessionAttribute("attr1", String.class));
assertTrue(this.sessionAttributesHandler.isHandlerSessionAttribute("attr2", String.class));
assertTrue(this.sessionAttributesHandler.isHandlerSessionAttribute("simple", TestBean.class));
@@ -54,8 +49,8 @@ public class SessionAttributesHandlerTests {
}
@Test
public void retrieveAttributes() throws Exception {
WebSession session = new InMemoryWebSessionStore().createWebSession().block(Duration.ZERO);
public void retrieveAttributes() {
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
assertNotNull(session);
session.getAttributes().put("attr1", "value1");
@@ -76,8 +71,8 @@ public class SessionAttributesHandlerTests {
}
@Test
public void cleanupAttributes() throws Exception {
WebSession session = new InMemoryWebSessionStore().createWebSession().block(Duration.ZERO);
public void cleanupAttributes() {
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
assertNotNull(session);
session.getAttributes().put("attr1", "value1");
@@ -98,8 +93,8 @@ public class SessionAttributesHandlerTests {
}
@Test
public void storeAttributes() throws Exception {
WebSession session = new InMemoryWebSessionStore().createWebSession().block(Duration.ZERO);
public void storeAttributes() {
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
assertNotNull(session);
ModelMap model = new ModelMap();

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.
@@ -21,21 +21,15 @@ import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.method.ResolvableMethod;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
import org.springframework.web.server.session.WebSessionManager;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link WebSessionArgumentResolver}.
@@ -50,7 +44,7 @@ public class WebSessionArgumentResolverTests {
@Test
public void supportsParameter() throws Exception {
public void supportsParameter() {
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(WebSession.class)));
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Mono.class, WebSession.class)));
assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Single.class, WebSession.class)));
@@ -58,14 +52,12 @@ public class WebSessionArgumentResolverTests {
@Test
public void resolverArgument() throws Exception {
public void resolverArgument() {
BindingContext context = new BindingContext();
WebSession session = mock(WebSession.class);
WebSessionManager manager = exchange -> Mono.just(session);
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(),
manager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
ServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build();
MethodParameter param = this.testMethod.arg(WebSession.class);
Object actual = this.resolver.resolveArgument(param, context, exchange).block();