Skip to content

Commit

Permalink
Merge branch 'main' into improve_test
Browse files Browse the repository at this point in the history
  • Loading branch information
iNoles authored May 21, 2024
2 parents 4363e4d + 1a716f5 commit bfff85e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public fun <T : Any> HttpResponse.toJson(
json: Json = Json { allowStructuredMapKeys = true },
deserializationStrategy: DeserializationStrategy<T>
): Result<T?, Throwable> = runCatching {
json.decodeFromString(deserializationStrategy, body)
json.decodeFromString(deserializationStrategy, response?.json().toString())
}
1 change: 1 addition & 0 deletions fuel/src/appleMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> = emptyMap()
}
9 changes: 9 additions & 0 deletions fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
val map = mutableMapOf<String, String>()
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)!!
}
8 changes: 3 additions & 5 deletions fuel/src/jsMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
@@ -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<String, String> = emptyMap()
}
15 changes: 12 additions & 3 deletions fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String, String> {
val headers = mutableMapOf<String, String>()
this@mapToFuel.asDynamic().forEach { value: String, key: String ->
headers.put(key, value)
}
return headers
}
}
1 change: 1 addition & 0 deletions fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> = emptyMap()
}
12 changes: 12 additions & 0 deletions fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -67,9 +68,20 @@ public class JVMHttpLoader(callFactoryLazy: Lazy<Call.Factory>) : HttpLoader {
return HttpResponse().apply {
statusCode = response.code
body = response.body
headers = response.toHeaders()
}
}

private fun Response.toHeaders(): Map<String, String> {
val header = mutableMapOf<String, String>()
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) {
Expand Down
11 changes: 11 additions & 0 deletions fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ class HttpLoaderBuilderTest {
parameters = listOf("foo" to "bar")
}.body.string()
assertEquals("Hello World 3", response)

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()
}
Expand Down

0 comments on commit bfff85e

Please sign in to comment.