From 5ac8f910b59247cbba2947a0853c8753db46b778 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 22 Dec 2024 20:33:44 -0800 Subject: [PATCH] Only throw an HTTP error if the response body is empty --- .idea/runConfigurations/Run Tests.xml | 9 ------ .../net/thauvin/erik/jokeapi/JokeUtil.kt | 14 ++++----- .../thauvin/erik/jokeapi/ExceptionsTest.kt | 29 +++++++++---------- 3 files changed, 21 insertions(+), 31 deletions(-) delete mode 100644 .idea/runConfigurations/Run Tests.xml diff --git a/.idea/runConfigurations/Run Tests.xml b/.idea/runConfigurations/Run Tests.xml deleted file mode 100644 index df4d7d6..0000000 --- a/.idea/runConfigurations/Run Tests.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt index 3ef3acc..2d1f6cb 100644 --- a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt +++ b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt @@ -59,15 +59,15 @@ internal fun fetchUrl(url: String, auth: String = ""): String { connection.setRequestProperty("Authentication", auth) } - if (connection.responseCode in 200..399) { - val body = connection.inputStream.bufferedReader().use { it.readText() } - if (JokeApi.logger.isLoggable(Level.FINE)) { - JokeApi.logger.fine(body) - } - return body - } else { + val stream = if (connection.responseCode in 200..399) connection.inputStream else connection.errorStream + val body = stream.bufferedReader().use { it.readText() } + if (body.isBlank()) { throw httpError(connection.responseCode) } + if (JokeApi.logger.isLoggable(Level.FINE)) { + JokeApi.logger.fine(body) + } + return body } finally { connection.disconnect() } diff --git a/src/test/kotlin/net/thauvin/erik/jokeapi/ExceptionsTest.kt b/src/test/kotlin/net/thauvin/erik/jokeapi/ExceptionsTest.kt index 4570a81..d441189 100644 --- a/src/test/kotlin/net/thauvin/erik/jokeapi/ExceptionsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/jokeapi/ExceptionsTest.kt @@ -41,8 +41,6 @@ import net.thauvin.erik.jokeapi.models.Category import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.extension.ExtendWith -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.ValueSource @ExtendWith(BeforeAllTests::class) internal class ExceptionsTest { @@ -63,19 +61,20 @@ internal class ExceptionsTest { } } - @ParameterizedTest - @ValueSource(ints = [400, 404, 403, 413, 414, 429, 500, 523, 666]) - fun `Validate HTTP Exceptions`(code: Int) { - val e = assertThrows { - fetchUrl("https://httpstat.us/$code") - } - assertThat(e, "fetchUrl($code)").all { - prop(HttpErrorException::statusCode).isEqualTo(code) - prop(HttpErrorException::message).isNotNull().isNotEmpty() - if (code < 600) - prop(HttpErrorException::cause).isNotNull().assertThat(Throwable::message).isNotNull() - else - prop(HttpErrorException::cause).isNull() + @Test + fun `Validate HTTP Exceptions`() { + val locs = ArrayList>() + locs.add(Pair("https://apichallenges.herokuapp.com/secret/note", 401)) + locs.add(Pair("https://apichallenges.herokuapp.com/todo", 404)) + + for ((url, code) in locs) { + val e = assertThrows { + fetchUrl(url) + } + assertThat(e, "fetchUrl($code)").all { + prop(HttpErrorException::statusCode).isEqualTo(code) + prop(HttpErrorException::message).isNotNull().isNotEmpty() + } } } }