Skip to content

Commit ec066a0

Browse files
committed
fix: Trim regex anchors before generating random strings from the regex #1826
1 parent 0554c1a commit ec066a0

File tree

19 files changed

+132
-26
lines changed

19 files changed

+132
-26
lines changed

consumer/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ dependencies {
1414

1515
implementation 'org.apache.httpcomponents.client5:httpclient5-fluent'
1616
implementation 'com.googlecode.java-diff-utils:diffutils:1.3.0'
17-
implementation 'dk.brics.automaton:automaton:1.11-8'
1817
implementation('io.netty:netty-handler') {
1918
exclude module: 'netty-transport-native-kqueue'
2019
}
@@ -26,7 +25,6 @@ dependencies {
2625
exclude group: 'au.com.dius.pact.core'
2726
}
2827
implementation 'org.apache.commons:commons-lang3'
29-
implementation 'com.github.mifmif:generex:1.0.2'
3028
implementation 'org.apache.commons:commons-io:1.3.2'
3129
implementation 'org.apache.commons:commons-text:1.10.0'
3230
implementation 'org.apache.tika:tika-core'

consumer/groovy/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ dependencies {
1111
implementation 'org.apache.groovy:groovy'
1212
implementation 'org.apache.groovy:groovy-json'
1313
implementation 'org.apache.httpcomponents.client5:httpclient5'
14-
implementation 'com.github.mifmif:generex:1.0.2'
1514
implementation 'org.apache.commons:commons-lang3'
1615
implementation 'org.apache.commons:commons-collections4'
1716
implementation('io.pact.plugin.driver:core') {

consumer/groovy/src/main/kotlin/au/com/dius/pact/consumer/groovy/Matchers.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher
2323
import au.com.dius.pact.core.model.matchingrules.MinTypeMatcher
2424
import au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher
2525
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
26+
import au.com.dius.pact.core.support.Random
2627
import au.com.dius.pact.core.support.isNotEmpty
27-
import com.mifmif.common.regex.Generex
2828
import io.github.oshai.kotlinlogging.KLogging
2929
import org.apache.commons.lang3.time.DateFormatUtils
3030
import org.apache.commons.lang3.time.DateUtils
@@ -69,7 +69,7 @@ class RegexpMatcher @JvmOverloads constructor(
6969
value: String? = null
7070
) : Matcher(value, RegexMatcher(regex, value), if (value == null) RegexGenerator(regex) else null) {
7171
override val value: Any?
72-
get() = super.value ?: Generex(regex).random()
72+
get() = super.value ?: Random.generateRandomString(regex)
7373
}
7474

7575
class HexadecimalMatcher @JvmOverloads constructor(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package au.com.dius.pact.consumer.groovy
2+
3+
import spock.lang.Issue
4+
import spock.lang.Specification
5+
6+
class RegexpMatcherSpec extends Specification {
7+
def 'returns the value provided to the constructor'() {
8+
expect:
9+
new RegexpMatcher('\\w+', 'word').value == 'word'
10+
}
11+
12+
def 'if no value is provided to the constructor, generates a random value when needed'() {
13+
expect:
14+
new RegexpMatcher('\\w+', null).value ==~ /\w+/
15+
}
16+
17+
@Issue('#1826')
18+
def 'handles regex anchors'() {
19+
expect:
20+
new RegexpMatcher('^\\w+$', null).value ==~ /\w+/
21+
}
22+
}

consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/ArrayOfPrimitivesBuilder.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher
1010
import au.com.dius.pact.core.model.matchingrules.MinTypeMatcher
1111
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
1212
import au.com.dius.pact.core.support.Json
13-
import com.mifmif.common.regex.Generex
14-
import org.json.JSONArray
13+
import au.com.dius.pact.core.support.Random
1514

1615
class ArrayOfPrimitivesBuilder {
1716

@@ -62,7 +61,7 @@ class ArrayOfPrimitivesBuilder {
6261
fun thatMatchRegex(regex: String): ArrayOfPrimitivesBuilder {
6362
this.matcher = RegexMatcher(regex)
6463
this.generator = RegexGenerator(regex)
65-
this.value = Generex(regex).random()
64+
this.value = Random.generateRandomString(regex)
6665
return this
6766
}
6867

consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/FormPostBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import au.com.dius.pact.core.model.generators.RegexGenerator
1313
import au.com.dius.pact.core.model.generators.TimeGenerator
1414
import au.com.dius.pact.core.model.generators.UuidGenerator
1515
import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory
16+
import au.com.dius.pact.core.support.Random
1617
import au.com.dius.pact.core.support.expressions.DataType
17-
import com.mifmif.common.regex.Generex
1818
import org.apache.commons.lang3.time.DateFormatUtils
1919
import org.apache.commons.lang3.time.FastDateFormat
2020
import java.net.URLEncoder
@@ -100,7 +100,7 @@ class FormPostBuilder(
100100
*/
101101
fun stringMatcher(name: String, regex: String): FormPostBuilder {
102102
generators.addGenerator(Category.BODY, name, RegexGenerator(regex))
103-
return stringMatcher(name, regex, Generex(regex).random())
103+
return stringMatcher(name, regex, Random.generateRandomString(regex))
104104
}
105105

106106
/**

consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import au.com.dius.pact.core.model.matchingrules.TypeMatcher
3131
import au.com.dius.pact.core.model.matchingrules.ValuesMatcher
3232
import au.com.dius.pact.core.model.matchingrules.expressions.MatchingRuleDefinition
3333
import au.com.dius.pact.core.support.Json.toJson
34+
import au.com.dius.pact.core.support.Random
3435
import au.com.dius.pact.core.support.expressions.DataType.Companion.from
3536
import au.com.dius.pact.core.support.json.JsonValue
3637
import au.com.dius.pact.core.support.padTo
37-
import com.mifmif.common.regex.Generex
3838
import org.apache.commons.lang3.StringUtils
3939
import org.apache.commons.lang3.time.DateFormatUtils
4040
import org.apache.commons.lang3.time.FastDateFormat
@@ -795,7 +795,7 @@ open class PactDslJsonBody : DslPart {
795795
*/
796796
fun stringMatcher(name: String, regex: String): PactDslJsonBody {
797797
generators.addGenerator(Category.BODY, matcherKey(name, rootPath), RegexGenerator(regex))
798-
stringMatcher(name, regex, *examples(Generex(regex).random()))
798+
stringMatcher(name, regex, *examples(Random.generateRandomString(regex)))
799799
return this
800800
}
801801

consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import au.com.dius.pact.core.model.matchingrules.RegexMatcher
2121
import au.com.dius.pact.core.model.matchingrules.RuleLogic
2222
import au.com.dius.pact.core.model.matchingrules.TypeMatcher
2323
import au.com.dius.pact.core.support.Json.toJson
24+
import au.com.dius.pact.core.support.Random
2425
import au.com.dius.pact.core.support.expressions.DataType.Companion.from
2526
import au.com.dius.pact.core.support.json.JsonValue
26-
import com.mifmif.common.regex.Generex
2727
import org.apache.commons.lang3.StringUtils
2828
import org.apache.commons.lang3.time.DateFormatUtils
2929
import org.apache.commons.lang3.time.FastDateFormat
@@ -612,7 +612,7 @@ open class PactDslJsonRootValue : DslPart("", "") {
612612
fun stringMatcher(regex: String): PactDslJsonRootValue {
613613
val rootValue = PactDslJsonRootValue()
614614
rootValue.generators.addGenerator(Category.BODY, "", RegexGenerator(regex))
615-
rootValue.value = Generex(regex).random()
615+
rootValue.value = Random.generateRandomString(regex)
616616
rootValue.setMatcher(rootValue.regexp(regex))
617617
return rootValue
618618
}

consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithPath.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import au.com.dius.pact.core.model.matchingrules.ContentTypeMatcher
1717
import au.com.dius.pact.core.model.matchingrules.MatchingRules
1818
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
1919
import au.com.dius.pact.core.model.queryStringToMap
20+
import au.com.dius.pact.core.support.Random
2021
import au.com.dius.pact.core.support.expressions.DataType
2122
import au.com.dius.pact.core.support.json.JsonValue
22-
import com.mifmif.common.regex.Generex
2323
import org.apache.commons.lang3.time.DateFormatUtils
2424
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
2525
import org.apache.hc.core5.http.ContentType
@@ -397,7 +397,7 @@ open class PactDslRequestWithPath : PactDslRequestBase {
397397
* @param pathRegex regular expression to use to match paths
398398
*/
399399
@JvmOverloads
400-
fun matchPath(pathRegex: String, path: String = Generex(pathRegex).random()): PactDslRequestWithPath {
400+
fun matchPath(pathRegex: String, path: String = Random.generateRandomString(pathRegex)): PactDslRequestWithPath {
401401
val re = Regex(pathRegex)
402402
if (!path.matches(re)) {
403403
throw InvalidMatcherException("Example \"$path\" does not match regular expression \"$pathRegex\"")
@@ -420,7 +420,7 @@ open class PactDslRequestWithPath : PactDslRequestBase {
420420
fun matchHeader(
421421
header: String,
422422
regex: String,
423-
headerExample: String = Generex(regex).random()
423+
headerExample: String = Random.generateRandomString(regex)
424424
): PactDslRequestWithPath {
425425
val re = Regex(regex)
426426
if (!headerExample.matches(re)) {
@@ -462,7 +462,7 @@ open class PactDslRequestWithPath : PactDslRequestBase {
462462
fun matchQuery(
463463
parameter: String,
464464
regex: String,
465-
example: String = Generex(regex).random()
465+
example: String = Random.generateRandomString(regex)
466466
): PactDslRequestWithPath {
467467
val re = Regex(regex)
468468
if (!example.matches(re)) {

consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithoutPath.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import au.com.dius.pact.core.model.generators.ProviderStateGenerator
1010
import au.com.dius.pact.core.model.matchingrules.ContentTypeMatcher
1111
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
1212
import au.com.dius.pact.core.model.queryStringToMap
13+
import au.com.dius.pact.core.support.Random
1314
import au.com.dius.pact.core.support.expressions.DataType
1415
import au.com.dius.pact.core.support.json.JsonValue
15-
import com.mifmif.common.regex.Generex
1616
import org.apache.commons.lang3.time.DateFormatUtils
1717
import org.apache.hc.core5.http.ContentType
1818
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
@@ -337,7 +337,7 @@ open class PactDslRequestWithoutPath @JvmOverloads constructor(
337337
* @param pathRegex string path regular expression to match with
338338
*/
339339
@JvmOverloads
340-
fun matchPath(pathRegex: String, path: String = Generex(pathRegex).random()): PactDslRequestWithPath {
340+
fun matchPath(pathRegex: String, path: String = Random.generateRandomString(pathRegex)): PactDslRequestWithPath {
341341
val re = Regex(pathRegex)
342342
if (!path.matches(re)) {
343343
throw InvalidMatcherException("Example \"$path\" does not match regular expression \"$pathRegex\"")
@@ -359,7 +359,7 @@ open class PactDslRequestWithoutPath @JvmOverloads constructor(
359359
@JvmOverloads
360360
inline fun matchPath(
361361
pathRegex: String,
362-
path: String = Generex(pathRegex).random(),
362+
path: String = Random.generateRandomString(pathRegex),
363363
addRequestMatchers: PactDslRequestWithPath.() -> PactDslRequestWithPath
364364
): PactDslRequestWithPath = addRequestMatchers(matchPath(pathRegex, path))
365365

0 commit comments

Comments
 (0)