use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest in project spring-framework by spring-projects.
the class DefaultClientRequestBuilderTests method bodyInserter.
@Test
public void bodyInserter() {
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 result = ClientRequest.create(POST, DEFAULT_URL).body(inserter).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();
}
use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest in project spring-framework by spring-projects.
the class DefaultClientRequestBuilderTests method bodyParameterizedTypeReference.
@Test
public void bodyParameterizedTypeReference() {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<>() {
};
ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(publisher, typeReference).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();
}
use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest in project spring-framework by spring-projects.
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 org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest in project spring-framework by spring-projects.
the class BodyInsertersTests method fromMultipartDataWithMultipleValues.
// SPR-16350
@Test
public void fromMultipartDataWithMultipleValues() {
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.put("name", Arrays.asList("value1", "value2"));
BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData(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();
StepVerifier.create(DataBufferUtils.join(request.getBody())).consumeNextWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
DataBufferUtils.release(dataBuffer);
String content = new String(resultBytes, StandardCharsets.UTF_8);
assertThat(content).contains("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + "Content-Length: 6\r\n" + "\r\n" + "value1");
assertThat(content).contains("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + "Content-Length: 6\r\n" + "\r\n" + "value2");
}).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method createExchange.
private ServerWebExchange createExchange(MultipartBodyBuilder builder) {
MockClientHttpRequest clientRequest = new MockClientHttpRequest(HttpMethod.POST, "/");
this.writer.write(Mono.just(builder.build()), forClass(MultiValueMap.class), MediaType.MULTIPART_FORM_DATA, clientRequest, Collections.emptyMap()).block();
MediaType contentType = clientRequest.getHeaders().getContentType();
Flux<DataBuffer> body = clientRequest.getBody();
MockServerHttpRequest serverRequest = MockServerHttpRequest.post("/").contentType(contentType).body(body);
return MockServerWebExchange.from(serverRequest);
}
Aggregations