mirror of
https://github.com/square/okhttp.git
synced 2026-01-24 04:02:07 +03:00
issue #364: spdy should set content-length when it is known.
This commit is contained in:
@@ -37,6 +37,10 @@ public final class SpdyTransport implements Transport {
|
||||
}
|
||||
|
||||
@Override public OutputStream createRequestBody() throws IOException {
|
||||
long fixedContentLength = httpEngine.policy.getFixedContentLength();
|
||||
if (fixedContentLength != -1) {
|
||||
httpEngine.requestHeaders.setContentLength(fixedContentLength);
|
||||
}
|
||||
// TODO: if we aren't streaming up to the server, we should buffer the whole request
|
||||
writeRequestHeaders();
|
||||
return stream.getOutputStream();
|
||||
|
||||
@@ -50,7 +50,9 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
@@ -115,19 +117,57 @@ public final class HttpOverSpdyTest {
|
||||
assertEquals(-1, connection.getInputStream().read());
|
||||
}
|
||||
|
||||
@Test public void post() throws Exception {
|
||||
byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
|
||||
|
||||
/** An output stream can be written to more than once, so we can't guess content length. */
|
||||
@Test public void noDefaultContentLengthOnPost() throws Exception {
|
||||
MockResponse response = new MockResponse().setBody("ABCDE");
|
||||
server.enqueue(response);
|
||||
server.play();
|
||||
|
||||
HttpURLConnection connection = client.open(server.getUrl("/foo"));
|
||||
connection.setDoOutput(true);
|
||||
connection.getOutputStream().write("FGHIJ".getBytes(Util.UTF_8));
|
||||
connection.getOutputStream().write(postBytes);
|
||||
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
|
||||
assertEquals("FGHIJ", request.getUtf8Body());
|
||||
assertArrayEquals(postBytes, request.getBody());
|
||||
assertNull(request.getHeader("Content-Length"));
|
||||
}
|
||||
|
||||
@Test public void userSuppliedContentLengthHeader() throws Exception {
|
||||
MockResponse response = new MockResponse().setBody("ABCDE");
|
||||
server.enqueue(response);
|
||||
server.play();
|
||||
|
||||
HttpURLConnection connection = client.open(server.getUrl("/foo"));
|
||||
connection.setRequestProperty("Content-Length", String.valueOf(postBytes.length));
|
||||
connection.setDoOutput(true);
|
||||
connection.getOutputStream().write(postBytes);
|
||||
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
|
||||
assertArrayEquals(postBytes, request.getBody());
|
||||
assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
|
||||
}
|
||||
|
||||
@Test public void setFixedLengthStreamingModeSetsContentLength() throws Exception {
|
||||
MockResponse response = new MockResponse().setBody("ABCDE");
|
||||
server.enqueue(response);
|
||||
server.play();
|
||||
|
||||
HttpURLConnection connection = client.open(server.getUrl("/foo"));
|
||||
connection.setFixedLengthStreamingMode(postBytes.length);
|
||||
connection.setDoOutput(true);
|
||||
connection.getOutputStream().write(postBytes);
|
||||
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
|
||||
assertArrayEquals(postBytes, request.getBody());
|
||||
assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
|
||||
}
|
||||
|
||||
@Test public void spdyConnectionReuse() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user