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 31eb05300a..3390b6456b 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 @@ -2,6 +2,7 @@ package io.codearte.accurest.dsl import groovy.transform.PackageScope import groovy.transform.TypeChecked import io.codearte.accurest.dsl.internal.ClientRequest +import io.codearte.accurest.dsl.internal.Header import io.codearte.accurest.dsl.internal.MatchingStrategy import io.codearte.accurest.dsl.internal.QueryParameter import io.codearte.accurest.dsl.internal.QueryParameters @@ -79,35 +80,32 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { if (body == null) { return [:] } - if (clientRequest.body?.containsPattern) { - return [bodyPatterns: [[matches: escapeBodyForJson(body)]]] - } - return [bodyPatterns: [[(getMatchType()): parseBody(body)]]] - } - - 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 + if (containsRegex(body)) { + return [bodyPatterns: [[matches: 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() }) } - } .join('') - }, - { LinkedList list, String json -> - return json.replaceAll(TEMPORARY_PATTERN_HOLDER, { String a, String[] b -> list.pop() }) - } - )) + ))]]] + } + + return [bodyPatterns: [[(getCompareType()): parseBody(body)]]] } - private String getMatchType() { - String content = request.headers?.entries.find { it.name == "Content-Type" } ?.clientValue?.toString() - if (content?.endsWith("json")) { + private String getCompareType() { + Header contentType = request.headers?.entries.find { it.name == "Content-Type" } + if (contentType && contentType.clientValue.toString().endsWith("json")) { return "equalToJson" } - if (content?.endsWith("xml")) { + if (contentType && contentType.clientValue.toString().endsWith("xml")) { return "equalToXml" } return "equalTo" @@ -117,4 +115,13 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { return body } + boolean containsRegex(Object bodyObject) { + String bodyString = bodyObject as String + return (bodyString =~ /\^.*\$/).find() + } + + boolean containsRegex(Map map) { + return map.values().any { it instanceof Pattern } + } + } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Body.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Body.groovy index 78813a74a8..c68380f0f2 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Body.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Body.groovy @@ -19,8 +19,6 @@ class Body extends DslProperty { private static final Pattern TEMPORARY_PATTERN_HOLDER = Pattern.compile('REGEXP>>(.*)<<') private static final String JSON_VALUE_PATTERN_FOR_REGEX = 'REGEXP>>%s<<' - boolean containsPattern - Body(Map body) { super(extractValue(body, {it.clientValue}), extractValue(body, {it.serverValue})) } @@ -41,18 +39,12 @@ class Body extends DslProperty { Body(GString bodyAsValue) { super(extractValue(bodyAsValue, {it.clientValue}), extractValue(bodyAsValue, {it.serverValue})) - containsPattern = containsPattern(bodyAsValue) } Body(DslProperty bodyAsValue) { super(bodyAsValue.clientValue, bodyAsValue.serverValue) } - private boolean containsPattern(GString bodyAsValue) { - return bodyAsValue.values.collect { it instanceof DslProperty ? it.clientValue : it } - .find { it instanceof Pattern } - } - /** * Due to the fact that we allow users to have a body with GString and different values inside * we need to be prepared that they pass regexps around both on client and server side.