Skip to content

Commit

Permalink
Merge pull request #1 from ChuckerTeam/develop
Browse files Browse the repository at this point in the history
Add tests to format utils (ChuckerTeam#281)
  • Loading branch information
adammasyk authored Mar 22, 2020
2 parents 73ddd2a + c343002 commit 60c25a8
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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<Context> {
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)
}
}
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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 = "<b> Accept: </b>text/html <br /><b> Authorization: </b>exampleToken <br />"
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 =
"""
<example>value</example>
""".trimIndent()
val expected =
"""
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<example>value</example>
""".trimIndent()
assertThat(FormatUtils.formatXml(xml)).isEqualTo(expected)
}
}

0 comments on commit 60c25a8

Please sign in to comment.