commonMain.com.apollographql.apollo3.testing.TestHttpEngine.kt Maven / Gradle / Ivy
package com.apollographql.apollo3.testing
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.ApolloResponse
import com.apollographql.apollo3.api.ResponseAdapterCache
import com.apollographql.apollo3.api.toResponse
import com.apollographql.apollo3.exception.ApolloNetworkException
import com.apollographql.apollo3.network.http.HttpEngine
import com.apollographql.apollo3.network.http.HttpRequest
import com.apollographql.apollo3.network.http.HttpResponse
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import okio.Buffer
class TestHttpEngine : HttpEngine {
fun interface ResponseProvider {
fun response(request: HttpRequest): HttpResponse
}
private val channel = Channel(capacity = Channel.UNLIMITED)
fun enqueue(responseProvider: (HttpRequest) -> HttpResponse) {
channel.offer(responseProvider)
}
fun enqueue(httpResponse: HttpResponse) {
enqueue {
httpResponse
}
}
fun enqueue(
operation: Operation,
data: D,
responseAdapterCache: ResponseAdapterCache = ResponseAdapterCache.DEFAULT
) {
val json = operation.toResponse(data, responseAdapterCache = responseAdapterCache)
enqueue(json)
}
fun enqueue(string: String) {
enqueue(HttpResponse(
statusCode = 200,
headers = emptyMap(),
body = Buffer().writeUtf8(string)
))
}
@OptIn(ExperimentalCoroutinesApi::class)
override suspend fun execute(request: HttpRequest, block: (HttpResponse) -> R): R {
if (channel.isEmpty) {
throw ApolloNetworkException(message = "No Response")
}
return block(channel.receive().response(request))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy