Compare commits

..

2 commits

Author SHA1 Message Date
5ac8f910b5
Only throw an HTTP error if the response body is empty 2024-12-22 20:33:44 -08:00
37517a2845
Updated dependencies
Bumped Kotlin to version 2.1.0
Bumped Kotlin extension to version 1.0.3
Bumped Dokka extension to version 1.0.2
Bumped JUnit to 5.11.4
Bumped UrlEncoder to version 1.6.0
2024-12-22 20:27:53 -08:00
4 changed files with 23 additions and 33 deletions

2
.idea/kotlinc.xml generated
View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="1.9.10" />
<option name="version" value="2.1.0" />
</component>
</project>

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)
}
if (connection.responseCode in 200..399) {
val body = connection.inputStream.bufferedReader().use { it.readText() }
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
} else {
throw httpError(connection.responseCode)
}
} finally {
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.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) {
@Test
fun `Validate HTTP Exceptions`() {
val locs = ArrayList<Pair<String, Int>>()
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<HttpErrorException> {
fetchUrl("https://httpstat.us/$code")
fetchUrl(url)
}
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()
}
}
}
}