Search in sources :

Example 6 with MockClientHttpRequest

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

the class DefaultClientRequestBuilderTests method bodyInserter.

@Test
public void bodyInserter() {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertThat(request.getBody()).isNotNull();
    StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.http.HttpMethod.GET) DELETE(org.springframework.http.HttpMethod.DELETE) BodyInserter(org.springframework.web.reactive.function.BodyInserter) ArrayList(java.util.ArrayList) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) BDDMockito.given(org.mockito.BDDMockito.given) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) OPTIONS(org.springframework.http.HttpMethod.OPTIONS) POST(org.springframework.http.HttpMethod.POST) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with MockClientHttpRequest

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

the class DefaultClientRequestBuilderTests method bodyParameterizedTypeReference.

@Test
public void bodyParameterizedTypeReference() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<>() {
    };
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(publisher, typeReference).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertThat(request.getBody()).isNotNull();
    StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
Also used : EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 8 with MockClientHttpRequest

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

the class DefaultClientResponseBuilderTests method mutate.

@Test
public void mutate() {
    Flux<DataBuffer> otherBody = Flux.just("foo", "bar").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
    HttpRequest mockClientHttpRequest = new MockClientHttpRequest(HttpMethod.GET, "/path");
    MockClientHttpResponse httpResponse = new MockClientHttpResponse(HttpStatus.OK);
    httpResponse.getHeaders().add("foo", "bar");
    httpResponse.getHeaders().add("bar", "baz");
    httpResponse.getCookies().add("baz", ResponseCookie.from("baz", "qux").build());
    httpResponse.setBody(otherBody);
    DefaultClientResponse otherResponse = new DefaultClientResponse(httpResponse, ExchangeStrategies.withDefaults(), "my-prefix", "", () -> mockClientHttpRequest);
    ClientResponse result = otherResponse.mutate().statusCode(HttpStatus.BAD_REQUEST).headers(headers -> headers.set("foo", "baar")).cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())).build();
    assertThat(result.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
    assertThat(result.headers().asHttpHeaders().size()).isEqualTo(3);
    assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
    assertThat(result.headers().asHttpHeaders().getFirst("bar")).isEqualTo("baz");
    assertThat(result.cookies().size()).isEqualTo(1);
    assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
    assertThat(result.logPrefix()).isEqualTo("my-prefix");
    StepVerifier.create(result.bodyToFlux(String.class)).expectNext("foobar").verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) HttpRequest(org.springframework.http.HttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ResponseCookie(org.springframework.http.ResponseCookie) HttpRequest(org.springframework.http.HttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 9 with MockClientHttpRequest

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

the class BodyInsertersTests method fromMultipartDataWithMultipleValues.

// SPR-16350
@Test
public void fromMultipartDataWithMultipleValues() {
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.put("name", Arrays.asList("value1", "value2"));
    BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData(map);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
    Mono<Void> result = inserter.insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(DataBufferUtils.join(request.getBody())).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        String content = new String(resultBytes, StandardCharsets.UTF_8);
        assertThat(content).contains("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + "Content-Length: 6\r\n" + "\r\n" + "value1");
        assertThat(content).contains("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + "Content-Length: 6\r\n" + "\r\n" + "value2");
    }).expectComplete().verify();
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 10 with MockClientHttpRequest

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

the class RequestPartMethodArgumentResolverTests method createExchange.

private ServerWebExchange createExchange(MultipartBodyBuilder builder) {
    MockClientHttpRequest clientRequest = new MockClientHttpRequest(HttpMethod.POST, "/");
    this.writer.write(Mono.just(builder.build()), forClass(MultiValueMap.class), MediaType.MULTIPART_FORM_DATA, clientRequest, Collections.emptyMap()).block();
    MediaType contentType = clientRequest.getHeaders().getContentType();
    Flux<DataBuffer> body = clientRequest.getBody();
    MockServerHttpRequest serverRequest = MockServerHttpRequest.post("/").contentType(contentType).body(body);
    return MockServerWebExchange.from(serverRequest);
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) MediaType(org.springframework.http.MediaType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

MockClientHttpRequest (org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest)14 Test (org.junit.jupiter.api.Test)11 ArrayList (java.util.ArrayList)5 DataBuffer (org.springframework.core.io.buffer.DataBuffer)5 ClientHttpRequest (org.springframework.http.client.reactive.ClientHttpRequest)5 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)5 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)4 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 StepVerifier (reactor.test.StepVerifier)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)3 List (java.util.List)3 BDDMockito.given (org.mockito.BDDMockito.given)3 Mockito.mock (org.mockito.Mockito.mock)3 Publisher (org.reactivestreams.Publisher)3 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)3