Search in sources :

Example 21 with HandlerResult

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

the class InvocableHandlerMethodTests method resolverThrowsException.

@Test
public void resolverThrowsException() throws Exception {
    Mono<Object> resolvedValue = Mono.error(new UnsupportedMediaTypeStatusException("boo"));
    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 UnsupportedMediaTypeStatusException");
    } catch (UnsupportedMediaTypeStatusException ex) {
        assertThat(ex.getMessage(), is("Request failure [status: 415, reason: \"boo\"]"));
    }
}
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) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 22 with HandlerResult

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

the class InvocableHandlerMethodTests method noMatchingResolver.

@Test
public void noMatchingResolver() throws Exception {
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method);
    try {
        mono.block();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), is("No suitable resolver for argument 0 of type 'java.lang.String' " + "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)

Example 23 with HandlerResult

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

the class InvocableHandlerMethodTests method invokeMethodWithNoValue.

@Test
public void invokeMethodWithNoValue() throws Exception {
    Mono<Object> resolvedValue = Mono.empty();
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method, resolverFor(resolvedValue));
    assertHandlerResultValue(mono, "success:null");
}
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)

Example 24 with HandlerResult

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

the class ViewResolutionResultHandlerTests method testHandle.

private ServerWebExchange testHandle(String path, MethodParameter returnType, Object returnValue, String responseBody, ViewResolver... resolvers) throws URISyntaxException {
    Model model = this.bindingContext.getModel();
    model.asMap().clear();
    model.addAttribute("id", "123");
    HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
    MockServerWebExchange exchange = get(path).toExchange();
    resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertResponseBody(exchange, responseBody);
    return exchange;
}
Also used : ConcurrentModel(org.springframework.ui.ConcurrentModel) Model(org.springframework.ui.Model) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange)

Example 25 with HandlerResult

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

the class ViewResolutionResultHandlerTests method contentNegotiation.

@Test
public void contentNegotiation() 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();
    TestView defaultView = new TestView("jsonView", APPLICATION_JSON);
    resultHandler(Collections.singletonList(defaultView), new TestViewResolver("account")).handleResult(exchange, handlerResult).block(Duration.ofSeconds(5));
    assertEquals(APPLICATION_JSON, exchange.getResponse().getHeaders().getContentType());
    assertResponseBody(exchange, "jsonView: {" + "org.springframework.validation.BindingResult.testBean=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "testBean=TestBean[name=Joe]" + "}");
}
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)

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