Search in sources :

Example 1 with MockClientHttpRequest

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

the class WebExchangeDataBinderTests method exchange.

private ServerWebExchange exchange(MultiValueMap<String, String> formData) {
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/");
    new FormHttpMessageWriter().write(Mono.just(formData), forClassWithGenerics(MultiValueMap.class, String.class, String.class), MediaType.APPLICATION_FORM_URLENCODED, request, Collections.emptyMap()).block();
    return MockServerWebExchange.from(MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_FORM_URLENCODED).body(request.getBody()));
}
Also used : MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter)

Example 2 with MockClientHttpRequest

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

the class BodyInsertersTests method fromFormDataMap.

@Test
public void fromFormDataMap() {
    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("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 : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) 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 3 with MockClientHttpRequest

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

the class BodyInsertersTests method fromMultipartData.

@Test
public void fromMultipartData() {
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.set("name 3", "value 3");
    BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData("name 1", "value 1").withPublisher("name 2", Flux.just("foo", "bar", "baz"), String.class).with(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();
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 4 with MockClientHttpRequest

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

the class BodyInsertersTests method ofResourceWithExplicitMediaType.

// gh-24366
@Test
public void ofResourceWithExplicitMediaType() throws IOException {
    Resource resource = new ClassPathResource("response.txt", getClass());
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/");
    request.getHeaders().setContentType(MediaType.TEXT_MARKDOWN);
    Mono<Void> result = BodyInserters.fromResource(resource).insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    byte[] expectedBytes = Files.readAllBytes(resource.getFile().toPath());
    assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_MARKDOWN);
    StepVerifier.create(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo(expectedBytes);
    }).expectComplete().verify();
}
Also used : Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 5 with MockClientHttpRequest

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

the class DefaultClientRequestBuilderTests method bodyClass.

@Test
public void bodyClass() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(publisher, String.class).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) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) 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