use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class RouterFunctionsTests method routeNoMatch.
@Test
public void routeNoMatch() {
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
RequestPredicate requestPredicate = mock(RequestPredicate.class);
given(requestPredicate.test(request)).willReturn(false);
RouterFunction<ServerResponse> result = RouterFunctions.route(requestPredicate, handlerFunction);
assertThat(result).isNotNull();
Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request);
StepVerifier.create(resultHandlerFunction).expectComplete().verify();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.
@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() {
HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
request -> Mono.just(new ServerResponse() {
@Override
public HttpStatus statusCode() {
return HttpStatus.OK;
}
@Override
public int rawStatusCode() {
return 200;
}
@Override
public HttpHeaders headers() {
return new HttpHeaders();
}
@Override
public MultiValueMap<String, ResponseCookie> cookies() {
return new LinkedMultiValueMap<>();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
});
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.NOT_FOUND);
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class DefaultServerRequestTests method checkNotModifiedTimestamp.
@ParameterizedHttpMethodTest
void checkNotModifiedTimestamp(String method) throws Exception {
Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
HttpHeaders headers = new HttpHeaders();
headers.setIfModifiedSince(now);
MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.valueOf(method), "/").headers(headers).build();
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<ServerResponse> result = request.checkNotModified(now);
StepVerifier.create(result).assertNext(serverResponse -> {
assertThat(serverResponse.statusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
assertThat(serverResponse.headers().getLastModified()).isEqualTo(now.toEpochMilli());
}).verifyComplete();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class DefaultServerRequestTests method checkNotModifiedETagAndTimestamp.
@ParameterizedHttpMethodTest
void checkNotModifiedETagAndTimestamp(String method) {
String eTag = "\"Foo\"";
Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
HttpHeaders headers = new HttpHeaders();
headers.setIfNoneMatch(eTag);
headers.setIfModifiedSince(now);
MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.valueOf(method), "/").headers(headers).build();
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<ServerResponse> result = request.checkNotModified(now, eTag);
StepVerifier.create(result).assertNext(serverResponse -> {
assertThat(serverResponse.statusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
assertThat(serverResponse.headers().getETag()).isEqualTo(eTag);
assertThat(serverResponse.headers().getLastModified()).isEqualTo(now.toEpochMilli());
}).verifyComplete();
}
use of reactor.core.publisher.Mono 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