use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerReturnsException.
@Test
public void toHttpHandlerHandlerReturnsException() {
HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new IllegalStateException());
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertThat(result).isNotNull();
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerWebFilter.
@Test
public void toHttpHandlerWebFilter() {
AtomicBoolean filterInvoked = new AtomicBoolean();
WebFilter webFilter = (exchange, chain) -> {
filterInvoked.set(true);
return chain.filter(exchange);
};
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HandlerStrategies handlerStrategies = HandlerStrategies.builder().webFilter(webFilter).build();
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies);
assertThat(result).isNotNull();
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(filterInvoked.get()).isTrue();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method overwriteHeaders.
@Test
public void overwriteHeaders() {
ServerResponse serverResponse = ServerResponse.ok().headers(headers -> headers.set("Foo", "Bar")).build().block();
assertThat(serverResponse).isNotNull();
MockServerWebExchange mockExchange = MockServerWebExchange.builder(MockServerHttpRequest.get("https://example.org")).build();
MockServerHttpResponse response = mockExchange.getResponse();
response.getHeaders().set("Foo", "Baz");
serverResponse.writeTo(mockExchange, EMPTY_CONTEXT).block();
assertThat(response.getHeaders().getFirst("Foo")).isEqualTo("Bar");
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method buildVoidPublisher.
@Test
public void buildVoidPublisher() {
Mono<Void> mono = Mono.empty();
Mono<ServerResponse> result = ServerResponse.ok().build(mono);
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block();
MockServerHttpResponse response = exchange.getResponse();
StepVerifier.create(response.getBody()).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method notModifiedEtag.
@Test
public void notModifiedEtag() {
String etag = "\"foo\"";
ServerResponse responseMono = ServerResponse.ok().eTag(etag).bodyValue("bar").block();
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_NONE_MATCH, etag).build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
responseMono.writeTo(exchange, EMPTY_CONTEXT);
MockServerHttpResponse response = exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
StepVerifier.create(response.getBody()).expectError(IllegalStateException.class).verify();
}
Aggregations