Search in sources :

Example 6 with HandlerResult

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

the class InvocableHandlerMethodTests method invokeMethodWithNoArguments.

@Test
public void invokeMethodWithNoArguments() throws Exception {
    Method method = on(TestController.class).mockCall(TestController::noArgs).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method);
    assertHandlerResultValue(mono, "success");
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 7 with HandlerResult

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

the class InvocableHandlerMethodTests method invokeMethodWithResponseStatus.

@Test
public void invokeMethodWithResponseStatus() throws Exception {
    Method method = on(TestController.class).annotPresent(ResponseStatus.class).resolveMethod();
    Mono<HandlerResult> mono = invoke(new TestController(), method);
    assertHandlerResultValue(mono, "created");
    assertThat(this.exchange.getResponse().getStatusCode(), is(HttpStatus.CREATED));
}
Also used : ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 8 with HandlerResult

use of org.springframework.web.reactive.HandlerResult 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 9 with HandlerResult

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

the class ViewResolutionResultHandlerTests method modelWithAsyncAttributes.

@Test
public void modelWithAsyncAttributes() throws Exception {
    this.bindingContext.getModel().addAttribute("attr1", Mono.just(new TestBean("Bean1"))).addAttribute("attr2", Flux.just(new TestBean("Bean1"), new TestBean("Bean2"))).addAttribute("attr3", Single.just(new TestBean("Bean2"))).addAttribute("attr4", Observable.just(new TestBean("Bean1"), new TestBean("Bean2"))).addAttribute("attr5", Mono.empty());
    MethodParameter returnType = on(TestController.class).resolveReturnType(void.class);
    HandlerResult result = new HandlerResult(new Object(), null, returnType, this.bindingContext);
    ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account"));
    MockServerWebExchange exchange = get("/account").toExchange();
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {" + "attr1=TestBean[name=Bean1], " + "attr2=[TestBean[name=Bean1], TestBean[name=Bean2]], " + "attr3=TestBean[name=Bean2], " + "attr4=[TestBean[name=Bean1], TestBean[name=Bean2]], " + "org.springframework.validation.BindingResult.attr1=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "org.springframework.validation.BindingResult.attr3=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors" + "}");
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 10 with HandlerResult

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

the class ViewResolutionResultHandlerTests method contentNegotiationWith406.

@Test
public void contentNegotiationWith406() throws Exception {
    TestBean value = new TestBean("Joe");
    MethodParameter returnType = on(TestController.class).resolveReturnType(TestBean.class);
    HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext);
    MockServerWebExchange exchange = get("/account").accept(APPLICATION_JSON).toExchange();
    ViewResolutionResultHandler resultHandler = resultHandler(new TestViewResolver("account"));
    Mono<Void> mono = resultHandler.handleResult(exchange, handlerResult);
    StepVerifier.create(mono).expectNextCount(0).expectError(NotAcceptableStatusException.class).verify();
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Aggregations

HandlerResult (org.springframework.web.reactive.HandlerResult)34 Test (org.junit.Test)24 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)22 MethodParameter (org.springframework.core.MethodParameter)16 Method (java.lang.reflect.Method)10 BindingContext (org.springframework.web.reactive.BindingContext)7 Mono (reactor.core.publisher.Mono)7 Arrays (java.util.Arrays)6 Optional (java.util.Optional)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)6 Matchers.is (org.hamcrest.Matchers.is)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertThat (org.junit.Assert.assertThat)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 Assert.fail (org.junit.Assert.fail)5 Mockito.any (org.mockito.Mockito.any)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.when (org.mockito.Mockito.when)5 Instant (java.time.Instant)4 HttpStatus (org.springframework.http.HttpStatus)4