use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.
the class ServerSentEventHttpMessageWriterTests method writePojoWithPrettyPrint.
@ParameterizedDataBufferAllocatingTest
// SPR-14899
void writePojoWithPrettyPrint(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
this.messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder(mapper));
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
testWrite(source, outputMessage, Pojo.class);
StepVerifier.create(outputMessage.getBody()).consumeNextWith(stringConsumer("data:")).consumeNextWith(stringConsumer("{\n" + "data: \"foo\" : \"foofoo\",\n" + "data: \"bar\" : \"barbar\"\n" + "data:}")).consumeNextWith(stringConsumer("\n\n")).consumeNextWith(stringConsumer("data:")).consumeNextWith(stringConsumer("{\n" + "data: \"foo\" : \"foofoofoo\",\n" + "data: \"bar\" : \"barbarbar\"\n" + "data:}")).consumeNextWith(stringConsumer("\n\n")).expectComplete().verify();
}
use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.
the class CancelWithoutDemandCodecTests method cancelWithSse.
// gh-22107
@Test
public void cancelWithSse() {
ServerSentEvent<?> event = ServerSentEvent.builder().data("bar").id("c42").event("foo").build();
ServerSentEventHttpMessageWriter writer = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder());
CancellingOutputMessage outputMessage = new CancellingOutputMessage(this.bufferFactory);
writer.write(Mono.just(event), ResolvableType.fromClass(ServerSentEvent.class), MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()).block(Duration.ofSeconds(5));
}
use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.
the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.
@Test
// gh-26232
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.fromClass(Mono.class), ResolvableType.fromClass(Map.class), null, request, response, Collections.emptyMap());
StepVerifier.create(result).expectError(AbortedException.class).verify();
bufferFactory.checkForLeaks();
}
use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.
the class ClientCodecConfigurerTests method jackson2CodecCustomization.
@Test
public void jackson2CodecCustomization() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);
List<HttpMessageReader<?>> readers = this.configurer.getReaders();
Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
assertThat(actualDecoder).isSameAs(decoder);
assertThat(findCodec(readers, ServerSentEventHttpMessageReader.class).getDecoder()).isSameAs(decoder);
List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
actualEncoder = findCodec(multipartWriter.getPartWriters(), Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
}
use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.
the class ClientCodecConfigurerTests method cloneShouldNotBeImpactedByChangesToOriginal.
@Test
public void cloneShouldNotBeImpactedByChangesToOriginal() {
ClientCodecConfigurer clone = this.configurer.clone();
this.configurer.registerDefaults(false);
this.configurer.customCodecs().register(new Jackson2JsonEncoder());
List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
assertThat(writers).hasSize(10);
}
Aggregations