Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increasing Test Coverages #866

Merged
merged 2 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions fuel-jackson-jvm/src/main/kotlin/fuel/jackson/ResponseExtension.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package fuel.jackson

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.doTry
import com.github.kittinunf.result.runCatching
import fuel.HttpResponse

public val defaultMapper: ObjectMapper = ObjectMapper().registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

public inline fun <reified T : Any> HttpResponse.toJackson(mapper: ObjectMapper = defaultMapper): Result<T, Throwable> =
doTry(work = {
Result.success(mapper.readValue(body.string()))
}, errorHandler = {
Result.failure(it)
})
public inline fun <reified T : Any> HttpResponse.toJackson(
mapper: ObjectMapper = jacksonObjectMapper()
): Result<T?, Throwable> =
runCatching {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool 👍

mapper.readValue(body.string())
}
19 changes: 10 additions & 9 deletions fuel-jackson-jvm/src/test/kotlin/fuel/jackson/FuelJacksonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import org.junit.Assert.fail
import org.junit.Test

class FuelJacksonTest {

private val createCustomMapper: ObjectMapper = ObjectMapper().registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).apply {
propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE
}

data class HttpBinUserAgentModel(
val userAgent: String = "",
val http_status: String = ""
Expand All @@ -29,7 +35,7 @@ class FuelJacksonTest {
val response = Fuel.get(mockWebServer.url("user-agent").toString())
val jackson = response.toJackson<HttpBinUserAgentModel>()
jackson.fold({
assertEquals("Fuel", it.userAgent)
assertEquals("Fuel", it?.userAgent)
}, {
fail(it.localizedMessage)
})
Expand All @@ -45,19 +51,14 @@ class FuelJacksonTest {
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val jackson = response.toJackson<HttpBinUserAgentModel>(createCustomMapper())
val jackson = response.toJackson<HttpBinUserAgentModel>(createCustomMapper)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] Just wondering whether we could provide a nice DSL for client to configure the mapper?

fun toJackson<T>(configure: ObjectMapper.() -> Unit) {
  val mapper = ObjectMapper()
  mapper.apply(configure)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could have to write a PR about it.

jackson.fold({
assertEquals("", it.userAgent)
assertEquals("OK", it.http_status)
assertEquals("", it?.userAgent)
assertEquals("OK", it?.http_status)
}, {
fail(it.localizedMessage)
})

mockWebServer.shutdown()
}

private fun createCustomMapper(): ObjectMapper = ObjectMapper().registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).apply {
propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ class FuelKotlinxSerializationTest {

mockWebServer.shutdown()
}

@Test
fun testSerializableResponseWithDefaultJson() = runBlocking {
val mockWebServer = MockWebServer().apply {
enqueue(MockResponse().setBody("{\"userAgent\": \"Fuel2\"}"))
start()
}

val response = Fuel.get(mockWebServer.url("user-agent").toString())
val json = response.toJson(deserializationStrategy = HttpBinUserAgentModel.serializer())
json.fold({
assertEquals("Fuel2", it?.userAgent)
}, {
fail(it.message)
})

mockWebServer.shutdown()
}
}
17 changes: 8 additions & 9 deletions fuel-moshi-jvm/src/main/kotlin/fuel/moshi/ResponseExtension.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package fuel.moshi

import com.github.kittinunf.result.Result
import com.github.kittinunf.result.runCatching
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import fuel.HttpResponse
import java.io.IOException
import java.lang.reflect.Type

public val defaultMoshi: Moshi.Builder = Moshi.Builder()

public inline fun <reified T : Any> HttpResponse.toMoshi(): Result<T?, IOException> = toMoshi(T::class.java)
public inline fun <reified T : Any> HttpResponse.toMoshi(): Result<T?, Throwable> = toMoshi(T::class.java)

public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, IOException> =
public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, Throwable> =
toMoshi(defaultMoshi.build().adapter(clazz))

public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, IOException> =
public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, Throwable> =
toMoshi(defaultMoshi.build().adapter(type))

public fun <T : Any> HttpResponse.toMoshi(jsonAdapter: JsonAdapter<T>): Result<T?, IOException> = try {
Result.success(jsonAdapter.fromJson(body.source()))
} catch (ioe: IOException) {
Result.failure(ioe)
}
public fun <T : Any> HttpResponse.toMoshi(jsonAdapter: JsonAdapter<T>): Result<T?, Throwable> =
runCatching {
jsonAdapter.fromJson(body.source())
}
34 changes: 34 additions & 0 deletions fuel/src/jvmTest/kotlin/fuel/HttpLoaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,40 @@ internal class HttpLoaderTest {
assertEquals(string, "Hello World")
}

@Test
fun `get test data with parameters`() = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("Hello There"))

val request = Request.Builder()
.url(mockWebServer.url("get").toString())
.parameters(listOf("foo" to "bar"))
.build()

val string = httpLoader.get(request).body.string()

val request1 = mockWebServer.takeRequest()

assertEquals("GET", request1.method)
assertEquals(string, "Hello There")
}

@Test
fun `get test data with headers`() = runBlocking {
mockWebServer.enqueue(MockResponse().setBody("Greeting Everyone"))

val request = Request.Builder()
.url(mockWebServer.url("get").toString())
.headers(mapOf("Foo" to "bar"))
.build()

val string = httpLoader.get(request).body.string()

val request1 = mockWebServer.takeRequest()

assertEquals("GET", request1.method)
assertEquals(string, "Greeting Everyone")
}

@Test
fun `post test data`() = runBlocking {
mockWebServer.enqueue(MockResponse())
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
coroutines = "1.7.0"
jackson = "2.13.4"
jackson = "2.15.0"
junit = "4.13.2"
moshi = "1.14.0"
okhttp = "5.0.0-alpha.11"
Expand Down