Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate network call for chunked responses #2363

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion coil-base/src/main/java/coil/fetch/HttpUriFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal class HttpUriFetcher(
}

// If we failed to read a new snapshot then read the response body if it's not empty.
if (responseBody.contentLength() > 0) {
if (response.peekBody(1).bytes().isNotEmpty()) {
ferinagy marked this conversation as resolved.
Show resolved Hide resolved
return SourceResult(
source = responseBody.toImageSource(),
mimeType = getMimeType(url, responseBody.contentType()),
Expand Down
21 changes: 21 additions & 0 deletions coil-base/src/test/java/coil/fetch/HttpUriFetcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okio.Buffer
import okio.FileSystem
import okio.blackholeSink
import okio.buffer
Expand Down Expand Up @@ -147,6 +148,18 @@ class HttpUriFetcherTest {
assertEquals(expectedSize, result.source.use { it.source().readAll(blackholeSink()) })
}

@Test
fun `no disk cache and chunked response - fetcher calls network just once`() = runTestAsync {
val url = server.url(IMAGE).toString()

server.enqueueChunkedImage(IMAGE)
server.enqueueChunkedImage(IMAGE)

newFetcher(url, diskCache = null).fetch()

assertEquals(1, server.requestCount)
}

@Test
fun `request on main thread with network cache policy disabled executes without throwing`() = runTestAsync {
val expectedSize = server.enqueueImage(IMAGE)
Expand Down Expand Up @@ -458,6 +471,14 @@ class HttpUriFetcherTest {
assertEquals(expectedSize, result.source.use { it.source().readAll(blackholeSink()) })
}

fun MockWebServer.enqueueChunkedImage(image: String) {
val buffer = Buffer()
val context = ApplicationProvider.getApplicationContext<Context>()
context.assets.open(image).source().buffer().readAll(buffer)
val response = MockResponse().setChunkedBody(buffer, 100)
enqueue(response)
}

private fun newFetcher(
url: String,
options: Options = Options(context),
Expand Down
Loading