content-length support in EncoderHttpMessageWriter

EncoderHttpMessageWriter checks explicitly for Mono publishers and sets
the content length, if it is known for the given data item.

Issue: SPR-16542
This commit is contained in:
Rossen Stoyanchev
2018-02-27 16:53:29 -05:00
parent 7a8e0ff3c3
commit 27815847b1
9 changed files with 106 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,10 +59,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.config.EnableWebFlux;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_XML;
import static java.util.Arrays.*;
import static org.junit.Assert.*;
import static org.springframework.http.MediaType.*;
/**
* {@code @RequestMapping} integration tests focusing on serialization and
@@ -87,7 +86,6 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
return wac;
}
@Test
public void byteBufferResponseBodyWithPublisher() throws Exception {
Person expected = new Person("Robert");
@@ -100,6 +98,14 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
assertEquals(expected, performGet("/raw-response/flux", new HttpHeaders(), String.class).getBody());
}
@Test
public void byteBufferResponseBodyWithMono() throws Exception {
String expected = "Hello!";
ResponseEntity<String> responseEntity = performGet("/raw-response/mono", new HttpHeaders(), String.class);
assertEquals(6, responseEntity.getHeaders().getContentLength());
assertEquals(expected, responseEntity.getBody());
}
@Test
public void byteBufferResponseBodyWithObservable() throws Exception {
String expected = "Hello!";
@@ -422,6 +428,11 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
return Flux.just(ByteBuffer.wrap("Hello!".getBytes()));
}
@GetMapping("/mono")
public Mono<ByteBuffer> getMonoString() {
return Mono.just(ByteBuffer.wrap("Hello!".getBytes()));
}
@GetMapping("/observable")
public Observable<ByteBuffer> getObservable() {
return Observable.just(ByteBuffer.wrap("Hello!".getBytes()));