diff --git a/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt index 746cae3..3f95231 100644 --- a/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt @@ -1,21 +1,28 @@ package com.vonage.client.kt +import com.fasterxml.jackson.databind.ObjectMapper import com.github.tomakehurst.wiremock.WireMockServer +import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.common.ConsoleNotifier import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options +import com.marcinziolo.kotlin.wiremock.* +import com.vonage.client.common.HttpMethod import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach abstract class AbstractTest { + val apiKey = "a1b2c3d4" + val applicationId = "00000000-0000-4000-8000-000000000000" + private val apiSecret = "1234567890abcdef" + private val apiKeySecretEncoded = "YTFiMmMzZDQ6MTIzNDU2Nzg5MGFiY2RlZg==" + val port = 8081 val wiremock: WireMockServer = WireMockServer( options().port(port).notifier(ConsoleNotifier(false)) ) val vonageClient = Vonage { - apiKey("a1b2c3d4") - apiSecret("1234567890abcdef") - applicationId("00000000-0000-4000-8000-000000000000") + apiKey(apiKey); apiSecret(apiSecret); applicationId(applicationId) privateKeyPath("src/test/resources/com/vonage/client/kt/application_key") httpConfig { baseUri("http://localhost:$port") @@ -32,4 +39,52 @@ abstract class AbstractTest { wiremock.resetAll() wiremock.stop() } + + protected enum class ContentType(val mime: String) { + APPLICATION_JSON("application/json"), + FORM_URLENCODED("application/x-www-form-urlencoded"); + + @Override + override fun toString(): String { + return mime + } + } + + protected enum class AuthType { + JWT, API_KEY_SECRET + } + + protected fun baseMockRequest( + httpMethod: HttpMethod, + expectedUrl: String, + contentType: ContentType?, + accept: ContentType?, + authType: AuthType?, + expectedBodyParams: Map? = null) = + wiremock.requestServerBuilderStep({ + url equalTo expectedUrl + headers contains "User-Agent" like "vonage-java-sdk.*" + if (authType != null) { + headers contains "Authorization" like when (authType) { + AuthType.JWT -> "Bearer eyJ.+" + AuthType.API_KEY_SECRET -> "Basic $apiKeySecretEncoded" + } + } + if (contentType != null) { + headers contains "Content-Type" equalTo contentType.mime + } + if (accept != null) { + headers contains "Accept" equalTo accept.mime + } + if (expectedBodyParams != null) { + body equalTo ObjectMapper().writeValueAsString(expectedBodyParams) + } + }, when (httpMethod) { + HttpMethod.GET -> WireMock::get + HttpMethod.POST -> WireMock::post + HttpMethod.PUT -> WireMock::put + HttpMethod.PATCH -> WireMock::patch + HttpMethod.DELETE -> WireMock::delete + else -> throw IllegalArgumentException("Unhandled HTTP method: $httpMethod") + }) } \ No newline at end of file diff --git a/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt b/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt index f5aa4a9..7e6f34e 100644 --- a/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt @@ -2,6 +2,7 @@ package com.vonage.client.kt import com.fasterxml.jackson.databind.ObjectMapper import com.marcinziolo.kotlin.wiremock.* +import com.vonage.client.common.HttpMethod import com.vonage.client.messages.Channel import com.vonage.client.messages.MessageRequest import com.vonage.client.messages.MessageResponseException @@ -36,16 +37,10 @@ class MessagesTest : AbstractTest() { private val captionMap = mapOf("caption" to caption) private fun baseMockRequest(expectedBodyParams: Map? = null) = - wiremock.post { - url equalTo "/v1/messages" - headers contains "User-Agent" like "vonage-java-sdk.*" - headers contains "Authorization" like "Bearer eyJ.+" - headers contains "Content-Type" equalTo "application/json" - headers contains "Accept" equalTo "application/json" - if (expectedBodyParams != null) { - body equalTo ObjectMapper().writeValueAsString(expectedBodyParams) - } - } + baseMockRequest(HttpMethod.POST, "/v1/messages", + ContentType.APPLICATION_JSON, ContentType.APPLICATION_JSON, + AuthType.JWT, expectedBodyParams + ) private fun mock202Response(expectedBodyParams: Map) { baseMockRequest(expectedBodyParams) returns {