From c34300232fab219a7fa4b407243e4b03a3e47634 Mon Sep 17 00:00:00 2001 From: adammasyk Date: Sat, 21 Mar 2020 20:23:05 +0100 Subject: [PATCH] Add tests to format utils (#281) * add tests to format utils * fixes after code review * formatting fix Co-authored-by: adammasyk --- .../chucker/TestTransactionFactory.kt | 95 +++++++++++++++++++ .../support/FormatUtilsSharedTextTest.kt | 47 +++++++++ .../internal/support/FormatUtilsTest.kt | 82 ++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 library/src/test/java/com/chuckerteam/chucker/TestTransactionFactory.kt create mode 100644 library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsSharedTextTest.kt diff --git a/library/src/test/java/com/chuckerteam/chucker/TestTransactionFactory.kt b/library/src/test/java/com/chuckerteam/chucker/TestTransactionFactory.kt new file mode 100644 index 000000000..85682f272 --- /dev/null +++ b/library/src/test/java/com/chuckerteam/chucker/TestTransactionFactory.kt @@ -0,0 +1,95 @@ +package com.chuckerteam.chucker + +import com.chuckerteam.chucker.internal.data.entity.HttpTransaction +import java.util.Date + +object TestTransactionFactory { + + internal fun createTransaction(method: String): HttpTransaction { + return HttpTransaction( + id = 0, + requestDate = Date(1300000).time, + responseDate = Date(1300300).time, + tookMs = 1000L, + protocol = "HTTP", + method = method, + url = "http://localhost/getUsers", + host = "localhost", + path = "/getUsers", + scheme = "", + responseTlsVersion = "", + responseCipherSuite = "", + requestContentLength = 1000L, + requestContentType = "application/json", + requestHeaders = null, + requestBody = null, + isRequestBodyPlainText = true, + responseCode = 200, + responseMessage = "OK", + error = null, + responseContentLength = 1000L, + responseContentType = "application/json", + responseHeaders = null, + responseBody = + """{"field": "value"}""", + isResponseBodyPlainText = true, + responseImageData = null + ) + } + + val expectedGetHttpTransaction = + """ + URL: http://localhost/getUsers + Method: GET + Protocol: HTTP + Status: Complete + Response: 200 OK + SSL: No + + Request time: ${Date(1300000)} + Response time: ${Date(1300300)} + Duration: 1000 ms + + Request size: 1.0 kB + Response size: 1.0 kB + Total size: 2.0 kB + + ---------- Request ---------- + + + + ---------- Response ---------- + + { + "field": "value" + } + """.trimIndent() + + val expectedHttpPostTransaction = + """ + URL: http://localhost/getUsers + Method: POST + Protocol: HTTP + Status: Complete + Response: 200 OK + SSL: No + + Request time: ${Date(1300000)} + Response time: ${Date(1300300)} + Duration: 1000 ms + + Request size: 1.0 kB + Response size: 1.0 kB + Total size: 2.0 kB + + ---------- Request ---------- + + + + ---------- Response ---------- + + { + "field": "value" + } + """.trimIndent() +} diff --git a/library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsSharedTextTest.kt b/library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsSharedTextTest.kt new file mode 100644 index 000000000..6770767ac --- /dev/null +++ b/library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsSharedTextTest.kt @@ -0,0 +1,47 @@ +package com.chuckerteam.chucker.internal.support + +import android.content.Context +import com.chuckerteam.chucker.R +import com.chuckerteam.chucker.TestTransactionFactory +import com.google.common.truth.Truth +import io.mockk.every +import io.mockk.mockk +import org.junit.jupiter.api.Test + +class FormatUtilsSharedTextTest { + + private val contextMock = mockk { + every { getString(R.string.chucker_url) } returns "URL" + every { getString(R.string.chucker_method) } returns "Method" + every { getString(R.string.chucker_protocol) } returns "Protocol" + every { getString(R.string.chucker_status) } returns "Status" + every { getString(R.string.chucker_response) } returns "Response" + every { getString(R.string.chucker_ssl) } returns "SSL" + every { getString(R.string.chucker_yes) } returns "Yes" + every { getString(R.string.chucker_no) } returns "No" + every { getString(R.string.chucker_request_time) } returns "Request time" + every { getString(R.string.chucker_response_time) } returns "Response time" + every { getString(R.string.chucker_duration) } returns "Duration" + every { getString(R.string.chucker_request_size) } returns "Request size" + every { getString(R.string.chucker_response_size) } returns "Response size" + every { getString(R.string.chucker_total_size) } returns "Total size" + every { getString(R.string.chucker_request) } returns "Request" + every { getString(R.string.chucker_body_omitted) } returns "(encoded or binary body omitted)" + } + + @Test + fun getShareTextForGetTransaction() { + Truth.assertThat( + FormatUtils.getShareText(contextMock, TestTransactionFactory.createTransaction("GET"), false) + ) + .isEqualTo(TestTransactionFactory.expectedGetHttpTransaction) + } + + @Test + fun getShareTextForPostTransaction() { + Truth.assertThat( + FormatUtils.getShareText(contextMock, TestTransactionFactory.createTransaction("POST"), false) + ) + .isEqualTo(TestTransactionFactory.expectedHttpPostTransaction) + } +} diff --git a/library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsTest.kt b/library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsTest.kt index 50a80a140..3294802a1 100644 --- a/library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsTest.kt +++ b/library/src/test/java/com/chuckerteam/chucker/internal/support/FormatUtilsTest.kt @@ -1,10 +1,16 @@ package com.chuckerteam.chucker.internal.support +import com.chuckerteam.chucker.internal.data.entity.HttpHeader import com.google.common.truth.Truth.assertThat import org.junit.jupiter.api.Test class FormatUtilsTest { + private val exampleHeadersList = listOf( + HttpHeader("Accept", "text/html"), + HttpHeader("Authorization", "exampleToken") + ) + @Test fun testFormatJson_withNullValues() { val parsedJson = FormatUtils.formatJson( @@ -58,4 +64,80 @@ class FormatUtilsTest { """.trimIndent() ) } + + @Test + fun testFormatHeaders_withNullValues() { + val result = FormatUtils.formatHeaders(null, false) + assertThat(result).isEmpty() + } + + @Test + fun testFormatHeaders_withEmptyValues() { + val result = FormatUtils.formatHeaders(listOf(), false) + assertThat(result).isEmpty() + } + + @Test + fun testFormatHeaders_withoutMarkup() { + val result = FormatUtils.formatHeaders(exampleHeadersList, false) + val expected = "Accept: text/html\nAuthorization: exampleToken\n" + assertThat(result).isEqualTo(expected) + } + + @Test + fun testFormatHeaders_withMarkup() { + val result = FormatUtils.formatHeaders(exampleHeadersList, true) + val expected = " Accept: text/html
Authorization: exampleToken
" + assertThat(result).isEqualTo(expected) + } + + @Test + fun testFormatByteCount_zeroBytes() { + val resultNonSi = FormatUtils.formatByteCount(0, false) + val resultSi = FormatUtils.formatByteCount(0, true) + val expected = "0 B" + assertThat(resultNonSi).isEqualTo(expected) + assertThat(resultSi).isEqualTo(expected) + } + + @Test + fun testFormatByteCount_oneKiloByte() { + testFormatByteCount(1024L, "1.0 kB", "1.0 KiB") + } + + @Test + fun testFormatByteCount_oneKiloByteSi() { + testFormatByteCount(1023L, "1.0 kB", "1023 B") + } + + private fun testFormatByteCount( + byteCountToTest: Long, + expectedSi: String, + expectedNonSi: String + ) { + val resultNonSi = FormatUtils.formatByteCount(byteCountToTest, false) + val resultSi = FormatUtils.formatByteCount(byteCountToTest, true) + assertThat(resultNonSi).isEqualTo(expectedNonSi) + assertThat(resultSi).isEqualTo(expectedSi) + } + + @Test + fun formatXml_emptyString() { + assertThat(FormatUtils.formatXml("")).isEmpty() + } + + @Test + fun formatXml_properXml() { + val xml = + """ + value + """.trimIndent() + val expected = + """ + + value + + """.trimIndent() + assertThat(FormatUtils.formatXml(xml)).isEqualTo(expected) + } }