Compare commits

..

1 commit

Author SHA1 Message Date
2a05564ec3
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 15:10:32 -08:00
4 changed files with 33 additions and 23 deletions

4
.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="2.1.0" />
<option name="version" value="1.9.10" />
</component>
</project>
</project>

9
.idea/runConfigurations/Run Tests.xml generated Normal file
View file

@ -0,0 +1,9 @@
<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)
}
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()
}

View file

@ -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<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(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<HttpErrorException> {
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()
}
}
}