Search in sources :

Example 86 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class DispatcherHandlerErrorTests method unknownReturnType.

@Test
public void unknownReturnType() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-return-type"));
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> assertThat(error).isInstanceOf(IllegalStateException.class).hasMessageStartingWith("No HandlerResultHandler")).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 87 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class DispatcherHandlerErrorTests method noHandler.

@Test
public void noHandler() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/does-not-exist"));
    Mono<Void> mono = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(mono).consumeErrorWith(ex -> {
        assertThat(ex).isInstanceOf(ResponseStatusException.class);
        assertThat(ex.getMessage()).isEqualTo("404 NOT_FOUND \"No matching handler\"");
    }).verify();
    // SPR-17475
    AtomicReference<Throwable> exceptionRef = new AtomicReference<>();
    StepVerifier.create(mono).consumeErrorWith(exceptionRef::set).verify();
    StepVerifier.create(mono).consumeErrorWith(ex -> assertThat(ex).isNotSameAs(exceptionRef.get())).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.jupiter.api.Test)

Example 88 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class DispatcherHandlerErrorTests method controllerReturnsMonoError.

@Test
public void controllerReturnsMonoError() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/error-signal"));
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> assertThat(error).isSameAs(EXCEPTION)).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 89 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class DefaultRenderingResponseTests method render.

@Test
public void render() {
    Map<String, Object> model = Collections.singletonMap("foo", "bar");
    Mono<RenderingResponse> result = RenderingResponse.create("view").modelAttributes(model).build();
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));
    ViewResolver viewResolver = mock(ViewResolver.class);
    View view = mock(View.class);
    given(viewResolver.resolveViewName("view", Locale.ENGLISH)).willReturn(Mono.just(view));
    given(view.render(model, null, exchange)).willReturn(Mono.empty());
    List<ViewResolver> viewResolvers = new ArrayList<>();
    viewResolvers.add(viewResolver);
    HandlerStrategies mockConfig = mock(HandlerStrategies.class);
    given(mockConfig.viewResolvers()).willReturn(viewResolvers);
    StepVerifier.create(result).expectNextMatches(response -> "view".equals(response.name()) && model.equals(response.model())).expectComplete().verify();
}
Also used : ArrayList(java.util.ArrayList) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) View(org.springframework.web.reactive.result.view.View) AbstractView(org.springframework.web.reactive.result.view.AbstractView) Test(org.junit.jupiter.api.Test)

Example 90 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class DispatcherHandlerTests method handlerMappingOrder.

@Test
void handlerMappingOrder() {
    HandlerMapping hm1 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
    HandlerMapping hm2 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) hm1).getOrder()).willReturn(1);
    given(((Ordered) hm2).getOrder()).willReturn(2);
    given((hm1).getHandler(any())).willReturn(Mono.just((Supplier<String>) () -> "1"));
    given((hm2).getHandler(any())).willReturn(Mono.just((Supplier<String>) () -> "2"));
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBean("b2", HandlerMapping.class, () -> hm2);
    context.registerBean("b1", HandlerMapping.class, () -> hm1);
    context.registerBean(HandlerAdapter.class, SupplierHandlerAdapter::new);
    context.registerBean(HandlerResultHandler.class, StringHandlerResultHandler::new);
    context.refresh();
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
    new DispatcherHandler(context).handle(exchange).block(Duration.ofSeconds(0));
    assertThat(exchange.getResponse().getBodyAsString().block(Duration.ofSeconds(5))).isEqualTo("1");
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Ordered(org.springframework.core.Ordered) Supplier(java.util.function.Supplier) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)224 Test (org.junit.jupiter.api.Test)216 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)61 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HttpHeaders (org.springframework.http.HttpHeaders)25 Resource (org.springframework.core.io.Resource)24 HandlerResult (org.springframework.web.reactive.HandlerResult)23 MethodParameter (org.springframework.core.MethodParameter)22 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 MediaType (org.springframework.http.MediaType)19 Mono (reactor.core.publisher.Mono)18 BeforeEach (org.junit.jupiter.api.BeforeEach)17 HttpMethod (org.springframework.http.HttpMethod)15 StepVerifier (reactor.test.StepVerifier)13 Arrays (java.util.Arrays)12 Collections (java.util.Collections)12 List (java.util.List)12 HttpStatus (org.springframework.http.HttpStatus)12 IOException (java.io.IOException)10