Only throw an HTTP error if the response body is empty

This commit is contained in:
Erik C. Thauvin 2024-12-22 20:33:44 -08:00
parent 37517a2845
commit 5ac8f910b5
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 21 additions and 31 deletions

View file

@ -1,9 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run Tests" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="net.thauvin.erik.JokeapiTest" />
<module name="app" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View file

@ -59,15 +59,15 @@ internal fun fetchUrl(url: String, auth: String = ""): String {
connection.setRequestProperty("Authentication", auth) connection.setRequestProperty("Authentication", auth)
} }
if (connection.responseCode in 200..399) { val stream = if (connection.responseCode in 200..399) connection.inputStream else connection.errorStream
val body = connection.inputStream.bufferedReader().use { it.readText() } val body = stream.bufferedReader().use { it.readText() }
if (JokeApi.logger.isLoggable(Level.FINE)) { if (body.isBlank()) {
JokeApi.logger.fine(body)
}
return body
} else {
throw httpError(connection.responseCode) throw httpError(connection.responseCode)
} }
if (JokeApi.logger.isLoggable(Level.FINE)) {
JokeApi.logger.fine(body)
}
return body
} finally { } finally {
connection.disconnect() connection.disconnect()
} }

View file

@ -41,8 +41,6 @@ import net.thauvin.erik.jokeapi.models.Category
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
@ExtendWith(BeforeAllTests::class) @ExtendWith(BeforeAllTests::class)
internal class ExceptionsTest { internal class ExceptionsTest {
@ -63,19 +61,20 @@ internal class ExceptionsTest {
} }
} }
@ParameterizedTest @Test
@ValueSource(ints = [400, 404, 403, 413, 414, 429, 500, 523, 666]) fun `Validate HTTP Exceptions`() {
fun `Validate HTTP Exceptions`(code: Int) { val locs = ArrayList<Pair<String, Int>>()
val e = assertThrows<HttpErrorException> { locs.add(Pair("https://apichallenges.herokuapp.com/secret/note", 401))
fetchUrl("https://httpstat.us/$code") locs.add(Pair("https://apichallenges.herokuapp.com/todo", 404))
}
assertThat(e, "fetchUrl($code)").all { for ((url, code) in locs) {
prop(HttpErrorException::statusCode).isEqualTo(code) val e = assertThrows<HttpErrorException> {
prop(HttpErrorException::message).isNotNull().isNotEmpty() fetchUrl(url)
if (code < 600) }
prop(HttpErrorException::cause).isNotNull().assertThat(Throwable::message).isNotNull() assertThat(e, "fetchUrl($code)").all {
else prop(HttpErrorException::statusCode).isEqualTo(code)
prop(HttpErrorException::cause).isNull() prop(HttpErrorException::message).isNotNull().isNotEmpty()
}
} }
} }
} }