Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,93 @@ class HttpSystemTests :
}
}

test("should match POST request with partial body containing specific fields") {
val aUniqueIdentifierForTest = 123
val aProductCategoryForTest = "food"
val expectedPostDtoName = UUID.randomUUID().toString()

TestSystem.validate {
wiremock {
mockPostRequestContaining(
url = "/post-with-request-containing",
requestContaining = mapOf("productId" to aUniqueIdentifierForTest),
statusCode = 200,
responseBody = TestDto(expectedPostDtoName).some()
)
}

http {
postAndExpectBody<TestDto>(
uri = "/post-with-request-containing",
body = mapOf(
"productId" to aUniqueIdentifierForTest,
"productCategory" to aProductCategoryForTest
).some()
) { actual ->
actual.body().name shouldBe expectedPostDtoName
}
}
}
}

test("should match PUT request with partial body containing specific fields") {
val aUniqueIdentifierForTest = 123
val aProductCategoryForTest = "food"
val expectedPutDtoName = UUID.randomUUID().toString()

TestSystem.validate {
wiremock {
mockPutRequestContaining(
url = "/put-with-request-containing",
requestContaining = mapOf("productId" to aUniqueIdentifierForTest),
statusCode = 200,
responseBody = TestDto(expectedPutDtoName).some()
)
}

http {
putAndExpectBody<TestDto>(
uri = "/put-with-request-containing",
body = mapOf(
"productId" to aUniqueIdentifierForTest,
"productCategory" to aProductCategoryForTest
).some()
) { actual ->
actual.body().name shouldBe expectedPutDtoName
}
}
}
}

test("should match PATCH request with partial body containing specific fields") {
val aUniqueIdentifierForTest = 123
val aProductCategoryForTest = "food"
val expectedPatchDtoName = UUID.randomUUID().toString()

TestSystem.validate {
wiremock {
mockPatchRequestContaining(
url = "/patch-with-request-containing",
requestContaining = mapOf("productId" to aUniqueIdentifierForTest),
statusCode = 200,
responseBody = TestDto(expectedPatchDtoName).some()
)
}

http {
patchAndExpectBody<TestDto>(
uri = "/patch-with-request-containing",
body = mapOf(
"productId" to aUniqueIdentifierForTest,
"productCategory" to aProductCategoryForTest
).some()
) { actual ->
actual.body().name shouldBe expectedPatchDtoName
}
}
}
}

test("java time instant should work") {
val expectedGetDtoName = UUID.randomUUID().toString()
TestSystem.validate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,84 @@ class WireMockSystem(
return this
}

@WiremockDsl
fun mockPostRequestContaining(
url: String,
requestContaining: Map<String, Any>,
statusCode: Int = 200,
responseBody: Option<Any> = None,
urlPatternFn: (url: String) -> UrlPattern = { urlEqualTo(it) }
): WireMockSystem = mockPostConfigure(url, urlPatternFn) { builder, serde ->

requestContaining.forEach { (key, value) ->
builder.withRequestBody(
matchingJsonPath(
"$.$key",
equalToJson(String(serde.serialize(value)))
)
)
}

val response = aResponse()
.withStatus(statusCode)
.withHeader("Content-Type", "application/json; charset=UTF-8")
.also { rb -> responseBody.map { rb.withBody(serde.serialize(it)) } }

builder.willReturn(response)
}

@WiremockDsl
fun mockPutRequestContaining(
url: String,
requestContaining: Map<String, Any>,
statusCode: Int = 200,
responseBody: Option<Any> = None,
urlPatternFn: (url: String) -> UrlPattern = { urlEqualTo(it) }
): WireMockSystem = mockPutConfigure(url, urlPatternFn) { builder, serde ->

requestContaining.forEach { (key, value) ->
builder.withRequestBody(
matchingJsonPath(
"$.$key",
equalToJson(String(serde.serialize(value)))
)
)
}

val response = aResponse()
.withStatus(statusCode)
.withHeader("Content-Type", "application/json; charset=UTF-8")
.also { rb -> responseBody.map { rb.withBody(serde.serialize(it)) } }

builder.willReturn(response)
}

@WiremockDsl
fun mockPatchRequestContaining(
url: String,
requestContaining: Map<String, Any>,
statusCode: Int = 200,
responseBody: Option<Any> = None,
urlPatternFn: (url: String) -> UrlPattern = { urlEqualTo(it) }
): WireMockSystem = mockPatchConfigure(url, urlPatternFn) { builder, serde ->

requestContaining.forEach { (key, value) ->
builder.withRequestBody(
matchingJsonPath(
"$.$key",
equalToJson(String(serde.serialize(value)))
)
)
}

val response = aResponse()
.withStatus(statusCode)
.withHeader("Content-Type", "application/json; charset=UTF-8")
.also { rb -> responseBody.map { rb.withBody(serde.serialize(it)) } }

builder.willReturn(response)
}

@WiremockDsl
fun behaviourFor(
url: String,
Expand Down