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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations