use of cn.taketoday.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.
the class DefaultClientRequestBuilderTests method build.
@Test
public void build() {
ClientRequest result = ClientRequest.create(GET, DEFAULT_URL).header("MyKey", "MyValue").cookie("foo", "bar").httpRequest(request -> {
MockClientHttpRequest nativeRequest = request.getNativeRequest();
nativeRequest.getHeaders().add("MyKey2", "MyValue2");
}).build();
MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
ExchangeStrategies strategies = mock(ExchangeStrategies.class);
result.writeTo(request, strategies).block();
assertThat(request.getHeaders().getFirst("MyKey")).isEqualTo("MyValue");
assertThat(request.getHeaders().getFirst("MyKey2")).isEqualTo("MyValue2");
assertThat(request.getCookies().getFirst("foo").getValue()).isEqualTo("bar");
StepVerifier.create(request.getBody()).expectComplete().verify();
}
use of cn.taketoday.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.
the class DefaultClientRequestBuilderTests method fromCopiesBody.
@Test
public void fromCopiesBody() {
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 other = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
ClientRequest result = ClientRequest.from(other).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(POST, "/");
result.writeTo(request, strategies).block();
String copiedBody = request.getBodyAsString().block();
assertThat(copiedBody).isEqualTo("foo");
}
use of cn.taketoday.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.
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();
}
use of cn.taketoday.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.
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 cn.taketoday.http.client.reactive.MockClientHttpRequest in project today-framework by TAKETODAY.
the class DefaultClientRequestBuilderTests method bodyParameterizedTypeReference.
@Test
public void bodyParameterizedTypeReference() {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
TypeReference<String> typeReference = new TypeReference<>() {
};
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();
}
Aggregations