Skip to content

Commit

Permalink
add tests to format utils
Browse files Browse the repository at this point in the history
  • Loading branch information
adammasyk committed Mar 19, 2020
1 parent 244d3da commit 62b120a
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import java.util.Date

object MockTransactionFactory {

internal fun createTransaction(method: String): HttpTransaction {
return HttpTransaction(
0, Date(1300000).time, Date(1300300).time,
1000L, "HTTP", method, "http://localhost/getUsers",
"localhost", "/getUsers", "", "", "",
1000L, "application/json", null,
null, true, 200, "OK",
null, 1000L, "application/json",
null, """{"field": "value"}""", true, 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"
}"""

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"
}"""
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
package com.chuckerteam.chucker.internal.support

import MockTransactionFactory
import android.content.Context
import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.internal.data.entity.HttpHeader
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Test

class FormatUtilsTest {

private val exampleHeadersList = listOf(
HttpHeader("Accept", "text/html"),
HttpHeader("Authorization", "exampleToken")
)

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 testFormatJson_withNullValues() {
val parsedJson = FormatUtils.formatJson(
Expand Down Expand Up @@ -58,4 +88,79 @@ 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() {
val oneKb = 1024L
val resultNonSi = FormatUtils.formatByteCount(oneKb, false)
val resultSi = FormatUtils.formatByteCount(oneKb, true)
val expectedNonSi = "1.0 KiB"
val expectedSi = "1.0 kB"
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>"""
val expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<example>value</example>\n"
assertThat(FormatUtils.formatXml(xml)).isEqualTo(expected)
}

@Test
fun getShareTextForGetTransaction() {
assertThat(
FormatUtils.getShareText(contextMock, MockTransactionFactory.createTransaction("GET"), false)
)
.isEqualTo(MockTransactionFactory.expectedGetHttpTransaction)
}

@Test
fun getShareTextForPostTransaction() {
assertThat(
FormatUtils.getShareText(contextMock, MockTransactionFactory.createTransaction("POST"), false)
)
.isEqualTo(MockTransactionFactory.expectedHttpPostTransaction)
}
}

0 comments on commit 62b120a

Please sign in to comment.