use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.
the class ResourceHandlerFunctionTests method head.
@Test
public void head() throws IOException {
MockServerWebExchange exchange = MockServerHttpRequest.head("http://localhost").toExchange();
MockServerHttpResponse mockResponse = exchange.getResponse();
ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults());
Mono<ServerResponse> response = this.handlerFunction.handle(request);
Mono<Void> result = response.then(res -> {
assertEquals(HttpStatus.OK, res.statusCode());
return res.writeTo(exchange, HandlerStrategies.withDefaults());
});
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(mockResponse.getBody()).expectComplete().verify();
assertEquals(MediaType.TEXT_PLAIN, mockResponse.getHeaders().getContentType());
assertEquals(this.resource.contentLength(), mockResponse.getHeaders().getContentLength());
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.
the class ResourceHandlerFunctionTests method options.
@Test
public void options() {
MockServerWebExchange exchange = MockServerHttpRequest.options("http://localhost").toExchange();
MockServerHttpResponse mockResponse = exchange.getResponse();
ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults());
Mono<ServerResponse> responseMono = this.handlerFunction.handle(request);
Mono<Void> result = responseMono.then(response -> {
assertEquals(HttpStatus.OK, response.statusCode());
assertEquals(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), response.headers().getAllow());
return response.writeTo(exchange, HandlerStrategies.withDefaults());
});
StepVerifier.create(result).expectComplete().verify();
assertEquals(HttpStatus.OK, mockResponse.getStatusCode());
assertEquals(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), mockResponse.getHeaders().getAllow());
StepVerifier.create(mockResponse.getBody()).expectComplete().verify();
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.
@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() throws Exception {
HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
request -> Mono.<ServerResponse>just(new ServerResponse() {
@Override
public HttpStatus statusCode() {
return HttpStatus.OK;
}
@Override
public HttpHeaders headers() {
return new HttpHeaders();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
});
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertNotNull(result);
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertEquals(HttpStatus.NOT_FOUND, httpResponse.getStatusCode());
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerReturnsException.
@Test
public void toHttpHandlerHandlerReturnsException() throws Exception {
HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new IllegalStateException());
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertNotNull(result);
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, httpResponse.getStatusCode());
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.
the class ResourceWebHandlerTests method getResourceNoCache.
@Test
public void getResourceNoCache() throws Exception {
MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.setCacheControl(CacheControl.noStore());
this.handler.handle(exchange).block(TIMEOUT);
MockServerHttpResponse response = exchange.getResponse();
assertEquals("no-store", response.getHeaders().getCacheControl());
assertTrue(response.getHeaders().containsKey("Last-Modified"));
assertEquals(response.getHeaders().getLastModified() / 1000, resourceLastModifiedDate("test/foo.css") / 1000);
assertEquals("bytes", response.getHeaders().getFirst("Accept-Ranges"));
assertEquals(1, response.getHeaders().get("Accept-Ranges").size());
}
Aggregations