Search in sources :

Example 11 with MockClientHttpRequest

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

the class WebExchangeDataBinderTests method exchangeMultipart.

private ServerWebExchange exchangeMultipart(MultiValueMap<String, ?> multipartData) {
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/");
    new MultipartHttpMessageWriter().write(Mono.just(multipartData), forClass(MultiValueMap.class), MediaType.MULTIPART_FORM_DATA, request, Collections.emptyMap()).block();
    return MockServerWebExchange.from(MockServerHttpRequest.post("/").contentType(request.getHeaders().getContentType()).body(request.getBody()));
}
Also used : MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter)

Example 12 with MockClientHttpRequest

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

the class BodyInsertersTests method fromFormDataWith.

@Test
public void fromFormDataWith() {
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData("name 1", "value 1").with("name 2", "value 2+1").with("name 2", "value 2+2").with("name 3", null);
    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(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8));
    }).expectComplete().verify();
}
Also used : 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) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 13 with MockClientHttpRequest

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

the class DefaultClientRequestBuilderTests method build.

@Test
public void build() {
    ClientRequest result = ClientRequest.create(GET, DEFAULT_URL).header("MyKey", "MyValue").cookie("foo", "bar").httpRequest(request -> {
        MockClientHttpRequest nativeRequest = request.getNativeRequest();
        nativeRequest.getHeaders().add("MyKey2", "MyValue2");
    }).build();
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    result.writeTo(request, strategies).block();
    assertThat(request.getHeaders().getFirst("MyKey")).isEqualTo("MyValue");
    assertThat(request.getHeaders().getFirst("MyKey2")).isEqualTo("MyValue2");
    assertThat(request.getCookies().getFirst("foo").getValue()).isEqualTo("bar");
    StepVerifier.create(request.getBody()).expectComplete().verify();
}
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) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 14 with MockClientHttpRequest

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

the class DefaultClientRequestBuilderTests method fromCopiesBody.

@Test
public void fromCopiesBody() {
    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 other = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    ClientRequest result = ClientRequest.from(other).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(POST, "/");
    result.writeTo(request, strategies).block();
    String copiedBody = request.getBodyAsString().block();
    assertThat(copiedBody).isEqualTo("foo");
}
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)

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