Search in sources :

Example 11 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerHandlerReturnsException.

@Test
public void toHttpHandlerHandlerReturnsException() {
    HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new IllegalStateException());
    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.INTERNAL_SERVER_ERROR);
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 12 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerWebFilter.

@Test
public void toHttpHandlerWebFilter() {
    AtomicBoolean filterInvoked = new AtomicBoolean();
    WebFilter webFilter = (exchange, chain) -> {
        filterInvoked.set(true);
        return chain.filter(exchange);
    };
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HandlerStrategies handlerStrategies = HandlerStrategies.builder().webFilter(webFilter).build();
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
    assertThat(filterInvoked.get()).isTrue();
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 13 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class DefaultServerResponseBuilderTests method overwriteHeaders.

@Test
public void overwriteHeaders() {
    ServerResponse serverResponse = ServerResponse.ok().headers(headers -> headers.set("Foo", "Bar")).build().block();
    assertThat(serverResponse).isNotNull();
    MockServerWebExchange mockExchange = MockServerWebExchange.builder(MockServerHttpRequest.get("https://example.org")).build();
    MockServerHttpResponse response = mockExchange.getResponse();
    response.getHeaders().set("Foo", "Baz");
    serverResponse.writeTo(mockExchange, EMPTY_CONTEXT).block();
    assertThat(response.getHeaders().getFirst("Foo")).isEqualTo("Bar");
}
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 14 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class DefaultServerResponseBuilderTests method buildVoidPublisher.

@Test
public void buildVoidPublisher() {
    Mono<Void> mono = Mono.empty();
    Mono<ServerResponse> result = ServerResponse.ok().build(mono);
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block();
    MockServerHttpResponse response = exchange.getResponse();
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) CacheControl(org.springframework.http.CacheControl) URI(java.net.URI) ResponseCookie(org.springframework.http.ResponseCookie) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 15 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class DefaultServerResponseBuilderTests method notModifiedEtag.

@Test
public void notModifiedEtag() {
    String etag = "\"foo\"";
    ServerResponse responseMono = ServerResponse.ok().eTag(etag).bodyValue("bar").block();
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_NONE_MATCH, etag).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();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) 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