Search in sources :

Example 31 with MockServerHttpRequest

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();
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 32 with MockServerHttpRequest

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);
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 33 with MockServerHttpRequest

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();
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 34 with MockServerHttpRequest

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();
    }
}
Also used : PathContainer(org.springframework.http.server.PathContainer) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)

Example 35 with MockServerHttpRequest

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();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) HttpCookie(org.springframework.http.HttpCookie) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) URI(java.net.URI) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) BodyExtractors.toMono(org.springframework.web.reactive.function.BodyExtractors.toMono) StringDecoder(org.springframework.core.codec.StringDecoder) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Optional(java.util.Optional) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) ServerWebInputException(org.springframework.web.server.ServerWebInputException) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Retention(java.lang.annotation.Retention) OptionalLong(java.util.OptionalLong) Charset(java.nio.charset.Charset) ValueSource(org.junit.jupiter.params.provider.ValueSource) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) HttpRange(org.springframework.http.HttpRange) ElementType(java.lang.annotation.ElementType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RetentionPolicy(java.lang.annotation.RetentionPolicy) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Part(org.springframework.http.codec.multipart.Part) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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