-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
aa8010f
commit 84e4ab7
Showing
3 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
fuel-jackson/src/main/kotlin/com/github/kittinunf/fuel/jackson/ObjectBody.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.kittinunf.fuel.jackson | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.github.kittinunf.fuel.core.Headers | ||
import com.github.kittinunf.fuel.core.Request | ||
import java.nio.charset.Charset | ||
|
||
/** | ||
* Set the body to an Object to be serialized | ||
*/ | ||
fun Request.objectBody( | ||
bodyObject: Any, | ||
charset: Charset = Charsets.UTF_8, | ||
mapper: ObjectMapper = defaultMapper | ||
): Request { | ||
val bodyString = mapper.writeValueAsString(bodyObject) | ||
this[Headers.CONTENT_TYPE] = "application/json" | ||
return body(bodyString, charset) | ||
} |
67 changes: 67 additions & 0 deletions
67
fuel-jackson/src/test/kotlin/com/github/kittinunf/fuel/ObjectBodyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.kittinunf.fuel | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategy | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import com.github.kittinunf.fuel.core.Headers | ||
import com.github.kittinunf.fuel.core.Method | ||
import com.github.kittinunf.fuel.core.requests.DefaultRequest | ||
import com.github.kittinunf.fuel.jackson.objectBody | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
import java.net.URL | ||
|
||
class ObjectBodyTest { | ||
@Test | ||
fun setsBodyCorrectly() { | ||
val expectedBody = "{\"foo\":42,\"bar\":\"foo bar\",\"fooBar\":\"foo bar\"}" | ||
val bodyObject = FakeObject() | ||
|
||
val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body")) | ||
.objectBody(bodyObject) | ||
|
||
assertThat(expectedBody, equalTo(String(request.body.toByteArray()))) | ||
} | ||
|
||
@Test | ||
fun setsContentTypeCorrectly() { | ||
val bodyObject = listOf( | ||
42, | ||
mapOf("foo" to "bar") | ||
) | ||
|
||
val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body")) | ||
.objectBody(bodyObject) | ||
|
||
assertThat(request[Headers.CONTENT_TYPE].lastOrNull(), equalTo("application/json")) | ||
} | ||
|
||
@Test | ||
fun setsBodyCorrectlyWithCustomMapper() { | ||
val mapper = createCustomMapper() | ||
val expectedBody = "{\"foo\":42,\"bar\":\"foo bar\",\"foo_bar\":\"foo bar\"}" | ||
val bodyObject = FakeObject() | ||
|
||
val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body")) | ||
.objectBody(bodyObject, mapper = mapper) | ||
|
||
assertThat(expectedBody, equalTo(String(request.body.toByteArray()))) | ||
} | ||
|
||
private fun createCustomMapper(): ObjectMapper { | ||
val mapper = ObjectMapper().registerKotlinModule() | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
|
||
mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE | ||
|
||
return mapper | ||
} | ||
} | ||
|
||
data class FakeObject( | ||
val foo: Int = 42, | ||
val bar: String = "foo bar", | ||
val fooBar: String = "foo bar" | ||
) |