Skip to content

Commit

Permalink
fix: matchPath should validate that the example provided explicitly m…
Browse files Browse the repository at this point in the history
…atches the regex #1767
  • Loading branch information
rholshausen committed Feb 15, 2024
1 parent b52f5a7 commit 152c7bc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.dius.pact.consumer.dsl

import au.com.dius.pact.consumer.ConsumerPactBuilder
import au.com.dius.pact.consumer.InvalidMatcherException
import au.com.dius.pact.consumer.xml.PactXmlBuilder
import au.com.dius.pact.core.model.Consumer
import au.com.dius.pact.core.model.ContentType.Companion.JSON
Expand Down Expand Up @@ -384,6 +385,11 @@ open class PactDslRequestWithPath : PactDslRequestBase {
*/
@JvmOverloads
fun matchPath(pathRegex: String, path: String = Generex(pathRegex).random()): PactDslRequestWithPath {
val re = Regex(pathRegex)
if (!path.matches(re)) {
throw InvalidMatcherException("Example \"$path\" does not match regular expression \"$pathRegex\"")
}

requestMatchers.addCategory("path").addRule(RegexMatcher(pathRegex))
this.path = path
return this
Expand All @@ -403,6 +409,11 @@ open class PactDslRequestWithPath : PactDslRequestBase {
regex: String,
headerExample: String = Generex(regex).random()
): PactDslRequestWithPath {
val re = Regex(regex)
if (!headerExample.matches(re)) {
throw InvalidMatcherException("Example \"$headerExample\" does not match regular expression \"$regex\"")
}

requestMatchers.addCategory("header").setRule(header, RegexMatcher(regex))
requestHeaders[header] = listOf(headerExample)
return this
Expand Down Expand Up @@ -440,6 +451,11 @@ open class PactDslRequestWithPath : PactDslRequestBase {
regex: String,
example: String = Generex(regex).random()
): PactDslRequestWithPath {
val re = Regex(regex)
if (!example.matches(re)) {
throw InvalidMatcherException("Example \"$example\" does not match regular expression \"$regex\"")
}

requestMatchers.addCategory("query").addRule(parameter, RegexMatcher(regex))
query[parameter] = listOf(example)
return this
Expand All @@ -453,6 +469,13 @@ open class PactDslRequestWithPath : PactDslRequestBase {
* @param example Example value list to use for the query parameter (unencoded)
*/
fun matchQuery(parameter: String, regex: String, example: List<String>): PactDslRequestWithPath {
val re = Regex(regex)
for (e in example) {
if (!e.matches(re)) {
throw InvalidMatcherException("Example \"$e\" does not match regular expression \"$regex\"")
}
}

requestMatchers.addCategory("query").addRule(parameter, RegexMatcher(regex))
query[parameter] = example
return this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.dius.pact.consumer.dsl

import au.com.dius.pact.consumer.ConsumerPactBuilder
import au.com.dius.pact.consumer.InvalidMatcherException
import au.com.dius.pact.consumer.xml.PactXmlBuilder
import au.com.dius.pact.core.model.OptionalBody.Companion.body
import au.com.dius.pact.core.model.PactSpecVersion
Expand Down Expand Up @@ -324,6 +325,11 @@ open class PactDslRequestWithoutPath @JvmOverloads constructor(
*/
@JvmOverloads
fun matchPath(pathRegex: String, path: String = Generex(pathRegex).random()): PactDslRequestWithPath {
val re = Regex(pathRegex)
if (!path.matches(re)) {
throw InvalidMatcherException("Example \"$path\" does not match regular expression \"$pathRegex\"")
}

requestMatchers.addCategory("path").addRule(RegexMatcher(pathRegex))
return PactDslRequestWithPath(consumerPactBuilder, consumerName, providerName, pactDslWithState.state,
description, path, requestMethod, requestHeaders, query, requestBody, requestMatchers, requestGenerators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,52 @@ class PactDslRequestWithPathSpec extends Specification {
'$': [matchers: [[match: 'contentType', value: 'application/xml']], combine: 'AND']
]
}
@Issue('#1767')
def 'match path should valid the example against the regex'() {
given:
def request = ConsumerPactBuilder.consumer('spec')
.hasPactWith('provider')
.uponReceiving('a XML request')
.path('/path')
when:
request.matchPath('\\d+', 'abcd')
then:
def ex = thrown(au.com.dius.pact.consumer.InvalidMatcherException)
ex.message == 'Example "abcd" does not match regular expression "\\d+"'
}
@Issue('#1767')
def 'match header should valid the example against the regex'() {
given:
def request = ConsumerPactBuilder.consumer('spec')
.hasPactWith('provider')
.uponReceiving('a XML request')
.path('/path')
when:
request.matchHeader('H', '\\d+', 'abcd')
then:
def ex = thrown(au.com.dius.pact.consumer.InvalidMatcherException)
ex.message == 'Example "abcd" does not match regular expression "\\d+"'
}
@Issue('#1767')
def 'match query parameter should valid the example against the regex'() {
given:
def request = ConsumerPactBuilder.consumer('spec')
.hasPactWith('provider')
.uponReceiving('a XML request')
.path('/path')
when:
request.matchQuery('H', '\\d+', 'abcd')
then:
def ex = thrown(au.com.dius.pact.consumer.InvalidMatcherException)
ex.message == 'Example "abcd" does not match regular expression "\\d+"'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,19 @@ class PactDslRequestWithoutPathSpec extends Specification {
'$': [matchers: [[match: 'contentType', value: 'application/xml']], combine: 'AND']
]
}

@Issue('#1767')
def 'match path should valid the example against the regex'() {
given:
def request = ConsumerPactBuilder.consumer('spec')
.hasPactWith('provider')
.uponReceiving('a XML request')

when:
request.matchPath('\\/\\d+', '/abcd')

then:
def ex = thrown(au.com.dius.pact.consumer.InvalidMatcherException)
ex.message == 'Example "/abcd" does not match regular expression "\\/\\d+"'
}
}

0 comments on commit 152c7bc

Please sign in to comment.