From 71d8fee597c024aa3828231acd9e7f0a3ffd3271 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 26 Mar 2024 16:44:35 +1100 Subject: [PATCH] feat(consumer/groovy): Support matchers on plain text bodies #443 --- .../dius/pact/consumer/groovy/BaseBuilder.kt | 11 +++++++ .../consumer/groovy/PactBuilderSpec.groovy | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/consumer/groovy/src/main/kotlin/au/com/dius/pact/consumer/groovy/BaseBuilder.kt b/consumer/groovy/src/main/kotlin/au/com/dius/pact/consumer/groovy/BaseBuilder.kt index ffd6bfbd95..513ebee981 100644 --- a/consumer/groovy/src/main/kotlin/au/com/dius/pact/consumer/groovy/BaseBuilder.kt +++ b/consumer/groovy/src/main/kotlin/au/com/dius/pact/consumer/groovy/BaseBuilder.kt @@ -6,6 +6,8 @@ import au.com.dius.pact.core.matchers.matcherCatalogueEntries import au.com.dius.pact.core.model.IHttpPart import au.com.dius.pact.core.model.OptionalBody import au.com.dius.pact.core.model.PactSpecVersion +import au.com.dius.pact.core.model.ContentType +import au.com.dius.pact.core.model.generators.Category import au.com.dius.pact.core.model.generators.Generators import au.com.dius.pact.core.model.generators.ProviderStateGenerator import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory @@ -42,6 +44,15 @@ open class BaseBuilder( httpPart.matchingRules.addCategory(body::class.property("matchers")?.get(body) as MatchingRuleCategory) httpPart.generators.addGenerators(body::class.property("generators")?.get(body) as Generators) OptionalBody.body(body::class.property(BODY)?.get(body).toString().toByteArray(contentType.asCharset())) + } else if (body is Matcher) { + httpPart.matchingRules.addCategory("body").addRule("$", body.matcher!!) + if (body.generator != null) { + httpPart.generators.addGenerator(Category.BODY, "$", body.generator!!) + } + if (!httpPart.hasHeader("Content-Type")) { + httpPart.headers["Content-Type"] = listOf(ContentType.TEXT_PLAIN.toString()) + } + OptionalBody.body(body.value?.toString()?.toByteArray(contentType.asCharset()), ContentType.TEXT_PLAIN) } else if (body != null && body !is String) { if (contentType.isBinaryType()) { when (body) { diff --git a/consumer/groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/PactBuilderSpec.groovy b/consumer/groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/PactBuilderSpec.groovy index 4e98b87e92..e2bdc68096 100644 --- a/consumer/groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/PactBuilderSpec.groovy +++ b/consumer/groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/PactBuilderSpec.groovy @@ -346,4 +346,35 @@ class PactBuilderSpec extends Specification { new ProviderState('provider state one'), new ProviderState('provider state two') ] } + + @Issue('#443') + def 'supports regex matcher on plain text body'() { + given: + aliceService { + uponReceiving('a request with plain test') + withAttributes( + method: 'post', + path: '/random', + body: regexp(~/\w+/, 'randomText')) + willRespondWith( + status: 200, + ) + } + + when: + aliceService.updateInteractions() + + then: + aliceService.interactions.size() == 1 + aliceService.interactions[0].request.body.valueAsString() == 'randomText' + aliceService.interactions[0].request.headers == [ + 'Content-Type': ['text/plain; charset=ISO-8859-1'] + ] + aliceService.interactions[0].request.matchingRules.toV3Map(null) == [ + body: [ + '$': [ + matchers: [[match: 'regex', regex: '\\w+']], combine: 'AND'] + ] + ] + } }