Search in sources :

Example 1 with MockClientHttpRequest

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

the class DefaultClientRequestBuilderTests method bodyInserter.

@Test
public void bodyInserter() throws Exception {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
        DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest result = ClientRequest.method(POST, URI.create("http://example.com")).body(inserter).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    when(strategies.messageWriters()).thenReturn(messageWriters::stream);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertNotNull(request.getBody());
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ByteBuffer(java.nio.ByteBuffer) BodyInserter(org.springframework.web.reactive.function.BodyInserter) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) Assert(org.junit.Assert) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) ByteBuffer(java.nio.ByteBuffer) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 2 with MockClientHttpRequest

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

the class BodyInsertersTests method ofFormData.

@Test
public void ofFormData() throws Exception {
    MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.set("name 1", "value 1");
    body.add("name 2", "value 2+1");
    body.add("name 2", "value 2+2");
    body.add("name 3", null);
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData(body);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("http://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);
        assertArrayEquals("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8), resultBytes);
    }).expectComplete().verify();
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 3 with MockClientHttpRequest

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

the class DefaultClientRequestBuilderTests method build.

@Test
public void build() throws Exception {
    ClientRequest result = ClientRequest.method(GET, URI.create("http://example.com")).header("MyKey", "MyValue").cookie("foo", "bar").build();
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    result.writeTo(request, strategies).block();
    assertEquals("MyValue", request.getHeaders().getFirst("MyKey"));
    assertEquals("bar", request.getCookies().getFirst("foo").getValue());
    StepVerifier.create(request.getBody()).expectComplete().verify();
}
Also used : MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 MockClientHttpRequest (org.springframework.mock.http.client.reactive.test.MockClientHttpRequest)3 ClientHttpRequest (org.springframework.http.client.reactive.ClientHttpRequest)2 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Assert (org.junit.Assert)1 Mockito (org.mockito.Mockito)1 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 HttpMethod (org.springframework.http.HttpMethod)1 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)1 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1 BodyInserter (org.springframework.web.reactive.function.BodyInserter)1 Mono (reactor.core.publisher.Mono)1