Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ private static long bufferSizeHeuristic(UrlResponseInfo info) {
final Map<String, List<String>> headers = info.getAllHeaders();
if (headers.containsKey(CONTENT_LENGTH)) {
long contentLength = Long.parseLong(headers.get(CONTENT_LENGTH).get(0));
boolean isCompressed =
boolean isIdentity =
!headers.containsKey(CONTENT_ENCODING)
|| (headers.get(CONTENT_ENCODING).size() == 1
&& "identity".equals(headers.get(CONTENT_ENCODING).get(0)));
if (isCompressed) {
// We have to guess at the uncompressed size. In the future, consider guessing a
// compression ratio based on the content-type and content-encoding. For now,
// assume 2.
return 2 * contentLength;
} else {
if (isIdentity) {
// In this case, we know exactly how many bytes we're going to get, so we can
// size our buffer perfectly. However, we still have to call read() for the last time,
// even when we know there shouldn't be any more bytes coming. To avoid allocating another
// buffer for that case, add one more byte than we really need.
return contentLength + 1;
} else {
// We have to guess at the uncompressed size. In the future, consider guessing a
// compression ratio based on the content-type and content-encoding. For now,
// assume 2.
return 2 * contentLength + 1;
}
} else {
// No content-length. This means we're either being sent a chunked response, or the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public ByteBuffer answer(InvocationOnMock invocation) throws Throwable {
anyString(), any(UrlRequest.Callback.class), any(Executor.class)))
.thenReturn(mockUrlRequestBuilder);
when(mockUrlRequestBuilder.build()).thenReturn(request);
org.mockito.Mockito.doAnswer(
new org.mockito.stubbing.Answer<Void>() {
@Override
public Void answer(org.mockito.invocation.InvocationOnMock invocation)
throws Throwable {
ByteBuffer buffer = invocation.getArgument(0);
if (!buffer.hasRemaining()) {
throw new IllegalArgumentException("ByteBuffer is already full.");
}
return null;
}
})
.when(request)
.read(any(ByteBuffer.class));

glideUrl = new GlideUrl("http://www.google.com");

Expand Down Expand Up @@ -204,7 +218,9 @@ public List<Map.Entry<String, String>> getAllHeadersAsList() {
public Map<String, List<String>> getAllHeaders() {
ImmutableMap.Builder<String, List<String>> builder = ImmutableMap.builder();
for (Map.Entry<String, String> entry : getAllHeadersAsList()) {
builder.put(entry.getKey(), ImmutableList.copyOf(entry.getValue().split(",")));
builder.put(
entry.getKey().toLowerCase(java.util.Locale.US),
ImmutableList.copyOf(entry.getValue().split(",")));
}
return builder.build();
}
Expand Down Expand Up @@ -377,6 +393,20 @@ public void testRequestComplete_with200NotCancelledMatchingLength_callsCallbackW
.isEqualTo(data);
}

@Test
public void testRequestComplete_with200NotCancelledZeroLength_callsCallbackWithEmptyData()
throws Exception {
ByteBuffer expected = ByteBuffer.allocateDirect(0);
ArgumentCaptor<ByteBuffer> captor = ArgumentCaptor.forClass(ByteBuffer.class);

fetcher.loadData(Priority.LOW, callback);
succeed(getInfo(0, 200), urlRequestListenerCaptor.getValue(), expected.duplicate());

verify(callback, timeout(1000)).onDataReady(captor.capture());
ByteBuffer received = captor.getValue();
assertThat(received.remaining()).isEqualTo(0);
}

private static Map<String, String> anyHeaders() {
return anyMap();
}
Expand Down
Loading