result.add(new ModelAttributeMethodArgumentResolver(reactiveRegistry, false)); result.add(new RequestHeaderMethodArgumentResolver(beanFactory, reactiveRegistry)); result.add(new RequestHeaderMapMethodArgumentResolver(reactiveRegistry)); result.add(new CookieValueMethodArgumentResolver(beanFactory, reactiveRegistry));
@Override public boolean supportsParameter(MethodParameter param) { return checkAnnotatedParamNoReactiveWrapper(param, RequestHeader.class, this::singleParam); }
@Test public void supportsParameter() { assertTrue("String parameter not supported", resolver.supportsParameter(paramNamedDefaultValueStringHeader)); assertTrue("String array parameter not supported", resolver.supportsParameter(paramNamedValueStringArray)); assertFalse("non-@RequestParam parameter supported", resolver.supportsParameter(paramNamedValueMap)); try { this.resolver.supportsParameter(this.paramMono); fail(); } catch (IllegalStateException ex) { assertTrue("Unexpected error message:\n" + ex.getMessage(), ex.getMessage().startsWith( "RequestHeaderMethodArgumentResolver doesn't support reactive type wrapper")); } }
@Test public void resolveDefaultValueFromSystemProperty() throws Exception { System.setProperty("systemProperty", "bar"); try { Mono<Object> mono = this.resolver.resolveArgument( this.paramSystemProperty, this.bindingContext, MockServerWebExchange.from(MockServerHttpRequest.get("/"))); Object result = mono.block(); assertTrue(result instanceof String); assertEquals("bar", result); } finally { System.clearProperty("systemProperty"); } }
@Test public void resolveDefaultValue() throws Exception { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); Mono<Object> mono = this.resolver.resolveArgument( this.paramNamedDefaultValueStringHeader, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof String); assertEquals("bar", result); }
@Before @SuppressWarnings("resource") public void setup() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.refresh(); ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry); ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultFormattingConversionService()); this.bindingContext = new BindingContext(initializer); Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null); this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0); this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1); this.paramSystemProperty = new SynthesizingMethodParameter(method, 2); this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3); this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4); this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5); this.paramDate = new SynthesizingMethodParameter(method, 6); this.paramInstant = new SynthesizingMethodParameter(method, 7); this.paramMono = new SynthesizingMethodParameter(method, 8); }
@Test @SuppressWarnings("deprecation") public void dateConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; MockServerHttpRequest request = MockServerHttpRequest.get("/").header("name", rfc1123val).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); Mono<Object> mono = this.resolver.resolveArgument(this.paramDate, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof Date); assertEquals(new Date(rfc1123val), result); }
protected List<SyncHandlerMethodArgumentResolver> getDefaultInitBinderArgumentResolvers() { List<SyncHandlerMethodArgumentResolver> resolvers = new ArrayList<>(); // Annotation-based argument resolution resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), false)); resolvers.add(new RequestParamMapMethodArgumentResolver()); resolvers.add(new PathVariableMethodArgumentResolver(getBeanFactory())); resolvers.add(new PathVariableMapMethodArgumentResolver()); resolvers.add(new RequestHeaderMethodArgumentResolver(getBeanFactory())); resolvers.add(new RequestHeaderMapMethodArgumentResolver()); resolvers.add(new CookieValueMethodArgumentResolver(getBeanFactory())); resolvers.add(new ExpressionValueMethodArgumentResolver(getBeanFactory())); resolvers.add(new RequestAttributeMethodArgumentResolver(getBeanFactory())); // Type-based argument resolution resolvers.add(new ModelArgumentResolver()); resolvers.add(new ServerWebExchangeArgumentResolver()); // Custom resolvers if (getCustomInitBinderArgumentResolvers() != null) { resolvers.addAll(getCustomInitBinderArgumentResolvers()); } // Catch-all resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true)); return resolvers; }
@Test public void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception { String expected = "foo"; MockServerHttpRequest request = MockServerHttpRequest.get("/").header("bar", expected).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); System.setProperty("systemProperty", "bar"); try { Mono<Object> mono = this.resolver.resolveArgument( this.paramResolvedNameWithPlaceholder, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof String); assertEquals(expected, result); } finally { System.clearProperty("systemProperty"); } }
resolvers.add(new RequestBodyArgumentResolver(getMessageReaders(), getReactiveAdapterRegistry())); resolvers.add(new ModelAttributeMethodArgumentResolver(getReactiveAdapterRegistry())); resolvers.add(new RequestHeaderMethodArgumentResolver(getBeanFactory())); resolvers.add(new RequestHeaderMapMethodArgumentResolver()); resolvers.add(new CookieValueMethodArgumentResolver(getBeanFactory()));
@Test public void resolveNameFromSystemPropertyThroughExpression() throws Exception { String expected = "foo"; MockServerHttpRequest request = MockServerHttpRequest.get("/").header("bar", expected).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); System.setProperty("systemProperty", "bar"); try { Mono<Object> mono = this.resolver.resolveArgument( this.paramResolvedNameWithExpression, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof String); assertEquals(expected, result); } finally { System.clearProperty("systemProperty"); } }
@Test public void resolveStringArgument() throws Exception { String expected = "foo"; ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").header("name", expected)); Mono<Object> mono = this.resolver.resolveArgument( this.paramNamedDefaultValueStringHeader, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof String); assertEquals(expected, result); }
@Test public void instantConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; MockServerHttpRequest request = MockServerHttpRequest.get("/").header("name", rfc1123val).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); Mono<Object> mono = this.resolver.resolveArgument(this.paramInstant, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof Instant); assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result); }
@Test public void resolveStringArrayArgument() throws Exception { MockServerHttpRequest request = MockServerHttpRequest.get("/").header("name", "foo", "bar").build(); ServerWebExchange exchange = MockServerWebExchange.from(request); Mono<Object> mono = this.resolver.resolveArgument( this.paramNamedValueStringArray, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof String[]); assertArrayEquals(new String[] {"foo", "bar"}, (String[]) result); }
@Test public void notFound() throws Exception { Mono<Object> mono = resolver.resolveArgument( this.paramNamedValueStringArray, this.bindingContext, MockServerWebExchange.from(MockServerHttpRequest.get("/"))); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); }