@@ -27,6 +27,7 @@ import org.junit.jupiter.api.AfterEach
27
27
import org.junit.jupiter.api.BeforeEach
28
28
import org.junit.jupiter.api.assertThrows
29
29
import com.fasterxml.jackson.databind.ObjectMapper
30
+ import com.github.tomakehurst.wiremock.client.MappingBuilder
30
31
import com.vonage.client.HttpWrapper
31
32
import com.vonage.client.users.channels.Websocket
32
33
import java.net.URI
@@ -105,7 +106,7 @@ abstract class AbstractTest {
105
106
protected val fileUrl = " $exampleUrlBase /file.pdf"
106
107
107
108
private val port = 8081
108
- private val wmBaseUrl = " http://localhost:$port "
109
+ protected val wmBaseUrl = " http://localhost:$port "
109
110
private val wiremock: WireMockServer = WireMockServer (
110
111
options().port(port).notifier(ConsoleNotifier (false ))
111
112
)
@@ -168,28 +169,31 @@ abstract class AbstractTest {
168
169
169
170
private fun Any.toJson (): String = ObjectMapper ().writeValueAsString(this )
170
171
172
+ private fun MappingBuilder.withAuth (authType : AuthType ? ): MappingBuilder {
173
+ when (authType) {
174
+ AuthType .API_KEY_SECRET_QUERY_PARAMS -> {
175
+ withFormParam(apiKeyName, equalTo(apiKey))
176
+ .withFormParam(apiSecretName, equalTo(apiSecret))
177
+ }
178
+ AuthType .JWT -> withHeader(authHeaderName, matching(jwtBearerPattern))
179
+ AuthType .ACCESS_TOKEN -> withHeader(authHeaderName, equalTo(accessTokenBearer))
180
+ AuthType .API_KEY_SECRET_HEADER -> withHeader(authHeaderName, equalTo(basicSecretEncodedHeader))
181
+ AuthType .API_KEY_SIGNATURE_SECRET -> withFormParam(apiKeyName, equalTo(apiKey))
182
+ null -> Unit
183
+ }
184
+ return this
185
+ }
186
+
171
187
protected fun mockPostQueryParams (expectedUrl : String , expectedRequestParams : Map <String , Any >,
172
188
authType : AuthType ? = AuthType .API_KEY_SECRET_QUERY_PARAMS ,
173
189
contentType : Boolean = false, status : Int = 200,
174
190
expectedResponseParams : Any? = null) {
175
191
176
- val stub = post(urlPathEqualTo(expectedUrl))
192
+ val stub = post(urlPathEqualTo(expectedUrl)).withAuth(authType)
177
193
if (contentType) {
178
194
stub.withHeader(contentTypeHeaderName, equalTo(ContentType .FORM_URLENCODED .mime))
179
195
}
180
196
181
- when (authType) {
182
- AuthType .API_KEY_SECRET_QUERY_PARAMS -> {
183
- stub.withFormParam(apiKeyName, equalTo(apiKey))
184
- .withFormParam(apiSecretName, equalTo(apiSecret))
185
- }
186
- AuthType .JWT -> stub.withHeader(authHeaderName, matching(jwtBearerPattern))
187
- AuthType .ACCESS_TOKEN -> stub.withHeader(authHeaderName, equalTo(accessTokenBearer))
188
- AuthType .API_KEY_SECRET_HEADER -> stub.withHeader(authHeaderName, equalTo(basicSecretEncodedHeader))
189
- AuthType .API_KEY_SIGNATURE_SECRET -> stub.withFormParam(apiKeyName, equalTo(apiKey))
190
- null -> Unit
191
- }
192
-
193
197
expectedRequestParams.forEach {(k, v) -> stub.withFormParam(k, equalTo(v.toString()))}
194
198
195
199
val response = aResponse().withStatus(status)
@@ -200,6 +204,13 @@ abstract class AbstractTest {
200
204
wiremock.stubFor(stub)
201
205
}
202
206
207
+ protected fun mockGetBinary (resourceUrl : String , body : ByteArray , authType : AuthType ? = AuthType .JWT ) {
208
+ wiremock.stubFor(
209
+ get(urlPathEqualTo(resourceUrl)).withAuth(authType)
210
+ .willReturn(aResponse().withBody(body).withStatus(200 ))
211
+ )
212
+ }
213
+
203
214
protected fun mockRequest (
204
215
httpMethod : HttpMethod ,
205
216
expectedUrl : String ,
@@ -283,7 +294,6 @@ abstract class AbstractTest {
283
294
mockRequest(HttpMethod .GET , expectedUrl, accept = ContentType .APPLICATION_JSON , authType = authType,
284
295
expectedParams = expectedQueryParams).mockReturn(status, expectedResponseParams)
285
296
286
-
287
297
protected fun BuildingStep.mockReturn (
288
298
status : Int? = null, expectedBody : Map <String , Any >? = null): ReturnsStep =
289
299
returns {
0 commit comments