Search in sources :

Example 21 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange 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 22 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange 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 23 with MockServerWebExchange

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

Example 24 with MockServerWebExchange

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

the class RequestPredicatesTests method all.

@Test
public void all() {
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    MockServerWebExchange mockExchange = MockServerWebExchange.from(mockRequest);
    RequestPredicate predicate = RequestPredicates.all();
    ServerRequest request = new DefaultServerRequest(mockExchange, Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 25 with MockServerWebExchange

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

the class CachingResourceResolverTests method resolveResourceNoAcceptEncoding.

@Test
public void resolveResourceNoAcceptEncoding() {
    String file = "bar.css";
    MockServerWebExchange exchange = MockServerWebExchange.from(get(file));
    Resource expected = this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT);
    String cacheKey = resourceKey(file);
    Object actual = this.cache.get(cacheKey).get();
    assertThat(actual).isEqualTo(expected);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) 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