diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy index 885c53483b..6d20d5d7f1 100755 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy @@ -39,11 +39,23 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { private Map appendBody(ClientRequest clientRequest) { Object body = clientRequest?.body?.clientValue - return body != null ? [bodyPatterns: [equalTo: parseBody(body)]] : [:] + if (body == null) { + return [:] + } + if (containsRegex(body)) { + return [bodyPatterns: [matches: parseBody(body)]] + } + + return [bodyPatterns: [equalTo: parseBody(body)]] } - private String parseBody(Object responseBodyObject) { - String responseBody = responseBodyObject as String + boolean containsRegex(Object bodyObject) { + String bodyString = bodyObject as String + return (bodyString =~ /\^.*\$/).find() + } + + private String parseBody(Object bodyObject) { + String responseBody = bodyObject as String try { def json = new JsonSlurper().parseText(responseBody) return escapeJava(JsonOutput.toJson(responseBody)) 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 f849bc717d..7112e6c6e1 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 @@ -111,13 +111,12 @@ class WiremockGroovyDslSpec extends Specification { GroovyDsl groovyDsl = GroovyDsl.make { request { method('GET') - url $(client(~/\/[0-9]{2}/), server('/12')) - body("""\ - { - "name": "Jan" - } - """ - ) + url $(client(regex('/[0-9]{2}')), server('/12')) + body """ + { + "name": "Jan" + } + """ } response { status 200 @@ -157,6 +156,58 @@ class WiremockGroovyDslSpec extends Specification { ''') } + + def 'should convert groovy dsl stub with regexp Body as String to wiremock stub for the client side'() { + given: + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method('GET') + url $(client(regex('/[0-9]{2}')), server('/12')) + body """ + { + "personalId": "${value(client(regex('^[0-9]{11}$')), server('57593728525'))}" + } + """ + } + response { + status 200 + body("""\ + { + "name": "Jan" + } + """ + ) + headers { + header 'Content-Type': 'text/plain' + } + } + } + when: + String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' +{ + "request": { + "method": "GET", + "urlPattern": "/[0-9]{2}", + "bodyPatterns": { + "matches":"{\\"personalId\\":\\"^[0-9]{11}$\\"}" + } + }, + "response": { + "status": 200, + "body": { + "name": "Jan" + }, + "headers": { + "Content-Type": "text/plain" + } + } +} +''') + } + + def "should generate stub with GET"() { given: GroovyDsl groovyDsl = GroovyDsl.make {