Skip to content

Commit

Permalink
final HttpClient tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Nov 7, 2024
1 parent 8e65160 commit dd2dd99
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions client/src/main/kotlin/http/HttpClient.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package http

import ch.qos.logback.classic.Level
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.core.JsonProcessingException
import http.dto.ClientErrorResponse
import java.util.*
import kong.unirest.HttpMethod
Expand Down Expand Up @@ -78,7 +78,7 @@ class HttpClient(private val baseUrl: String) {
try {
val body = parseBody(response, responseType)
SuccessResponse(response.status, body)
} catch (e: JsonMappingException) {
} catch (e: JsonProcessingException) {
logger.error(
"responseParseError",
"Failed to parse response",
Expand Down
25 changes: 23 additions & 2 deletions client/src/test/kotlin/http/HttpClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.kotest.matchers.maps.shouldContainKeys
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldNotBeEmpty
import io.kotest.matchers.types.shouldBeInstanceOf
import java.util.UUID
import kong.unirest.HttpMethod
import kong.unirest.HttpStatus
Expand Down Expand Up @@ -90,10 +91,12 @@ class HttpClientTest : AbstractTest() {
@Test
fun `GET with generic error response`() {
val (client, server) = setUpWebServer()
server.enqueue(MockResponse().setResponseCode(HttpStatus.NOT_FOUND))
server.enqueue(
MockResponse().setResponseCode(HttpStatus.NOT_FOUND).setBody("I looked everywhere")
)

val response = client.doCall<Unit>(HttpMethod.GET, "/test-endpoint")
response shouldBe FailureResponse(HttpStatus.NOT_FOUND, "", null, null)
response shouldBe FailureResponse(HttpStatus.NOT_FOUND, "I looked everywhere", null, null)

val responseLog = verifyLog("http.response", Level.ERROR)
responseLog.message shouldBe "Received 404 for GET /test-endpoint"
Expand Down Expand Up @@ -182,6 +185,24 @@ class HttpClientTest : AbstractTest() {
responseLog.findLogField("responseBody") shouldBe ""
}

@Test
fun `Should handle JSON parse errors`() {
val (client, server) = setUpWebServer()
val responseBody = """{
"fieldOne": "foo",
}"""

server.enqueue(MockResponse().setBody(responseBody))

val response = client.doCall<TestApiResponse>(HttpMethod.GET, "/test-endpoint")
response.shouldBeInstanceOf<FailureResponse<TestApiResponse>>()
response.statusCode shouldBe 200
response.body shouldBe responseBody
response.errorCode shouldBe JSON_PARSE_ERROR

verifyLog("responseParseError", Level.ERROR)
}

@Test
fun `Should handle additional fields`() {
val (client, server) = setUpWebServer()
Expand Down
2 changes: 0 additions & 2 deletions client/src/test/kotlin/http/WebSocketReceiverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class WebSocketReceiverTest : AbstractClientTest() {

val receiver = WebSocketReceiver()
val lobbyMessage = LobbyMessage(emptyList(), listOf(OnlineUser("Alyssa", 5)))
val thing = CoreGlobals.jsonMapper.writeValueAsString(lobbyMessage)
println(thing)
receiver.receiveMessage(CoreGlobals.jsonMapper.writeValueAsString(lobbyMessage))

verify { lobby.syncLobby(lobbyMessage) }
Expand Down

0 comments on commit dd2dd99

Please sign in to comment.