Skip to content

Commit

Permalink
convert empty httpEntity to {} to avoid DeliveryStatus initialization…
Browse files Browse the repository at this point in the history
… exception

Signed-off-by: Hailong Cui <ihailong@amazon.com>
  • Loading branch information
Hailong-am committed Mar 30, 2023
1 parent 464fdb2 commit 6c5c1a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ class DestinationHttpClient {
@Throws(IOException::class)
fun getResponseString(response: CloseableHttpResponse): String {
val entity: HttpEntity = response.entity ?: return "{}"
return EntityUtils.toString(entity)
val responseString = EntityUtils.toString(entity)
// DeliveryStatus need statusText must not be empty, convert empty response to {}
return if (responseString.isNullOrEmpty()) "{}" else responseString
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal class ChimeDestinationTests {
@Test
fun `test chime message empty entity response`() {
val mockHttpClient: CloseableHttpClient = EasyMock.createMock(CloseableHttpClient::class.java)
val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "")
val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "{}")

val httpResponse = mockk<CloseableHttpResponse>()
EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost::class.java))).andReturn(httpResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internal class CustomWebhookDestinationTests {
@MethodSource("methodToHttpRequestType")
fun `test custom webhook message empty entity response`(method: String, expectedHttpClass: Class<HttpUriRequest>) {
val mockHttpClient: CloseableHttpClient = EasyMock.createMock(CloseableHttpClient::class.java)
val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "")
val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "{}")

val httpResponse = mockk<CloseableHttpResponse>()
EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost::class.java))).andReturn(httpResponse)
Expand Down Expand Up @@ -218,4 +218,17 @@ internal class CustomWebhookDestinationTests {
val actualRequestBody = httpClient.buildRequestBody(destination, message)
assertEquals(messageText, actualRequestBody)
}

@Test
fun `test get response string`() {
val httpClient = DestinationHttpClient()
val response = mockk<CloseableHttpResponse>()
every { response.entity } returns null
var responseString = httpClient.getResponseString(response)
assertEquals(responseString, "{}")

every { response.entity } returns StringEntity("")
responseString = httpClient.getResponseString(response)
assertEquals(responseString, "{}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal class SlackDestinationTests {
@Test
fun `test Slack message empty entity response`() {
val mockHttpClient: CloseableHttpClient = EasyMock.createMock(CloseableHttpClient::class.java)
val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "")
val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "{}")

val httpResponse = mockk<CloseableHttpResponse>()
EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost::class.java))).andReturn(httpResponse)
Expand Down

0 comments on commit 6c5c1a6

Please sign in to comment.