Search in sources :

Example 16 with HandlerResult

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

the class ResponseEntityResultHandlerTests method headers.

@Test
public void headers() throws Exception {
    URI location = new URI("/path");
    ResponseEntity<Void> value = ResponseEntity.created(location).build();
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = get("/path").toExchange();
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.CREATED, exchange.getResponse().getStatusCode());
    assertEquals(1, exchange.getResponse().getHeaders().size());
    assertEquals(location, exchange.getResponse().getHeaders().getLocation());
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) URI(java.net.URI) Test(org.junit.Test)

Example 17 with HandlerResult

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

the class ResponseEntityResultHandlerTests method handleMonoWithWildcardBodyTypeAndNullBody.

// SPR-14877
@Test
public void handleMonoWithWildcardBodyTypeAndNullBody() throws Exception {
    MockServerWebExchange exchange = get("/path").toExchange();
    exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(APPLICATION_JSON));
    MethodParameter returnType = on(TestController.class).resolveReturnType(Mono.class, ResponseEntity.class);
    HandlerResult result = new HandlerResult(new TestController(), Mono.just(notFound().build()), returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.NOT_FOUND, exchange.getResponse().getStatusCode());
    assertResponseBodyIsEmpty(exchange);
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 18 with HandlerResult

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

the class ResponseEntityResultHandlerTests method handleReturnValueEtagInvalidIfNoneMatch.

// SPR-14559
@Test
public void handleReturnValueEtagInvalidIfNoneMatch() throws Exception {
    MockServerWebExchange exchange = get("/path").ifNoneMatch("unquoted").toExchange();
    ResponseEntity<String> entity = ok().eTag("\"deadb33f8badf00d\"").body("body");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(entity, returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
    assertResponseBody(exchange, "body");
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 19 with HandlerResult

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

the class HandlerFunctionAdapter method handle.

@Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
    HandlerFunction<?> handlerFunction = (HandlerFunction<?>) handler;
    ServerRequest request = exchange.<ServerRequest>getAttribute(RouterFunctions.REQUEST_ATTRIBUTE).orElseThrow(() -> new IllegalStateException("Could not find ServerRequest in exchange attributes"));
    return handlerFunction.handle(request).map(response -> new HandlerResult(handlerFunction, response, HANDLER_FUNCTION_RETURN_TYPE));
}
Also used : HandlerFunction(org.springframework.web.reactive.function.server.HandlerFunction) HandlerResult(org.springframework.web.reactive.HandlerResult) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest)

Example 20 with HandlerResult

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

the class InvocableHandlerMethodTests method illegalArgumentExceptionIsWrappedWithInvocationDetails.

@Test
public void illegalArgumentExceptionIsWrappedWithInvocationDetails() throws Exception {
    Mono<Object> resolvedValue = Mono.just(1);
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method, resolverFor(resolvedValue));
    try {
        mono.block();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), is("Failed to invoke handler method with resolved arguments: " + "[0][type=java.lang.Integer][value=1] " + "on " + method.toGenericString()));
    }
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ResolvableMethod.on(org.springframework.web.method.ResolvableMethod.on) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) BindingContext(org.springframework.web.reactive.BindingContext) Mockito.when(org.mockito.Mockito.when) HandlerResult(org.springframework.web.reactive.HandlerResult) Assert.assertThat(org.junit.Assert.assertThat) HttpStatus(org.springframework.http.HttpStatus) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Mockito.any(org.mockito.Mockito.any) Method(java.lang.reflect.Method) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) 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