Search in sources :

Example 1 with MockClientHttpRequest

use of cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest in project today-infrastructure by TAKETODAY.

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(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 2 with MockClientHttpRequest

use of cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest in project today-infrastructure by TAKETODAY.

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) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseCookie(cn.taketoday.http.ResponseCookie) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) HttpRequest(cn.taketoday.http.HttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with MockClientHttpRequest

use of cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.

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(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 4 with MockClientHttpRequest

use of cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.

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) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StandardCharsets(java.nio.charset.StandardCharsets) ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpStatusCode(cn.taketoday.http.HttpStatusCode) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) HttpStatus(cn.taketoday.http.HttpStatus) HttpRequest(cn.taketoday.http.HttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with MockClientHttpRequest

use of cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.

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(cn.taketoday.core.LinkedMultiValueMap) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

MockClientHttpRequest (cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest)12 Test (org.junit.jupiter.api.Test)12 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)8 MultiValueMap (cn.taketoday.core.MultiValueMap)4 ClientHttpRequest (cn.taketoday.http.client.reactive.ClientHttpRequest)4 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 Resource (cn.taketoday.core.io.Resource)2 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)2 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 HttpMethod (cn.taketoday.http.HttpMethod)2 HttpRequest (cn.taketoday.http.HttpRequest)2 HttpStatus (cn.taketoday.http.HttpStatus)2 ResponseCookie (cn.taketoday.http.ResponseCookie)2 MockClientHttpResponse (cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Flux (reactor.core.publisher.Flux)2 StepVerifier (reactor.test.StepVerifier)2 HttpStatusCode (cn.taketoday.http.HttpStatusCode)1