Skip to content

Commit

Permalink
kotlin: Add test for query param encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-mman committed Feb 12, 2025
1 parent d04ef57 commit 6c4d1da
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
1 change: 1 addition & 0 deletions kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp:4.12.0"
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5:1.5.21'
testImplementation "org.wiremock:wiremock:3.12.0"
}

jar {
Expand Down
8 changes: 4 additions & 4 deletions kotlin/lib/src/main/kotlin/SvixHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ constructor(private val baseUrl: HttpUrl, val defaultHeaders: Map<String, String
reqBody: Req? = null,
): Res {
var reqBuilder = Request.Builder().url(url)
var jsonBody :String?=null
var jsonBody: String? = null
if (reqBody != null) {
jsonBody = Json.encodeToString(reqBody)
reqBuilder = reqBuilder.method(method, jsonBody.toRequestBody())
Expand All @@ -40,7 +40,7 @@ constructor(private val baseUrl: HttpUrl, val defaultHeaders: Map<String, String
val request = reqBuilder.build()
val debug: String = System.getenv("DEBUG") ?: "no"
if (debug == "yes") {
dbgRequest(request,jsonBody)
dbgRequest(request, jsonBody)
}
val res = client.newCall(request).execute()

Expand All @@ -62,10 +62,10 @@ constructor(private val baseUrl: HttpUrl, val defaultHeaders: Map<String, String
}
}

fun dbgRequest(request: Request,jsonBody:String?) {
fun dbgRequest(request: Request, jsonBody: String?) {
println("_____ start dbg _____")
println("Url: ${request.url}")
println("Method ${request.method} path: ${request.url.encodedPath}")
println("${request.method} /${request.url.toString().split("/").drop(3).joinToString("/")}")
for ((k, v) in request.headers) {
println("$k: $v")
}
Expand Down
79 changes: 79 additions & 0 deletions kotlin/lib/src/test/com/svix/kotlin/WiremockTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.svix.kotlin

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.*
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options
import com.svix.kotlin.models.Ordering
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.*

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class WiremockTests {

private val wireMockServer = WireMockServer(options().port(0).withRootDirectory("lib/src/test/resources"))

@BeforeAll
fun beforeAll() {
wireMockServer.start()
}

@AfterAll
fun afterAll() {
wireMockServer.stop()
}

@BeforeEach
fun beforeEach() {
wireMockServer.resetAll()
}

private fun testClient(): Svix {
return Svix("testtk_asd12.eu", SvixOptions(this.wireMockServer.baseUrl()))
}

@Test
fun testEnumQueryParamEncodedCorrectly() {
val svx = testClient()
wireMockServer.stubFor(
WireMock.get(urlMatching("/api/v1/app?.*"))
.willReturn(WireMock.ok().withBodyFile("ListResponseApplicationOut.json"))
)
runBlocking {
svx.application.list(
ApplicationListOptions(
limit = 5UL,
order = Ordering.ASCENDING,
iterator = "cool-string-!@#$%^",
)
)
}
wireMockServer.verify(
1,
getRequestedFor(
urlEqualTo(
"/api/v1/app?limit=5&iterator=cool-string-%21%40%23%24%25%5E&order=ascending"
)
),
)
}

@Test
fun testSetQueryParamEncodedCorrectly() {
val svx = testClient()
wireMockServer.stubFor(
WireMock.get(urlMatching("/api/v1/app/app_asd123/msg.*"))
.willReturn(WireMock.ok().withBodyFile("ListResponseMessageOut.json"))
)
runBlocking {
svx.message.list(
"app_asd123",
MessageListOptions(eventTypes = setOf("key3", "key4", "key1")),
)
}
wireMockServer.verify(
1,
getRequestedFor(urlEqualTo("/api/v1/app/app_asd123/msg?event_types=key1%2Ckey3%2Ckey4")),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":[{"uid":"unique-identifier","name":"My first application","rateLimit":0,"id":"app_1srOrx2ZWZBpBUvZwXKQmoEYga2","createdAt":"2019-08-24T14:15:22Z","updatedAt":"2019-08-24T14:15:22Z","metadata":{"property1":"string","property2":"string"}}],"iterator":"iterator","prevIterator":"-iterator","done":true}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":[{"eventId":"unique-identifier","eventType":"user.signup","payload":{"email":"[email protected]","type":"user.created","username":"test_user"},"channels":["project_123","group_2"],"id":"msg_1srOrx2ZWZBpBUvZwXKQmoEYga2","timestamp":"2019-08-24T14:15:22Z","tags":["project_1337"]}],"iterator":"iterator","prevIterator":"-iterator","done":true}

0 comments on commit 6c4d1da

Please sign in to comment.