Skip to content

Commit

Permalink
More tests on truncated response bodies.
Browse files Browse the repository at this point in the history
Change the exception on a truncated chunked body.
  • Loading branch information
squarejesse committed Jun 21, 2015
1 parent cf4b019 commit b28127c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.squareup.okhttp.TlsVersion;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.RecordingAuthenticator;
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
import com.squareup.okhttp.internal.RecordingOkAuthenticator;
import com.squareup.okhttp.internal.SingleInetAddressNetwork;
import com.squareup.okhttp.internal.SslContextBuilder;
Expand All @@ -43,6 +42,7 @@
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import com.squareup.okhttp.mockwebserver.SocketPolicy;
import com.squareup.okhttp.mockwebserver.rule.MockWebServerRule;
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -710,13 +710,24 @@ private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception {
assertEquals("android.com", request.getHeader("Host"));
}

@Test public void contentDisagreesWithContentLengthHeader() throws IOException {
@Test public void contentDisagreesWithContentLengthHeaderBodyTooLong() throws IOException {
server.enqueue(new MockResponse().setBody("abc\r\nYOU SHOULD NOT SEE THIS")
.clearHeaders()
.addHeader("Content-Length: 3"));
assertContent("abc", client.open(server.getUrl("/")));
}

@Test public void contentDisagreesWithContentLengthHeaderBodyTooShort() throws IOException {
server.enqueue(new MockResponse().setBody("abc")
.setHeader("Content-Length", "5")
.setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
try {
readAscii(client.open(server.getUrl("/")).getInputStream(), 5);
fail();
} catch (ProtocolException expected) {
}
}

public void testConnectViaSocketFactory(boolean useHttps) throws IOException {
SocketFactory uselessSocketFactory = new SocketFactory() {
public Socket createSocket() { throw new IllegalArgumentException("useless"); }
Expand Down Expand Up @@ -758,7 +769,7 @@ public Socket createSocket(String host, int port, InetAddress localHost, int loc
testConnectViaSocketFactory(true);
}

@Test public void contentDisagreesWithChunkedHeader() throws IOException {
@Test public void contentDisagreesWithChunkedHeaderBodyTooLong() throws IOException {
MockResponse mockResponse = new MockResponse();
mockResponse.setChunkedBody("abc", 3);
Buffer buffer = mockResponse.getBody();
Expand All @@ -772,6 +783,28 @@ public Socket createSocket(String host, int port, InetAddress localHost, int loc
assertContent("abc", client.open(server.getUrl("/")));
}

@Test public void contentDisagreesWithChunkedHeaderBodyTooShort() throws IOException {
MockResponse mockResponse = new MockResponse();
mockResponse.setChunkedBody("abcde", 5);

Buffer truncatedBody = new Buffer();
Buffer fullBody = mockResponse.getBody();
truncatedBody.write(fullBody, fullBody.indexOf((byte) 'e'));
mockResponse.setBody(truncatedBody);

mockResponse.clearHeaders();
mockResponse.addHeader("Transfer-encoding: chunked");
mockResponse.setSocketPolicy(SocketPolicy.DISCONNECT_AT_END);

server.enqueue(mockResponse);

try {
readAscii(client.open(server.getUrl("/")).getInputStream(), 5);
fail();
} catch (ProtocolException expected) {
}
}

@Test public void connectViaHttpProxyToHttpsUsingProxyArgWithNoProxy() throws Exception {
testConnectViaDirectProxyToHttps(ProxyConfig.NO_PROXY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ private class ChunkedSource extends AbstractSource {
long read = source.read(sink, Math.min(byteCount, bytesRemainingInChunk));
if (read == -1) {
unexpectedEndOfInput(); // The server didn't supply the promised chunk length.
throw new IOException("unexpected end of stream");
throw new ProtocolException("unexpected end of stream");
}
bytesRemainingInChunk -= read;
return read;
Expand Down

0 comments on commit b28127c

Please sign in to comment.