use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class ResourceHandlerFunctionTests method get.
@Test
public void get() throws IOException {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));
MockServerHttpResponse mockResponse = exchange.getResponse();
ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults().messageReaders());
Mono<ServerResponse> responseMono = this.handlerFunction.handle(request);
Mono<Void> result = responseMono.flatMap(response -> {
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
boolean condition = response instanceof EntityResponse;
assertThat(condition).isTrue();
@SuppressWarnings("unchecked") EntityResponse<Resource> entityResponse = (EntityResponse<Resource>) response;
assertThat(entityResponse.entity()).isEqualTo(this.resource);
return response.writeTo(exchange, context);
});
StepVerifier.create(result).expectComplete().verify();
byte[] expectedBytes = Files.readAllBytes(this.resource.getFile().toPath());
StepVerifier.create(mockResponse.getBody()).consumeNextWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
assertThat(resultBytes).isEqualTo(expectedBytes);
}).expectComplete().verify();
assertThat(mockResponse.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
assertThat(mockResponse.getHeaders().getContentLength()).isEqualTo(this.resource.contentLength());
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class ResourceHandlerFunctionTests method options.
@Test
public void options() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options("http://localhost"));
MockServerHttpResponse mockResponse = exchange.getResponse();
ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults().messageReaders());
Mono<ServerResponse> responseMono = this.handlerFunction.handle(request);
Mono<Void> result = responseMono.flatMap(response -> {
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.headers().getAllow()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS));
return response.writeTo(exchange, context);
});
StepVerifier.create(result).expectComplete().verify();
assertThat(mockResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(mockResponse.getHeaders().getAllow()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS));
StepVerifier.create(mockResponse.getBody()).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method notModifiedLastModified.
@Test
public void notModifiedLastModified() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
EntityResponse<String> responseMono = EntityResponse.fromObject("bar").lastModified(oneMinuteBeforeNow).build().block();
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)).build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
responseMono.writeTo(exchange, DefaultServerResponseBuilderTests.EMPTY_CONTEXT);
MockServerHttpResponse response = exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
StepVerifier.create(response.getBody()).expectError(IllegalStateException.class).verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method notModifiedLastModified.
@Test
public void notModifiedLastModified() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
ServerResponse responseMono = ServerResponse.ok().lastModified(oneMinuteBeforeNow).bodyValue("bar").block();
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)).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();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.
@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() {
HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
request -> Mono.just(new ServerResponse() {
@Override
public HttpStatus statusCode() {
return HttpStatus.OK;
}
@Override
public int rawStatusCode() {
return 200;
}
@Override
public HttpHeaders headers() {
return new HttpHeaders();
}
@Override
public MultiValueMap<String, ResponseCookie> cookies() {
return new LinkedMultiValueMap<>();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
});
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.NOT_FOUND);
}
Aggregations