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):
```kotlin
val lang = JokeApi.apiCall(
val response = JokeApi.apiCall(
endPoint = "langcode",
path = "french",
params = mapOf(Parameter.FORMAT to Format.YAML.value)
)
println(lang.data)
if (response.statusCode == 200) {
println(response.data)
}
```
```yaml
error: false
code: "fr"

View file

@ -22,6 +22,7 @@
<ID>WildcardImport:GetJokeTest.kt$import assertk.assertions.*</ID>
<ID>WildcardImport:GetJokeTest.kt$import net.thauvin.erik.jokeapi.models.*</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:JokeConfig.kt$import net.thauvin.erik.jokeapi.models.*</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.
*
* @property code The HTTP status code.
* @property data The response text.
* @property statusCode The HTTP status code.
* @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",
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)")
}
@ -75,7 +75,7 @@ internal class ApiCallTest {
fun `Get Ping Response`() {
// See https://v2.jokeapi.dev/#ping-endpoint
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!")
}
@ -86,7 +86,7 @@ internal class ApiCallTest {
endPoint = "languages",
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'?>")
}
}

View file

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

View file

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

View file

@ -56,7 +56,7 @@ internal class JokeUtilTest {
fun `Validate Authentication Header`() {
val token = "AUTH-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\"")
}
}