Skip to content

Commit

Permalink
merge latest changes from main
Browse files Browse the repository at this point in the history
  • Loading branch information
iNoles committed May 22, 2024
1 parent feaa51e commit 3d65b38
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 54 deletions.
11 changes: 0 additions & 11 deletions fuel/src/jsMain/kotlin/fuel/HttpResponse.kt

This file was deleted.

35 changes: 0 additions & 35 deletions fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt

This file was deleted.

3 changes: 2 additions & 1 deletion fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package fuel

import kotlinx.coroutines.ExperimentalCoroutinesApi
import okhttp3.Call
import okhttp3.Request.Builder
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.executeAsync
import okhttp3.coroutines.executeAsync
import okhttp3.internal.http.HttpMethod

public class JVMHttpLoader(callFactoryLazy: Lazy<Call.Factory>) : HttpLoader {
Expand Down
14 changes: 8 additions & 6 deletions fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.runBlocking
import mockwebserver3.MockResponse
import mockwebserver3.MockWebServer
import mockwebserver3.SocketPolicy
import okhttp3.ExperimentalOkHttpApi
import okhttp3.OkHttpClient
import org.junit.After
import org.junit.Assert.assertEquals
Expand All @@ -13,6 +14,7 @@ import java.net.SocketTimeoutException
import java.util.concurrent.TimeUnit
import kotlin.test.assertNotNull

@OptIn(ExperimentalOkHttpApi::class)
class HttpLoaderBuilderTest {

private lateinit var mockWebServer: MockWebServer
Expand All @@ -29,7 +31,7 @@ class HttpLoaderBuilderTest {

@Test
fun `default okhttp settings`() = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("Hello World"))
mockWebServer.enqueue(MockResponse(body = "Hello World"))
val response = JVMHttpLoader().get {
url = mockWebServer.url("hello").toString()
}.body.string()
Expand All @@ -40,7 +42,7 @@ class HttpLoaderBuilderTest {

@Test
fun `default okhttp settings with parameter`() = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("Hello World 3"))
mockWebServer.enqueue(MockResponse(body = "Hello World 3"))
val response = JVMHttpLoader().get {
url = mockWebServer.url("hello").toString()
parameters = listOf("foo" to "bar")
Expand All @@ -52,7 +54,7 @@ class HttpLoaderBuilderTest {

@Test
fun `default okhttp settings with headers`() = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("Hello World"))
mockWebServer.enqueue(MockResponse(body = "Hello World"))
val response = JVMHttpLoader().get {
url = mockWebServer.url("hello").toString()
}.headers
Expand All @@ -63,7 +65,7 @@ class HttpLoaderBuilderTest {

@Test
fun `setting connect timeouts`() = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("Hello World 4"))
mockWebServer.enqueue(MockResponse(body = "Hello World 4"))

val httpLoader = FuelBuilder()
.config {
Expand All @@ -78,7 +80,7 @@ class HttpLoaderBuilderTest {

@Test
fun `setting call timeouts`() = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("Hello World 5"))
mockWebServer.enqueue(MockResponse(body = "Hello World 5"))

val okhttp = OkHttpClient.Builder().callTimeout(30L, TimeUnit.MILLISECONDS).build()
val httpLoader = FuelBuilder().config(okhttp).build()
Expand All @@ -90,7 +92,7 @@ class HttpLoaderBuilderTest {

@Test
fun `no socket response`(): Unit = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("{}").setSocketPolicy(SocketPolicy.NO_RESPONSE))
mockWebServer.enqueue(MockResponse(body = "{}", socketPolicy = SocketPolicy.NoResponse))
try {
JVMHttpLoader().get {
url = mockWebServer.url("socket").toString()
Expand Down
1 change: 1 addition & 0 deletions fuel/src/jvmTest/kotlin/fuel/RoutingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test

@OptIn(ExperimentalOkHttpApi::class)
class RoutingTest {

sealed class TestApi(private val host: String) : FuelRouting {
Expand Down
1 change: 1 addition & 0 deletions fuel/src/wasmJsMain/kotlin/fuel/HttpResponse.wasmJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import org.w3c.fetch.Response
public actual class HttpResponse {
public var statusCode: Short = -1
public var response: Response? = null
public var headers: Map<String, String> = emptyMap()
}
15 changes: 14 additions & 1 deletion fuel/src/wasmJsMain/kotlin/fuel/HttpUrlFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fuel

import kotlinx.browser.window
import kotlinx.coroutines.suspendCancellableCoroutine
import org.w3c.fetch.Headers
import org.w3c.fetch.RequestInit
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
Expand All @@ -14,7 +15,7 @@ internal class HttpUrlFetcher {

val requestInit = obj<RequestInit> {}
requestInit.method = method
requestInit.headers = request.headers?.toJsReference()
requestInit.headers = request.headers.toJsReference()
requestInit.body = body?.toJsString()
return suspendCancellableCoroutine { continuation ->
window.fetch(urlString, requestInit)
Expand All @@ -23,6 +24,7 @@ internal class HttpUrlFetcher {
continuation.resume(HttpResponse().apply {
statusCode = it.status
response = it
headers = it.headers.mapToFuel()
})
null
} else {
Expand All @@ -36,4 +38,15 @@ internal class HttpUrlFetcher {
}
}
}

private fun Headers.mapToFuel(): Map<String, String> {
val headers = mutableMapOf<String, String>()
val keys = getKeys(this@mapToFuel)
for (i in 0 until keys.length) {
val key = keys[i].toString()
val value = this@mapToFuel.get(key)!!
headers[key] = value
}
return headers
}
}
3 changes: 3 additions & 0 deletions fuel/src/wasmJsMain/kotlin/fuel/Object.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ public inline fun <T : JsAny> obj(init: T.() -> Unit): T {
public operator fun JsAny.set(key: String, value: JsAny) {
this[key] = value
}

public fun getKeys(headers: org.w3c.fetch.Headers): JsArray<JsString> =
js("Array.from(headers.keys())")

0 comments on commit 3d65b38

Please sign in to comment.