From dd2dd997c5893fe12832841a900c23109afbab99 Mon Sep 17 00:00:00 2001 From: Alyssa Date: Thu, 7 Nov 2024 19:13:01 +0000 Subject: [PATCH] final HttpClient tweaks --- client/src/main/kotlin/http/HttpClient.kt | 4 +-- client/src/test/kotlin/http/HttpClientTest.kt | 25 +++++++++++++++++-- .../test/kotlin/http/WebSocketReceiverTest.kt | 2 -- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/client/src/main/kotlin/http/HttpClient.kt b/client/src/main/kotlin/http/HttpClient.kt index 987d5d9..f6fed5b 100644 --- a/client/src/main/kotlin/http/HttpClient.kt +++ b/client/src/main/kotlin/http/HttpClient.kt @@ -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 @@ -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", diff --git a/client/src/test/kotlin/http/HttpClientTest.kt b/client/src/test/kotlin/http/HttpClientTest.kt index 360bb5a..4adbad9 100644 --- a/client/src/test/kotlin/http/HttpClientTest.kt +++ b/client/src/test/kotlin/http/HttpClientTest.kt @@ -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 @@ -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(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" @@ -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(HttpMethod.GET, "/test-endpoint") + response.shouldBeInstanceOf>() + 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() diff --git a/client/src/test/kotlin/http/WebSocketReceiverTest.kt b/client/src/test/kotlin/http/WebSocketReceiverTest.kt index 084b83e..05ca4d9 100644 --- a/client/src/test/kotlin/http/WebSocketReceiverTest.kt +++ b/client/src/test/kotlin/http/WebSocketReceiverTest.kt @@ -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) }