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

Add test to verify etag is updated correctly. #2676

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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 @@ -25,10 +25,12 @@ import kotlin.test.assertNotNull
import kotlin.time.Duration.Companion.seconds
import kotlinx.datetime.Instant
import okio.Buffer
import okio.BufferedSource
import okio.ByteString.Companion.encodeUtf8
import okio.blackholeSink
import okio.buffer
import okio.fakefilesystem.FakeFileSystem
import okio.use

class CacheControlCacheStrategyTest : RobolectricTest() {
// Friday, November 8, 2024 8:00:00 AM UTC
Expand Down Expand Up @@ -85,7 +87,7 @@ class CacheControlCacheStrategyTest : RobolectricTest() {
}

@Test
fun cachedResponseIsVerifiedAndReturnedFromTheCache() = runTestAsync {
fun etagIsVerified_304() = runTestAsync {
val url = FAKE_URL
val expectedSize = FAKE_DATA.size.toLong()
val etag = "fake_etag"
Expand All @@ -100,8 +102,14 @@ class CacheControlCacheStrategyTest : RobolectricTest() {
assertEquals(1, networkClient.requests.size)
assertIs<SourceFetchResult>(result)
assertEquals(DataSource.NETWORK, result.dataSource)
assertEquals(expectedSize, result.source.use { it.source().readAll(blackholeSink()) })
diskCache.openSnapshot(url).use(::assertNotNull)
assertEquals(FAKE_DATA, result.source.use { it.source().readByteString() })

// Assert is was persisted to the disk cache with the correct etag and data.
diskCache.openSnapshot(url)!!.use {
val cachedResponse = fileSystem.source(it.metadata).buffer().use(CacheNetworkResponse::readFrom)
assertEquals(etag, cachedResponse.headers["ETag"])
assertEquals(FAKE_DATA, fileSystem.source(it.data).buffer().use(BufferedSource::readByteString))
}

// Don't set a response body as it should be read from the cache.
response = FakeNetworkResponse(
Expand All @@ -121,6 +129,61 @@ class CacheControlCacheStrategyTest : RobolectricTest() {
assertEquals(etag, networkClient.requests[1].headers["If-None-Match"])
}

@Test
fun etagIsVerified_different() = runTestAsync {
val url = FAKE_URL
val newHeaders = { etag: String ->
NetworkHeaders.Builder()
.set("Cache-Control", "no-cache")
.set("ETag", etag)
.build()
}
var data = "abcdefg".encodeUtf8()
var response = FakeNetworkResponse(
headers = newHeaders("first_etag"),
body = NetworkResponseBody(Buffer().apply { write(data) }),
)
networkClient.enqueue(url, response)
var result = newFetcher(url).fetch()

assertEquals(1, networkClient.requests.size)
assertIs<SourceFetchResult>(result)
assertEquals(DataSource.NETWORK, result.dataSource)
assertEquals(data, result.source.use { it.source().readByteString() })

// Assert is was persisted to the disk cache with the correct etag and data.
diskCache.openSnapshot(url)!!.use {
val cachedResponse = fileSystem.source(it.metadata).buffer().use(CacheNetworkResponse::readFrom)
assertEquals("first_etag", cachedResponse.headers["ETag"])
assertEquals(data, fileSystem.source(it.data).buffer().use(BufferedSource::readByteString))
}

// Don't set a response body as it should be read from the cache.
data = "1234567".encodeUtf8()
response = FakeNetworkResponse(
code = 200,
headers = newHeaders("second_etag"),
body = NetworkResponseBody(Buffer().apply { write(data) }),
)
networkClient.enqueue(url, response)
result = newFetcher(url).fetch()

assertIs<SourceFetchResult>(result)
assertEquals(DataSource.NETWORK, result.dataSource)
assertEquals(data, result.source.use { it.source().readByteString() })

// Ensure we passed the correct etag.
assertEquals(2, networkClient.requests.size)
assertEquals("first_etag", networkClient.requests[1].headers["If-None-Match"])

// Assert is was persisted to the disk cache with the correct etag and data.
diskCache.openSnapshot(url)!!.use {
val cachedResponse = fileSystem.source(it.metadata).buffer().use(CacheNetworkResponse::readFrom)
assertEquals("second_etag", cachedResponse.headers["ETag"])
assertEquals(data, fileSystem.source(it.data).buffer().use(BufferedSource::readByteString))
}
}

/** Regression test: https://github.com/coil-kt/coil/issues/1256 */
@Test
fun HTTP_NOT_MODIFIED_ResponseCombinesHeadersWithCachedResponse() = runTestAsync {
Expand Down