Skip to content

Commit

Permalink
increased test coverage for format utils (ChuckerTeam#291)
Browse files Browse the repository at this point in the history
Co-authored-by: Karthik R <karthr@paypal.com>
  • Loading branch information
adb-shell and Karthik R authored Mar 27, 2020
1 parent d39b759 commit 59d340b
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chuckerteam.chucker.internal.support

import com.chuckerteam.chucker.TestTransactionFactory
import com.chuckerteam.chucker.internal.data.entity.HttpHeader
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -140,4 +141,51 @@ class FormatUtilsTest {
""".trimIndent()
assertThat(FormatUtils.formatXml(xml)).isEqualTo(expected)
}

@Test
fun testCurlCommandWithoutHeaders() {
getRequestMethods().forEach { method ->
val transaction = TestTransactionFactory.createTransaction(method)
val curlСommand = FormatUtils.getShareCurlCommand(transaction)
val expectedCurlCommand = "curl -X $method http://localhost/getUsers"
assertThat(curlСommand).isEqualTo(expectedCurlCommand)
}
}

@Test
fun testCurlCommandWithHeaders() {
val httpHeaders = ArrayList<HttpHeader>()
for (i in 0 until 5) {
httpHeaders.add(HttpHeader("name$i", "value$i"))
}
val dummyHeaders = JsonConverter.instance.toJson(httpHeaders)

getRequestMethods().forEach { method ->
val transaction = TestTransactionFactory.createTransaction(method)
transaction.requestHeaders = dummyHeaders
val curlСommand = FormatUtils.getShareCurlCommand(transaction)
var expectedCurlCommand = "curl -X $method"
httpHeaders.forEach { header ->
expectedCurlCommand += " -H \"${header.name}: ${header.value}\""
}
expectedCurlCommand += " http://localhost/getUsers"
assertThat(curlСommand).isEqualTo(expectedCurlCommand)
}
}

@Test
fun testCurlPostAndPutCommandWithRequestBody() {
getRequestMethods().filter { method ->
method == "POST" || method == "PUT"
}.forEach { method ->
val dummyRequestBody = "{thing:put}"
val transaction = TestTransactionFactory.createTransaction(method)
transaction.requestBody = dummyRequestBody
val curlСommand = FormatUtils.getShareCurlCommand(transaction)
val expectedCurlCommand = "curl -X $method --data $'$dummyRequestBody' http://localhost/getUsers"
assertThat(curlСommand).isEqualTo(expectedCurlCommand)
}
}

private fun getRequestMethods() = arrayOf("GET", "POST", "PUT", "DELETE")
}

0 comments on commit 59d340b

Please sign in to comment.