diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy index f9bc9bdc23..ea0b6b4a9f 100755 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy @@ -131,13 +131,17 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { private List extractReqexpMatching(Object responseBodyObject) { def matchingStrategies = new ArrayList() - responseBodyObject.each { k, v -> - if (v instanceof List) { - v.each { - matchingStrategies.addAll(extractReqexpMatching((Map)it)) + if (responseBodyObject instanceof GString) { + return [new MatchingStrategy(responseBodyObject, MatchingStrategy.Type.MATCHING)] + } else if (responseBodyObject instanceof Map) { + responseBodyObject.each { k, v -> + if (v instanceof List) { + v.each { + matchingStrategies.addAll(extractReqexpMatching((Map)it)) + } + } else { + matchingStrategies.add(new MatchingStrategy(/.*${k}":.?"?${v}"?.*/, MatchingStrategy.Type.MATCHING)) } - } else { - matchingStrategies.add(new MatchingStrategy(/.*${k}":.?"?${v}"?.*/, MatchingStrategy.Type.MATCHING)) } } return matchingStrategies diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy index c066f4a9cc..80540ec70d 100755 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy @@ -101,6 +101,55 @@ class WiremockGroovyDslSpec extends WiremockSpec { stubMappingIsValidWiremockStub(wiremockStub) } + @Issue("#86") + def 'should convert groovy dsl stub with GString and regexp'() { + given: + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method('POST') + url('/ws/payments') + headers { + header("Content-Type": 'application/x-www-form-urlencoded') + } + body("""paymentType=INCOMING&transferType=BANK&amount=${value(client(regex('[0-9]{3}\\.[0-9]{2}')), server(500.00))}&bookingDate=${value(client(regex('[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])')), server('2015-05-18'))}""") + } + response { + status 204 + body( + paymentId: value(client('4'), server(regex('[1-9][0-9]*'))), + foundExistingPayment: false + ) + } + } + when: + String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' +{ + "request": { + "method": "POST", + "headers": { + "Content-Type": { + "equalTo": "application/x-www-form-urlencoded" + } + }, + "url": "/ws/payments", + "bodyPatterns": [ + { + "matches": "paymentType=INCOMING&transferType=BANK&amount=[0-9]{3}\\\\.[0-9]{2}&bookingDate=[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])" + } + ] + }, + "response": { + "status": 204, + "body": "{\\"paymentId\\":\\"4\\",\\"foundExistingPayment\\":false}" + } +} +''') + and: + stubMappingIsValidWiremockStub(wiremockStub) + } + def 'should convert groovy dsl stub with Body as String to wiremock stub for the client side'() { given: GroovyDsl groovyDsl = GroovyDsl.make {