Only throw an HTTP error if the response body is empty
This commit is contained in:
parent
37517a2845
commit
5ac8f910b5
3 changed files with 21 additions and 31 deletions
9
.idea/runConfigurations/Run Tests.xml
generated
9
.idea/runConfigurations/Run Tests.xml
generated
|
@ -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>
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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<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()
|
||||
@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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue