Implement selecting compare method by content type
This commit is contained in:
@@ -2,7 +2,6 @@ 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
|
||||
@@ -80,32 +79,35 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy {
|
||||
if (body == null) {
|
||||
return [:]
|
||||
}
|
||||
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() })
|
||||
}
|
||||
))]]]
|
||||
if (clientRequest.body?.containsPattern) {
|
||||
return [bodyPatterns: [[matches: escapeBodyForJson(body)]]]
|
||||
}
|
||||
|
||||
return [bodyPatterns: [[(getCompareType()): parseBody(body)]]]
|
||||
return [bodyPatterns: [[(getMatchType()): parseBody(body)]]]
|
||||
}
|
||||
|
||||
private String getCompareType() {
|
||||
Header contentType = request.headers?.entries.find { it.name == "Content-Type" }
|
||||
if (contentType && contentType.clientValue.toString().endsWith("json")) {
|
||||
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() })
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
private String getMatchType() {
|
||||
String content = request.headers?.entries.find { it.name == "Content-Type" } ?.clientValue?.toString()
|
||||
if (content?.endsWith("json")) {
|
||||
return "equalToJson"
|
||||
}
|
||||
if (contentType && contentType.clientValue.toString().endsWith("xml")) {
|
||||
if (content?.endsWith("xml")) {
|
||||
return "equalToXml"
|
||||
}
|
||||
return "equalTo"
|
||||
@@ -115,13 +117,4 @@ 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 }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ 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<String, DslProperty> body) {
|
||||
super(extractValue(body, {it.clientValue}), extractValue(body, {it.serverValue}))
|
||||
}
|
||||
@@ -39,12 +41,18 @@ 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.
|
||||
|
||||
Reference in New Issue
Block a user