Search in sources :

Example 6 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange 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();
}
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 7 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange 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();
}
Also used : ZonedDateTime(java.time.ZonedDateTime) 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 8 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class DefaultEntityResponseBuilderTests method bodyInserter.

@Test
public void bodyInserter() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));
    ServerResponse.Context context = new ServerResponse.Context() {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
        }

        @Override
        public List<ViewResolver> viewResolvers() {
            return Collections.emptyList();
        }
    };
    StepVerifier.create(result).consumeNextWith(response -> {
        StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
        response.writeTo(exchange, context);
    }).expectComplete().verify();
    assertThat(exchange.getResponse().getBody()).isNotNull();
}
Also used : EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Test(org.junit.jupiter.api.Test)

Example 9 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class DefaultServerRequestBuilderTests method from.

@Test
public void from() {
    MockServerHttpRequest request = MockServerHttpRequest.post("https://example.com").header("foo", "bar").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    ServerRequest other = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders());
    other.attributes().put("attr1", "value1");
    Flux<DataBuffer> body = Flux.just("baz").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
    ServerRequest result = ServerRequest.from(other).method(HttpMethod.HEAD).headers(httpHeaders -> httpHeaders.set("foo", "baar")).cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())).attribute("attr2", "value2").attributes(attributes -> attributes.put("attr3", "value3")).body(body).build();
    assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
    assertThat(result.headers().asHttpHeaders()).hasSize(1);
    assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
    assertThat(result.cookies()).hasSize(1);
    assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
    assertThat(result.attributes()).containsOnlyKeys(ServerWebExchange.LOG_ID_ATTRIBUTE, "attr1", "attr2", "attr3");
    assertThat(result.attributes()).contains(entry("attr1", "value1"), entry("attr2", "value2"), entry("attr3", "value3"));
    StepVerifier.create(result.bodyToFlux(String.class)).expectNext("baz").verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ResponseCookie(org.springframework.http.ResponseCookie) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange 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();
}
Also used : ZonedDateTime(java.time.ZonedDateTime) 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

MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)224 Test (org.junit.jupiter.api.Test)216 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)61 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HttpHeaders (org.springframework.http.HttpHeaders)25 Resource (org.springframework.core.io.Resource)24 HandlerResult (org.springframework.web.reactive.HandlerResult)23 MethodParameter (org.springframework.core.MethodParameter)22 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 MediaType (org.springframework.http.MediaType)19 Mono (reactor.core.publisher.Mono)18 BeforeEach (org.junit.jupiter.api.BeforeEach)17 HttpMethod (org.springframework.http.HttpMethod)15 StepVerifier (reactor.test.StepVerifier)13 Arrays (java.util.Arrays)12 Collections (java.util.Collections)12 List (java.util.List)12 HttpStatus (org.springframework.http.HttpStatus)12 IOException (java.io.IOException)10