use of cn.taketoday.http.HttpMethod.POST in project today-framework by TAKETODAY.
the class RestTemplateTests method clientHttpRequestInitializerAndRequestInterceptorAreBothApplied.
@Test
void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
ClientHttpRequestInitializer initializer = request -> request.getHeaders().add("MyHeader", "MyInitializerValue");
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setHttpRequestInitializers(Collections.singletonList(initializer));
template.setInterceptors(interceptor);
MediaType contentType = MediaType.TEXT_PLAIN;
given(converter.canWrite(String.class, contentType)).willReturn(true);
HttpHeaders requestHeaders = HttpHeaders.create();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = HttpHeaders.create();
entityHeaders.setContentType(contentType);
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader")).contains("MyInterceptorValue", "MyInitializerValue");
verify(response).close();
}
use of cn.taketoday.http.HttpMethod.POST 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.HttpMethod.POST in project today-infrastructure by TAKETODAY.
the class RestTemplateTests method clientHttpRequestInitializerAndRequestInterceptorAreBothApplied.
@Test
void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
ClientHttpRequestInitializer initializer = request -> request.getHeaders().add("MyHeader", "MyInitializerValue");
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setHttpRequestInitializers(Collections.singletonList(initializer));
template.setInterceptors(interceptor);
MediaType contentType = MediaType.TEXT_PLAIN;
given(converter.canWrite(String.class, contentType)).willReturn(true);
HttpHeaders requestHeaders = HttpHeaders.create();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = HttpHeaders.create();
entityHeaders.setContentType(contentType);
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader")).contains("MyInterceptorValue", "MyInitializerValue");
verify(response).close();
}
use of cn.taketoday.http.HttpMethod.POST in project today-infrastructure 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.HttpMethod.POST in project today-infrastructure 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");
}
Aggregations