diff --git a/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt b/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt index 032263ae..90bcc2e2 100644 --- a/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt +++ b/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt @@ -10,5 +10,5 @@ public fun HttpResponse.toJson( json: Json = Json { allowStructuredMapKeys = true }, deserializationStrategy: DeserializationStrategy ): Result = runCatching { - json.decodeFromString(deserializationStrategy, body) + json.decodeFromString(deserializationStrategy, response?.json().toString()) } diff --git a/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt b/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt index bc742add..594c657c 100644 --- a/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt +++ b/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt @@ -6,4 +6,5 @@ public actual class HttpResponse { public var statusCode: Int = -1 public var nsData: NSData? = null public var body: String? = null + public var headers: Map = emptyMap() } diff --git a/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt b/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt index 6d812e0d..d7c13c10 100644 --- a/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt +++ b/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt @@ -85,9 +85,18 @@ internal class HttpUrlFetcher(private val sessionConfiguration: NSURLSessionConf statusCode = httpResponse.statusCode.toInt() nsData = data body = bodyString + headers = httpResponse.readHeaders() } } + private fun NSHTTPURLResponse.readHeaders(): Map { + val map = mutableMapOf() + allHeaderFields.forEach { + map[it.key as String] = it.value as String + } + return map + } + @BetaInteropApi private fun String.encode(): NSData = NSString.create(string = this).dataUsingEncoding(NSWindowsCP1251StringEncoding)!! } diff --git a/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt b/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt index 755cdbf2..df8414a1 100644 --- a/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt +++ b/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt @@ -1,11 +1,9 @@ package fuel -import org.khronos.webgl.ArrayBuffer -import org.w3c.files.Blob +import org.w3c.fetch.Response public actual class HttpResponse { public var statusCode: Int = -1 - public lateinit var array: ArrayBuffer - public var body: String = "" - public lateinit var blob: Blob + public var response: Response? = null + public var headers: Map = emptyMap() } diff --git a/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt b/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt index 4485366f..f34fd9ac 100644 --- a/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt +++ b/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt @@ -2,7 +2,9 @@ package fuel import kotlinx.browser.window import kotlinx.coroutines.await +import org.w3c.fetch.Headers import org.w3c.fetch.RequestInit + internal class HttpUrlFetcher { suspend fun fetch(request: Request, method: String?, body: String? = null): HttpResponse { val urlString = request.parameters?.let { @@ -18,9 +20,16 @@ internal class HttpUrlFetcher { ).await() return HttpResponse().apply { this.statusCode = res.status.toInt() - this.array = res.arrayBuffer().await() - this.body = res.text().await() - this.blob = res.blob().await() + this.response = res + this.headers = res.headers.mapToFuel() + } + } + + private fun Headers.mapToFuel(): Map { + val headers = mutableMapOf() + this@mapToFuel.asDynamic().forEach { value: String, key: String -> + headers.put(key, value) } + return headers } } diff --git a/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt b/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt index 2f55257e..3a0e4ba7 100644 --- a/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt +++ b/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt @@ -5,4 +5,5 @@ import okhttp3.ResponseBody public actual class HttpResponse { public var statusCode: Int = -1 public lateinit var body: ResponseBody + public var headers: Map = emptyMap() } diff --git a/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt b/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt index b80ac6bd..0bf7f48a 100644 --- a/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt +++ b/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt @@ -3,6 +3,7 @@ package fuel import okhttp3.Call import okhttp3.Request.Builder import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response import okhttp3.executeAsync import okhttp3.internal.http.HttpMethod @@ -67,9 +68,20 @@ public class JVMHttpLoader(callFactoryLazy: Lazy) : HttpLoader { return HttpResponse().apply { statusCode = response.code body = response.body + headers = response.toHeaders() } } + private fun Response.toHeaders(): Map { + val header = mutableMapOf() + for ((key, values) in headers) { + for (value in values) { + header[key] = value.toString() + } + } + return header + } + private fun createRequestBuilder(request: Request, method: String): Builder { val builder = Builder() with(builder) { diff --git a/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt b/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt index d1677e1a..15a88624 100644 --- a/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt +++ b/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt @@ -38,6 +38,17 @@ internal class HttpLoaderBuilderTest { mockWebServer.shutdown() } + @Test + fun `default okhttp settings with headers`() = runBlocking { + mockWebServer.enqueue(MockResponse().setBody("Hello World")) + val response = JVMHttpLoader().get { + url = mockWebServer.url("hello").toString() + }.headers + assertEquals("1", response["Content-Length"]) + + mockWebServer.shutdown() + } + @Test fun `setting connect timeouts`() = runBlocking { mockWebServer.enqueue(MockResponse().setBody("Hello World"))