diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index a8d9757..f8467b4 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - - + \ No newline at end of file diff --git a/.idea/runConfigurations/Run Tests.xml b/.idea/runConfigurations/Run Tests.xml new file mode 100644 index 0000000..df4d7d6 --- /dev/null +++ b/.idea/runConfigurations/Run Tests.xml @@ -0,0 +1,9 @@ + + + + \ 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 2d1f6cb..3ef3acc 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) } - val stream = if (connection.responseCode in 200..399) connection.inputStream else connection.errorStream - val body = stream.bufferedReader().use { it.readText() } - if (body.isBlank()) { + 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 { 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 d441189..4570a81 100644 --- a/src/test/kotlin/net/thauvin/erik/jokeapi/ExceptionsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/jokeapi/ExceptionsTest.kt @@ -41,6 +41,8 @@ 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 { @@ -61,20 +63,19 @@ internal class ExceptionsTest { } } - @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() - } + @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() } } }