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();
}
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();
}
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();
}
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();
}
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");
}
Aggregations