Ugly fix for escaping bug #143 #126

This commit is contained in:
Mariusz Smykula
2015-10-02 17:07:00 +02:00
parent ac3b87bd06
commit bf43ca3c74
2 changed files with 33 additions and 7 deletions

View File

@@ -131,7 +131,7 @@ class ContentUtils {
bodyAsValue.values.collect { transformJSONStringValue(it, valueProvider) } as String[],
bodyAsValue.strings.clone() as String[]
)
def parsedJson = new JsonSlurper().parseText(transformedString.toString())
def parsedJson = new JsonSlurper().parseText(transformedString.toString().replace('\\', '\\\\'))
return convertAllTemporaryRegexPlaceholdersBackToPatterns(parsedJson)
}
@@ -166,7 +166,7 @@ class ContentUtils {
MapConverter.transformValues(parsedJson, { Object value ->
if (value instanceof String) {
String string = (String) value
Matcher matcher = TEMPORARY_PATTERN_HOLDER.matcher(string)
Matcher matcher = TEMPORARY_PATTERN_HOLDER.matcher(string.trim())
if (matcher.matches()) {
List val = matcher[0] as List
String pattern = val[1]

View File

@@ -238,6 +238,35 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
}
def "should generate regex assertions for string objects in response body"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "GET"
url "test"
}
response {
status 200
body("""{"property1":"a","property2":"${value(client('123'), server(regex('[0-9]{3}')))}"}""")
headers {
header('Content-Type': 'application/json')
}
}
}
MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
then:
blockBuilder.toString().contains("\$[?(@.property2 =~ /[0-9]{3}/)]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
}
@Issue(["#126", "#143"])
def "should generate escaped regex assertions for string objects in response body"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
@@ -246,12 +275,10 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
}
response {
status 200
body("""{"property1":"a","property2":"${value(client('123'), server(regex('[0-9]{3}')))}"}""")
body("""{"property":" ${value(client('123'), server(regex('\\d+')))}"}""")
headers {
header('Content-Type': 'application/json')
}
}
}
MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl)
@@ -259,8 +286,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
when:
builder.appendTo(blockBuilder)
then:
blockBuilder.toString().contains("\$[?(@.property2 =~ /[0-9]{3}/)]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
blockBuilder.toString().contains("\$[?(@.property =~ /\\d+/)]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
}