Renamed JsonResource code to statusCode

This commit is contained in:
Erik C. Thauvin 2024-12-26 11:16:24 -08:00
parent f0ccff6529
commit 97e0479ffb
Signed by: erik
GPG key ID: 776702A6A2DA330E
7 changed files with 38 additions and 27 deletions

View file

@ -154,13 +154,16 @@ A generic `apiCall()` function is available to access other [JokeAPI endpoints](
For example to retrieve the French [language code](https://v2.jokeapi.dev/#langcode-endpoint): For example to retrieve the French [language code](https://v2.jokeapi.dev/#langcode-endpoint):
```kotlin ```kotlin
val lang = JokeApi.apiCall( val response = JokeApi.apiCall(
endPoint = "langcode", endPoint = "langcode",
path = "french", path = "french",
params = mapOf(Parameter.FORMAT to Format.YAML.value) params = mapOf(Parameter.FORMAT to Format.YAML.value)
) )
println(lang.data) if (response.statusCode == 200) {
println(response.data)
}
``` ```
```yaml ```yaml
error: false error: false
code: "fr" code: "fr"

View file

@ -22,6 +22,7 @@
<ID>WildcardImport:GetJokeTest.kt$import assertk.assertions.*</ID> <ID>WildcardImport:GetJokeTest.kt$import assertk.assertions.*</ID>
<ID>WildcardImport:GetJokeTest.kt$import net.thauvin.erik.jokeapi.models.*</ID> <ID>WildcardImport:GetJokeTest.kt$import net.thauvin.erik.jokeapi.models.*</ID>
<ID>WildcardImport:GetJokesTest.kt$import assertk.assertions.*</ID> <ID>WildcardImport:GetJokesTest.kt$import assertk.assertions.*</ID>
<ID>WildcardImport:GetRawJokesTest.kt$import assertk.assertions.*</ID>
<ID>WildcardImport:JokeApi.kt$import net.thauvin.erik.jokeapi.models.*</ID> <ID>WildcardImport:JokeApi.kt$import net.thauvin.erik.jokeapi.models.*</ID>
<ID>WildcardImport:JokeConfig.kt$import net.thauvin.erik.jokeapi.models.*</ID> <ID>WildcardImport:JokeConfig.kt$import net.thauvin.erik.jokeapi.models.*</ID>
<ID>WildcardImport:JokeConfigTest.kt$import assertk.assertions.*</ID> <ID>WildcardImport:JokeConfigTest.kt$import assertk.assertions.*</ID>

View file

@ -33,7 +33,7 @@ package net.thauvin.erik.jokeapi.models
/** /**
* The Joke API response. * The Joke API response.
* *
* @property code The HTTP status code. * @property statusCode The HTTP status code.
* @property data The response text. * @property data The response body text.
*/ */
data class JokeResponse(val code: Int, val data: String) data class JokeResponse(val statusCode: Int, val data: String)

View file

@ -67,7 +67,7 @@ internal class ApiCallTest {
endPoint = "langcode", path = "french", endPoint = "langcode", path = "french",
params = mapOf(Parameter.FORMAT to Format.YAML.value) params = mapOf(Parameter.FORMAT to Format.YAML.value)
) )
assertThat(lang.code).isEqualTo(200) assertThat(lang.statusCode).isEqualTo(200)
assertContains(lang.data, "code: \"fr\"", false, "apiCall(langcode, french, yaml)") assertContains(lang.data, "code: \"fr\"", false, "apiCall(langcode, french, yaml)")
} }
@ -75,7 +75,7 @@ internal class ApiCallTest {
fun `Get Ping Response`() { fun `Get Ping Response`() {
// See https://v2.jokeapi.dev/#ping-endpoint // See https://v2.jokeapi.dev/#ping-endpoint
val ping = apiCall(endPoint = "ping", params = mapOf(Parameter.FORMAT to Format.TXT.value)) val ping = apiCall(endPoint = "ping", params = mapOf(Parameter.FORMAT to Format.TXT.value))
assertThat(ping.code).isEqualTo(200) assertThat(ping.statusCode).isEqualTo(200)
assertThat(ping.data).startsWith("Pong!") assertThat(ping.data).startsWith("Pong!")
} }
@ -86,7 +86,7 @@ internal class ApiCallTest {
endPoint = "languages", endPoint = "languages",
params = mapOf(Parameter.FORMAT to Format.XML.value, Parameter.LANG to Language.FR.value) params = mapOf(Parameter.FORMAT to Format.XML.value, Parameter.LANG to Language.FR.value)
) )
assertThat(lang.code).isEqualTo(200) assertThat(lang.statusCode).isEqualTo(200)
assertThat(lang.data).startsWith("<?xml version='1.0'?>") assertThat(lang.data).startsWith("<?xml version='1.0'?>")
} }
} }

View file

@ -33,53 +33,60 @@ package net.thauvin.erik.jokeapi
import assertk.all import assertk.all
import assertk.assertThat import assertk.assertThat
import assertk.assertions.doesNotContain import assertk.assertions.*
import assertk.assertions.isEqualTo
import assertk.assertions.isNotEmpty
import assertk.assertions.startsWith
import net.thauvin.erik.jokeapi.models.Format import net.thauvin.erik.jokeapi.models.Format
import net.thauvin.erik.jokeapi.models.IdRange import net.thauvin.erik.jokeapi.models.IdRange
import net.thauvin.erik.jokeapi.models.JokeResponse
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import kotlin.test.assertContains
@ExtendWith(BeforeAllTests::class) @ExtendWith(BeforeAllTests::class)
internal class GetRawJokesTest { internal class GetRawJokesTest {
@Test @Test
fun `Get Raw Joke with TXT`() { fun `Get Raw Joke with TXT`() {
val response = rawJokes(format = Format.TXT) val response = rawJokes(format = Format.TXT)
assertThat(response.code).isEqualTo(200) assertThat(response).all {
assertThat(response.data, "rawJoke(data)").all { prop("statusCode", JokeResponse::statusCode).isEqualTo(200)
prop("data", JokeResponse::data).all {
isNotEmpty() isNotEmpty()
doesNotContain("Error") doesNotContain("Error")
} }
} }
}
@Test @Test
fun `Get Raw Joke with XML`() { fun `Get Raw Joke with XML`() {
val response = rawJokes(format = Format.XML) val response = rawJokes(format = Format.XML)
assertThat(response.code).isEqualTo(200) assertThat(response).all {
assertThat(response.data, "rawJoke(xml)").startsWith("<?xml version='1.0'?>\n<data>\n <error>false</error>") prop("statusCode", JokeResponse::statusCode).isEqualTo(200)
prop("data", JokeResponse::data).startsWith("<?xml version='1.0'?>\n<data>\n <error>false</error>")
}
} }
@Test @Test
fun `Get Raw Joke with YAML`() { fun `Get Raw Joke with YAML`() {
val response = rawJokes(format = Format.YAML) val response = rawJokes(format = Format.YAML)
assertThat(response.code).isEqualTo(200) assertThat(response).all {
assertThat(response.data, "rawJoke(yaml)").startsWith("error: false") prop("statusCode", JokeResponse::statusCode).isEqualTo(200)
prop("data", JokeResponse::data).startsWith("error: false")
}
} }
@Test @Test
fun `Get Raw Jokes`() { fun `Get Raw Jokes`() {
val response = rawJokes(amount = 2) val response = rawJokes(amount = 2)
assertThat(response.code).isEqualTo(200) assertThat(response).all {
assertContains(response.data, "\"amount\": 2", false, "rawJoke(2)") prop("statusCode", JokeResponse::statusCode).isEqualTo(200)
prop("data", JokeResponse::data).isNotEmpty()
}
} }
@Test @Test
fun `Get Raw Invalid Jokes`() { fun `Get Raw Invalid Jokes`() {
val response = rawJokes(contains = "foo", safe = true, amount = 2, idRange = IdRange(160, 161)) val response = rawJokes(contains = "foo", safe = true, amount = 2, idRange = IdRange(160, 161))
assertThat(response.code).isEqualTo(400) assertThat(response).all {
assertContains(response.data, "\"error\": true", false, "getRawJokes(foo)") prop("statusCode", JokeResponse::statusCode).isEqualTo(400)
prop("data", JokeResponse::data).contains("\"error\": true")
}
} }
} }

View file

@ -103,7 +103,7 @@ internal class JokeConfigTest {
safe(true) safe(true)
}.build() }.build()
val jokes = getRawJokes(config) val jokes = getRawJokes(config)
assertThat(jokes.code).isEqualTo(200) assertThat(jokes.statusCode).isEqualTo(200)
assertContains(jokes.data, "----------------------------------------------", false, "config.amount(2)") assertContains(jokes.data, "----------------------------------------------", false, "config.amount(2)")
} }

View file

@ -56,7 +56,7 @@ internal class JokeUtilTest {
fun `Validate Authentication Header`() { fun `Validate Authentication Header`() {
val token = "AUTH-TOKEN" val token = "AUTH-TOKEN"
val response = fetchUrl("https://postman-echo.com/get", token) val response = fetchUrl("https://postman-echo.com/get", token)
assertThat(response.code).isEqualTo(200) assertThat(response.statusCode).isEqualTo(200)
assertThat(response.data, "body").contains("\"authentication\": \"$token\"") assertThat(response.data, "body").contains("\"authentication\": \"$token\"")
} }
} }