Fixes #230 Cannot match url with query parameters when both contain regular expressions (#231)

This commit is contained in:
Mariusz Smykuła
2016-04-15 19:48:55 +02:00
parent df15d9f4c4
commit 69a9584ba2
3 changed files with 49 additions and 2 deletions

View File

@@ -24,12 +24,14 @@ class WireMockToDslConverter {
def response = wireMockStub.response
def bodyPatterns = request.bodyPatterns
String urlPattern = request.urlPattern
String urlPathPattern = request.urlPathPattern
return """\
${priority ? "priority ${priority}" : ''}
request {
${request.method ? "method \"\"\"$request.method\"\"\"" : ""}
${request.url ? "url \"\"\"$request.url\"\"\"" : ""}
${urlPattern ? "url \$(client(regex('${escapeJava(urlPattern)}')), server('${new Xeger(escapeJava(urlPattern)).generate()}'))" : ""}
${urlPathPattern ? "urlPath \$(client(regex('${escapeJava(urlPattern)}')), server('${new Xeger(escapeJava(urlPattern)).generate()}'))" : ""}
${request.urlPath ? "url \"\"\"$request.urlPath\"\"\"" : ""}
${
request.headers ? """headers {

View File

@@ -108,13 +108,17 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
private void appendUrl(RequestPattern requestPattern) {
Object urlPath = request?.urlPath?.clientValue
if (urlPath) {
requestPattern.setUrlPath(getStubSideValue(urlPath.toString()).toString())
if(urlPath instanceof Pattern) {
requestPattern.setUrlPathPattern(getStubSideValue(urlPath.toString()).toString())
} else {
requestPattern.setUrlPath(getStubSideValue(urlPath.toString()).toString())
}
}
if(!request.url) {
return
}
Object url = getUrlIfGstring(request?.url?.clientValue)
if(url instanceof Pattern) {
if (url instanceof Pattern) {
requestPattern.setUrlPattern(url.pattern())
} else {
requestPattern.setUrl(url.toString())

View File

@@ -713,6 +713,47 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
stubMappingIsValidWireMockStub(json)
}
@Issue('#230')
def "should generate request with urlPathPattern and queryParameters for client side\
when both contains regular expressions"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
request {
method 'GET'
urlPath($(client(regex("/users/[0-9]+")), server("/users/1"))) {
queryParameters {
parameter 'search': $(client(notMatching(~/^\/[0-9]{2}$/)), server("10"))
}
}
}
response {
status 200
}
}
when:
def json = toWireMockClientJsonStub(groovyDsl)
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
"request": {
"method": "GET",
"urlPathPattern": "/users/[0-9]+",
"queryParameters": {
"search": {
"doesNotMatch": "^/[0-9]{2}$"
}
}
},
"response": {
"status": 200,
}
}
'''), json)
and:
stubMappingIsValidWireMockStub(json)
}
def "should generate request with urlPath for client side"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {