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