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 3c726f66d2..31eb05300a 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 @@ -80,24 +80,26 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { return [:] } if (clientRequest.body?.containsPattern) { - return [bodyPatterns: parseMatchesBody(body)] + return [bodyPatterns: [[matches: escapeBodyForJson(body)]]] } return [bodyPatterns: [[(getMatchType()): parseBody(body)]]] } - private Object parseMatchesBody(def responseBodyObject) { - def regexList = new ArrayList<>() - responseBodyObject.each { k, v -> - if (v instanceof List) { - v.each { - regexList.addAll(parseMatchesBody((Map)it)) + private Object escapeBodyForJson(Object body) { + return parseBody(convertJsonStructureToObjectUnderstandingStructure(body, + { it instanceof Pattern }, + { String json -> json.collect { + switch(it) { + case ('{'): return '\\{' + case ('}'): return '\\}' + default: return it + } + } .join('') + }, + { LinkedList list, String json -> + return json.replaceAll(TEMPORARY_PATTERN_HOLDER, { String a, String[] b -> list.pop() }) } - } else { - String regex = ".*${k}\":.?\"${v}\".*" - regexList.add([matches: regex]); - } - } - return regexList + )) } private String getMatchType() { 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 452cc758ed..4674441a09 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 @@ -161,7 +161,7 @@ class WiremockGroovyDslSpec extends WiremockSpec { } body """ { - "name": "${value(client('Jan'), server('Honza'))}" + "name": "Jan" } """ } @@ -270,20 +270,22 @@ class WiremockGroovyDslSpec extends WiremockSpec { 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" - } - } + "request": { + "method": "GET", + "urlPattern": "/[0-9]{2}", + "bodyPatterns": [ + { + "matches":"\\\\{\\"personalId\\":\\"^[0-9]{11}$\\"\\\\}" + } + ] + }, + "response": { + "status": 200, + "body": "{\\"name\\":\\"Jan\\"}", + "headers": { + "Content-Type": "text/plain" + } + } } ''') and: @@ -334,8 +336,9 @@ class WiremockGroovyDslSpec extends WiremockSpec { }, "url": "/fraudcheck", "bodyPatterns": [ - {"matches": ".*clientPesel\\":.?\\"[0-9]{10}\\".*"}, - {"matches": ".*loanAmount\\":.?\\"123.123\\".*"} + { + "matches": "\\\\{\\"clientPesel\\":\\"[0-9]{10}\\",\\"loanAmount\\":123.123\\\\}" + } ] }, "response": { @@ -655,78 +658,10 @@ class WiremockGroovyDslSpec extends WiremockSpec { }, "X-Custom-Header": { "matches": "^.*2134.*$" - } - } - } - ''') - } - - def 'should convert groovy dsl stub with rich tree Body as String to wiremock stub for the client side'() { - given: - GroovyDsl groovyDsl = GroovyDsl.make { - request { - method('GET') - url $(client(~/\/[0-9]{2}/), server('/12')) - body """\ - { - "personalId": "${value(client(regex('[0-9]{11}')), server('57593728525'))}", - "firstName": "${value(client(regex('.*')), server('Bruce'))}", - "lastName": "${value(client(regex('.*')), server('Lee'))}", - "birthDate": "${value(client(regex('[0-9]{4}-[0-9]{2}-[0-9]{2}')), server('1985-12-12'))}", - "errors": [ - { - "propertyName": "${value(client(regex('[0-9]{2}')), server('04'))}", - "providerValue": "Test" - }, - { - "propertyName": "${value(client(regex('[0-9]{2}')), server('08'))}", - "providerValue": "Test" - } - ] - } - """ - } - 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": ".*birthDate\\":.?\\"[0-9]{4}-[0-9]{2}-[0-9]{2}\\".*"}, - {"matches": ".*propertyName\\":.?\\"[0-9]{2}\\".*"}, - {"matches": ".*providerValue\\":.?\\"Test\\".*"}, - {"matches": ".*propertyName\\":.?\\"[0-9]{2}\\".*"}, - {"matches": ".*providerValue\\":.?\\"Test\\".*"}, - {"matches": ".*firstName\\":.?\\".*\\".*"}, - {"matches": ".*lastName\\":.?\\".*\\".*"}, - {"matches": ".*personalId\\":.?\\"[0-9]{11}\\".*"} - ] - }, - "response": { - "status": 200, - "body": "{\\"name\\":\\"Jan\\"}", - "headers": { - "Content-Type": "text/plain" - } - } - } - ''') + } + } + } + ''') } String toJsonString(value) {