use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.
the class BodyInsertersTests method createContext.
@BeforeEach
public void createContext() {
final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
messageWriters.add(new ResourceHttpMessageWriter());
messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
messageWriters.add(new FormHttpMessageWriter());
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
messageWriters.add(new MultipartHttpMessageWriter(messageWriters));
this.context = new BodyInserter.Context() {
@Override
public List<HttpMessageWriter<?>> messageWriters() {
return messageWriters;
}
@Override
public Optional<ServerHttpRequest> serverRequest() {
return Optional.empty();
}
@Override
public Map<String, Object> hints() {
return hints;
}
};
this.hints = new HashMap<>();
}
use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.
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