use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class RouterFunctionsTests method nestMatch.
@Test
public void nestMatch() {
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
RouterFunction<ServerResponse> routerFunction = request -> Mono.just(handlerFunction);
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
RequestPredicate requestPredicate = mock(RequestPredicate.class);
given(requestPredicate.nest(request)).willReturn(Optional.of(request));
RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction);
assertThat(result).isNotNull();
Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request);
StepVerifier.create(resultHandlerFunction).expectNext(handlerFunction).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest 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);
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest 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();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class SimpleUrlHandlerMappingTests method testUrl.
void testUrl(String url, Object bean, HandlerMapping handlerMapping, String pathWithinMapping) {
MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, URI.create(url)).build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
Object actual = handlerMapping.getHandler(exchange).block();
if (bean != null) {
assertThat(actual).isNotNull();
assertThat(actual).isSameAs(bean);
// noinspection OptionalGetWithoutIsPresent
PathContainer path = exchange.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
assertThat(path).isNotNull();
assertThat(path.value()).isEqualTo(pathWithinMapping);
} else {
assertThat(actual).isNull();
}
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class DefaultServerRequestTests method multipartData.
@Test
public void multipartData() {
String data = "--12345\r\n" + "Content-Disposition: form-data; name=\"foo\"\r\n" + "\r\n" + "bar\r\n" + "--12345\r\n" + "Content-Disposition: form-data; name=\"baz\"\r\n" + "\r\n" + "qux\r\n" + "--12345--\r\n";
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=12345");
MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "https://example.com").headers(httpHeaders).body(body);
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<MultiValueMap<String, Part>> resultData = request.multipartData();
StepVerifier.create(resultData).consumeNextWith(formData -> {
assertThat(formData.size()).isEqualTo(2);
Part part = formData.getFirst("foo");
boolean condition1 = part instanceof FormFieldPart;
assertThat(condition1).isTrue();
FormFieldPart formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("bar");
part = formData.getFirst("baz");
boolean condition = part instanceof FormFieldPart;
assertThat(condition).isTrue();
formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("qux");
}).verifyComplete();
}
Aggregations