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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations