Search in sources :

Example 16 with MockServerHttpResponse

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

Example 17 with MockServerHttpResponse

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

Example 18 with MockServerHttpResponse

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());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.Test)

Example 19 with MockServerHttpResponse

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());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 20 with MockServerHttpResponse

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

Aggregations

MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)42 Test (org.junit.Test)37 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)14 HttpHandler (org.springframework.http.server.reactive.HttpHandler)9 HttpHeaders (org.springframework.http.HttpHeaders)8 HttpStatus (org.springframework.http.HttpStatus)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)8 Mono (reactor.core.publisher.Mono)8 StepVerifier (reactor.test.StepVerifier)8 MediaType (org.springframework.http.MediaType)7 Assert (org.junit.Assert)6 Mockito (org.mockito.Mockito)6 ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)6 ResponseStatusException (org.springframework.web.server.ResponseStatusException)6 HashMap (java.util.HashMap)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)4 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)4 Flux (reactor.core.publisher.Flux)4 Before (org.junit.Before)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3