Trendyol / stove

Stove: The easiest way of writing e2e/component tests for your JVM back-end API with Kotlin
https://trendyol.github.io/stove/
Apache License 2.0
174 stars 13 forks source link

(WireMock): Add an optional parameter for url matching strategy, so users can pass the strategy #647

Open osoykan opened 1 week ago

osoykan commented 1 week ago

The current DSL for one of the WireMock operations:

@WiremockDsl
fun mockGet(
  url: String,
  statusCode: Int,
  responseBody: Option<Any> = None,
  metadata: Map<String, Any> = mapOf()
): WireMockSystem {
  val mockRequest = get(urlEqualTo(url))
  mockRequest.withMetadata(metadata)
  val mockResponse = configureBody(statusCode, responseBody)
  val stub = wireMock.stubFor(mockRequest.willReturn(mockResponse).withId(UUID.randomUUID()))
  stubLog.put(stub.id, stub)
  return this
}

The behavior of urlEqualTo(url) is explicitly defined by Stove, but there might be cases where users might want to relax the equality here. So, this can be provided from outside, as function parameter. Although, this might lead users to carefully design their endpoints and tests since relaxed mocks can be shared with other tests, and it can introduce flaky test results.

Suggestion:

@WiremockDsl
fun mockGet(
  url: String,
  statusCode: Int,
  responseBody: Option<Any> = None,
  urlPatternFn: (url: String) -> UrlPattern = { urlEqualTo(it) },
  metadata: Map<String, Any> = mapOf()
): WireMockSystem {
  val mockRequest = get(urlPatternFn(url))
  mockRequest.withMetadata(metadata)
  val mockResponse = configureBody(statusCode, responseBody)
  val stub = wireMock.stubFor(mockRequest.willReturn(mockResponse).withId(UUID.randomUUID()))
  stubLog.put(stub.id, stub)
  return this
}

// Usage:
 wiremock {
   mockGet(
     "/suppliers/**",
     statusCode = 200,
     responseBody = supplierPermission.some(),
     urlPatternFn = { url -> urlMatching(url) }
   )
 }