use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class HandlerMethodMappingTests method ambiguousMatchOnPreFlightRequestWithCorsConfig.
// gh-26490
@Test
public void ambiguousMatchOnPreFlightRequestWithCorsConfig() throws Exception {
this.mapping.registerMapping("/f?o", this.handler, this.method1);
this.mapping.registerMapping("/fo?", this.handler, this.handler.getClass().getMethod("corsHandlerMethod"));
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options("https://example.org/foo").header(HttpHeaders.ORIGIN, "https://domain.com").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET"));
this.mapping.getHandler(exchange).block();
MockServerHttpResponse response = exchange.getResponse();
assertThat(response.getStatusCode()).isNull();
assertThat(response.getHeaders().getAccessControlAllowOrigin()).isEqualTo("https://domain.com");
assertThat(response.getHeaders().getAccessControlAllowMethods()).containsExactly(HttpMethod.GET);
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class ResourceWebHandlerTests method getResourceNoCache.
@Test
public void getResourceNoCache() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.setCacheControl(CacheControl.noStore());
this.handler.handle(exchange).block(TIMEOUT);
MockServerHttpResponse response = exchange.getResponse();
assertThat(response.getHeaders().getCacheControl()).isEqualTo("no-store");
assertThat(response.getHeaders().containsKey("Last-Modified")).isTrue();
assertThat(resourceLastModifiedDate("test/foo.css") / 1000).isEqualTo(response.getHeaders().getLastModified() / 1000);
assertThat(response.getHeaders().getFirst("Accept-Ranges")).isEqualTo("bytes");
assertThat(response.getHeaders().get("Accept-Ranges").size()).isEqualTo(1);
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class AbstractResponseStatusExceptionHandlerTests method handleResponseStatusExceptionWithHeaders.
// gh-23741
@Test
public void handleResponseStatusExceptionWithHeaders() {
Throwable ex = new NotAcceptableStatusException(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML));
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
MockServerHttpResponse response = this.exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
assertThat(response.getHeaders().getAccept()).containsOnly(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML);
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class AbstractResponseStatusExceptionHandlerTests method handleMethodNotAllowed.
// gh-23741
@Test
public void handleMethodNotAllowed() {
Throwable ex = new MethodNotAllowedException(HttpMethod.PATCH, Arrays.asList(HttpMethod.POST, HttpMethod.PUT));
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
MockServerHttpResponse response = this.exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED);
assertThat(response.getHeaders().getAllow()).containsOnly(HttpMethod.POST, HttpMethod.PUT);
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class ResourceHandlerFunctionTests method head.
@Test
public void head() throws IOException {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.head("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().getFilename()).isEqualTo(this.resource.getFilename());
return response.writeTo(exchange, context);
});
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(mockResponse.getBody()).expectComplete().verify();
assertThat(mockResponse.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
assertThat(mockResponse.getHeaders().getContentLength()).isEqualTo(this.resource.contentLength());
}
Aggregations