-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTP cache: do not remove cached entries on transport errors (#6314)
* HTTP cache: do not remove cached entries on transport errors See #6313 * only filter out ApolloNetworkErrors
- Loading branch information
1 parent
9eadd40
commit 9def5f3
Showing
3 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import com.apollographql.apollo.ApolloClient | ||
import com.apollographql.apollo.api.http.HttpRequest | ||
import com.apollographql.apollo.api.http.HttpResponse | ||
import com.apollographql.apollo.cache.http.httpCache | ||
import com.apollographql.apollo.exception.ApolloNetworkException | ||
import com.apollographql.apollo.network.http.HttpEngine | ||
import com.apollographql.apollo.network.http.HttpNetworkTransport | ||
import httpcache.GetRandomQuery | ||
import kotlinx.coroutines.runBlocking | ||
import okio.Buffer | ||
import okio.IOException | ||
import okio.Source | ||
import okio.Timeout | ||
import okio.buffer | ||
import java.io.File | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
|
||
class FaultySource: Source { | ||
|
||
private val data = Buffer().writeUtf8("{ \"data\":") | ||
|
||
override fun close() { | ||
|
||
} | ||
|
||
override fun read(sink: Buffer, byteCount: Long): Long { | ||
val remaining = data.size.coerceAtMost(byteCount) | ||
if (remaining == 0L) { | ||
throw IOException("failed to read") | ||
} | ||
data.read(sink, remaining) | ||
|
||
return remaining | ||
} | ||
|
||
override fun timeout(): Timeout { | ||
return Timeout.NONE | ||
} | ||
} | ||
|
||
class FaultyEngine: HttpEngine { | ||
val requests = mutableListOf<HttpRequest>() | ||
|
||
override suspend fun execute(request: HttpRequest): HttpResponse { | ||
requests.add(request) | ||
|
||
return HttpResponse.Builder(200) | ||
.body(FaultySource().buffer()) | ||
.addHeader("content-length", "30") | ||
.build() | ||
} | ||
} | ||
|
||
class FaultyEngineTest { | ||
|
||
@Test | ||
fun httpErrorsAreNotCached() = runBlocking { | ||
val engine = FaultyEngine() | ||
|
||
val dir = File("build/httpCache") | ||
dir.deleteRecursively() | ||
|
||
val apolloClient = ApolloClient.Builder().apply { | ||
httpCache(dir, Long.MAX_VALUE) | ||
networkTransport( | ||
HttpNetworkTransport.Builder() | ||
.serverUrl("unused") | ||
.interceptors(httpInterceptors) | ||
.httpEngine(engine) | ||
.build() | ||
) | ||
httpInterceptors(emptyList()) | ||
}.build() | ||
|
||
apolloClient.query(GetRandomQuery()).execute().apply { | ||
assertIs<ApolloNetworkException>(exception) | ||
} | ||
apolloClient.query(GetRandomQuery()).execute().apply { | ||
assertIs<ApolloNetworkException>(exception) | ||
} | ||
|
||
assertEquals(2, engine.requests.size) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters