use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-security by spring-projects.
the class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests method getBody.
private static String getBody(ClientRequest request) {
final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
messageWriters.add(new ResourceHttpMessageWriter());
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));
BodyInserter.Context 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 new HashMap<>();
}
};
MockClientHttpRequest body = new MockClientHttpRequest(HttpMethod.GET, "/");
request.body().insert(body, context).block();
return body.getBodyAsString().block();
}
use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.
the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.
// gh-26232
@Test
void monoResponseShouldNotLeakIfCancelled() {
LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory);
response.setWriteHandler(flux -> {
throw AbortedException.beforeSend();
});
HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder());
Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), ResolvableType.forClass(Mono.class), ResolvableType.forClass(Map.class), null, request, response, Collections.emptyMap());
StepVerifier.create(result).expectError(AbortedException.class).verify();
bufferFactory.checkForLeaks();
}
use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method bodyInserter.
@Test
public void bodyInserter() {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));
ServerResponse.Context context = new ServerResponse.Context() {
@Override
public List<HttpMessageWriter<?>> messageWriters() {
return Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
}
@Override
public List<ViewResolver> viewResolvers() {
return Collections.emptyList();
}
};
StepVerifier.create(result).consumeNextWith(response -> {
StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
response.writeTo(exchange, context);
}).expectComplete().verify();
assertThat(exchange.getResponse().getBody()).isNotNull();
}
use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.
the class BaseDefaultCodecs method getBaseTypedWriters.
/**
* Return "base" typed writers only, i.e. common to client and server.
*/
final List<HttpMessageWriter<?>> getBaseTypedWriters() {
if (!this.registerDefaults) {
return Collections.emptyList();
}
List<HttpMessageWriter<?>> writers = new ArrayList<>();
addCodec(writers, new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
addCodec(writers, new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
addCodec(writers, new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
if (nettyByteBufPresent) {
addCodec(writers, new EncoderHttpMessageWriter<>(new NettyByteBufEncoder()));
}
addCodec(writers, new ResourceHttpMessageWriter());
addCodec(writers, new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
if (protobufPresent) {
addCodec(writers, new ProtobufHttpMessageWriter(this.protobufEncoder != null ? (ProtobufEncoder) this.protobufEncoder : new ProtobufEncoder()));
}
return writers;
}
use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.
the class ResponseEntityResultHandlerTests method handleWithProducibleContentTypeShouldFailWithServerError.
// gh-23287
@Test
public void handleWithProducibleContentTypeShouldFailWithServerError() {
ResponseEntity<String> value = ResponseEntity.ok().body("<foo/>");
MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
HandlerResult result = handlerResult(value, returnType);
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
Set<MediaType> mediaTypes = Collections.singleton(MediaType.APPLICATION_XML);
exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
ResponseEntityResultHandler resultHandler = new ResponseEntityResultHandler(Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())), new RequestedContentTypeResolverBuilder().build());
StepVerifier.create(resultHandler.handleResult(exchange, result)).consumeErrorWith(ex -> assertThat(ex).isInstanceOf(HttpMessageNotWritableException.class).hasMessageContaining("with preset Content-Type")).verify();
}
Aggregations