Merge branch '2.1.x'

This commit is contained in:
Marcin Grzejszczak
2019-11-04 09:25:01 +01:00
3 changed files with 39 additions and 7 deletions

View File

@@ -121,14 +121,15 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
}
boolean bodyHasMatchingStrategy = request.body.clientValue instanceof MatchingStrategy
MatchingStrategy matchingStrategy = getMatchingStrategyFromBody(request.body)
Object clientSideBody = MapConverter.transformToClientValues(request.body)
if (contentType == ContentType.JSON) {
def originalBody = matchingStrategy?.clientValue
if (bodyHasMatchingStrategy) {
requestPattern.withRequestBody(
convertToValuePattern(matchingStrategy))
} else if (containsPattern(request?.body)) {
} else if (clientSideBody instanceof Pattern || clientSideBody instanceof RegexProperty) {
requestPattern.withRequestBody(
convertToValuePattern(appendBodyRegexpMatchPattern(request.body)))
convertToValuePattern(appendBodyRegexpMatchPattern(request.body, contentType)))
}
else {
def body = JsonToJsonPathsConverter.
@@ -437,13 +438,14 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
}
private MatchingStrategy appendBodyRegexpMatchPattern(Object value, ContentType contentType) {
Object clientValue = MapConverter.transformToClientValues(value)
switch (contentType) {
case ContentType.JSON:
return new MatchingStrategy(
buildJSONRegexpMatch(value), MatchingStrategy.Type.MATCHING)
buildJSONRegexpMatch(clientValue), MatchingStrategy.Type.MATCHING)
case ContentType.UNKNOWN:
return new MatchingStrategy(
buildGStringRegexpForStubSide(value), MatchingStrategy.Type.MATCHING)
buildGStringRegexpForStubSide(clientValue), MatchingStrategy.Type.MATCHING)
case ContentType.XML:
throw new IllegalStateException("XML pattern matching is not implemented yet")
}

View File

@@ -160,7 +160,11 @@ class JsonToJsonPathsConverter {
removeTrailingContainers(pathToDelete, context)
}
else {
String lastParent = matcherPath.substring(0, matcherPath.lastIndexOf("."))
int lastIndexOfDot = matcherPath.lastIndexOf(".")
if (lastIndexOfDot == -1) {
return false
}
String lastParent = matcherPath.substring(0, lastIndexOfDot)
def lastParentObject = context.read(lastParent)
if (isIterable(lastParentObject)
&&
@@ -235,9 +239,9 @@ class JsonToJsonPathsConverter {
return path
}
int lastIndexOfDot = lastIndexOfDot(path)
String toLastDot = path.substring(0, lastIndexOfDot)
String fromLastDot = path.substring(lastIndexOfDot + 1)
String propertyName = "@.${fromLastDot}"
String toLastDot = lastIndexOfDot == -1 ? '$' : path.substring(0, lastIndexOfDot)
String propertyName = lastIndexOfDot == -1 ? '@' : "@.${fromLastDot}"
String comparison = createComparison(propertyName, bodyMatcher, value, body)
return "${toLastDot}[?(${comparison})]"
}

View File

@@ -2818,6 +2818,32 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
@Issue("#894")
def "should not fail when matchers don't have dots"() {
given:
Contract contractDsl = Contract.make {
request {
method 'POST'
url "/example"
body([ "123", "234"])
bodyMatchers {
jsonPath('$[*]', byRegex(nonEmpty()))
}
}
response {
status 201
}
}
when:
String wireMockStub = new WireMockStubStrategy("Test",
new ContractMetadata(null, false, 0, null, contractDsl), contractDsl)
.toWireMockClientStub()
then:
stubMappingIsValidWireMockStub(wireMockStub)
}
WireMockConfiguration config() {
return new WireMockConfiguration().extensions(responseTemplateTransformer())
}