Search in sources :

Example 11 with BindingContext

use of org.springframework.web.reactive.BindingContext in project spring-framework by spring-projects.

the class RequestAttributeMethodArgumentResolverTests method resolveOptional.

@Test
public void resolveOptional() throws Exception {
    MethodParameter param = initMethodParameter(3);
    Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    assertFalse(((Optional<?>) mono.block()).isPresent());
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    BindingContext bindingContext = new BindingContext(initializer);
    Foo foo = new Foo();
    this.exchange.getAttributes().put("foo", foo);
    mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    Optional<?> optional = (Optional<?>) mono.block();
    assertTrue(optional.isPresent());
    assertSame(foo, optional.get());
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Optional(java.util.Optional) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 12 with BindingContext

use of org.springframework.web.reactive.BindingContext in project spring-framework by spring-projects.

the class RequestAttributeMethodArgumentResolverTests method resolveNotRequired.

@Test
public void resolveNotRequired() throws Exception {
    MethodParameter param = initMethodParameter(2);
    Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertNull(mono.block());
    Foo foo = new Foo();
    this.exchange.getAttributes().put("foo", foo);
    mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertSame(foo, mono.block());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 13 with BindingContext

use of org.springframework.web.reactive.BindingContext in project spring-framework by spring-projects.

the class RequestAttributeMethodArgumentResolverTests method resolveWithName.

@Test
public void resolveWithName() throws Exception {
    MethodParameter param = initMethodParameter(1);
    Foo foo = new Foo();
    this.exchange.getAttributes().put("specialFoo", foo);
    Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertSame(foo, mono.block());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 14 with BindingContext

use of org.springframework.web.reactive.BindingContext in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method testHttpOptions.

private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.options(requestURI).toExchange();
    HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
    BindingContext bindingContext = new BindingContext();
    InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
    Mono<HandlerResult> mono = invocable.invoke(exchange, bindingContext);
    HandlerResult result = mono.block();
    assertNotNull(result);
    Optional<Object> value = result.getReturnValue();
    assertTrue(value.isPresent());
    assertEquals(HttpHeaders.class, value.get().getClass());
    assertEquals(allowHeader, ((HttpHeaders) value.get()).getFirst("Allow"));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 15 with BindingContext

use of org.springframework.web.reactive.BindingContext in project spring-framework by spring-projects.

the class ExpressionValueMethodArgumentResolverTests method resolveSystemProperty.

@Test
public void resolveSystemProperty() throws Exception {
    System.setProperty("systemProperty", "22");
    try {
        Mono<Object> mono = this.resolver.resolveArgument(this.paramSystemProperty, new BindingContext(), this.exchange);
        Object value = mono.block();
        assertEquals(22, value);
    } finally {
        System.clearProperty("systemProperty");
    }
}
Also used : BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Aggregations

BindingContext (org.springframework.web.reactive.BindingContext)43 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)16 MethodParameter (org.springframework.core.MethodParameter)14 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)10 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)9 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)8 WebDataBinder (org.springframework.web.bind.WebDataBinder)7 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)6 Mono (reactor.core.publisher.Mono)6 Map (java.util.Map)5 Before (org.junit.Before)5 HashMap (java.util.HashMap)4 Optional (java.util.Optional)4 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)4 ServerWebInputException (org.springframework.web.server.ServerWebInputException)4 Method (java.lang.reflect.Method)3 List (java.util.List)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 ReactiveAdapter (org.springframework.core.ReactiveAdapter)3