Skip to content

Commit

Permalink
feat(consumer/groovy): Support matchers on plain text bodies #443
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Mar 26, 2024
1 parent 7f70936 commit 71d8fee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]
]
}
}

0 comments on commit 71d8fee

Please sign in to comment.