Search in sources :

Example 1 with MockServerHttpResponse

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);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with MockServerHttpResponse

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);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with MockServerHttpResponse

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);
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockServerHttpResponse

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);
}
Also used : MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with MockServerHttpResponse

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());
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)64 Test (org.junit.jupiter.api.Test)55 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)28 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)24 HttpHandler (org.springframework.http.server.reactive.HttpHandler)14 HttpHeaders (org.springframework.http.HttpHeaders)13 Flux (reactor.core.publisher.Flux)13 Mono (reactor.core.publisher.Mono)13 Collections (java.util.Collections)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 DataBuffer (org.springframework.core.io.buffer.DataBuffer)11 MultiValueMap (org.springframework.util.MultiValueMap)11 StandardCharsets (java.nio.charset.StandardCharsets)10 HttpStatus (org.springframework.http.HttpStatus)10 ResponseCookie (org.springframework.http.ResponseCookie)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)10 WebFilter (org.springframework.web.server.WebFilter)10 StepVerifier (reactor.test.StepVerifier)10