Search in sources :

Example 6 with MockServerHttpRequest

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

the class RequestMappingInfoTests method matchProducesCondition.

@Test
public void matchProducesCondition() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/foo").accept(MediaType.TEXT_PLAIN).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    RequestMappingInfo info = paths("/foo").produces("text/plain").build();
    RequestMappingInfo match = info.getMatchingCondition(exchange);
    assertThat(match).isNotNull();
    info = paths("/foo").produces("application/xml").build();
    match = info.getMatchingCondition(exchange);
    assertThat(match).isNull();
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) Test(org.junit.jupiter.api.Test)

Example 7 with MockServerHttpRequest

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

the class RouterFunctionTests method andRoute.

@Test
public void andRoute() {
    RouterFunction<ServerResponse> routerFunction1 = request -> Mono.empty();
    RequestPredicate requestPredicate = request -> true;
    RouterFunction<ServerResponse> result = routerFunction1.andRoute(requestPredicate, this::handlerMethod);
    assertThat(result).isNotNull();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectNextCount(1).expectComplete().verify();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 8 with MockServerHttpRequest

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

the class RouterFunctionTests method andOther.

@Test
public void andOther() {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().bodyValue("42");
    RouterFunction<?> routerFunction1 = request -> Mono.empty();
    RouterFunction<ServerResponse> routerFunction2 = request -> Mono.just(handlerFunction);
    RouterFunction<?> result = routerFunction1.andOther(routerFunction2);
    assertThat(result).isNotNull();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectNextMatches(o -> o.equals(handlerFunction)).expectComplete().verify();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 9 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest 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 10 with MockServerHttpRequest

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

Aggregations

MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)182 Test (org.junit.jupiter.api.Test)160 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)101 Mono (reactor.core.publisher.Mono)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)50 Collections (java.util.Collections)48 StepVerifier (reactor.test.StepVerifier)46 HttpHeaders (org.springframework.http.HttpHeaders)39 Flux (reactor.core.publisher.Flux)37 MediaType (org.springframework.http.MediaType)34 ServerWebExchange (org.springframework.web.server.ServerWebExchange)34 MultiValueMap (org.springframework.util.MultiValueMap)33 HttpStatus (org.springframework.http.HttpStatus)31 StandardCharsets (java.nio.charset.StandardCharsets)29 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)29 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)27 ClassPathResource (org.springframework.core.io.ClassPathResource)25 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)24 Optional (java.util.Optional)23