diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml
index f8467b4..a8d9757 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
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()
+ }
}
}
}