Fixed wrong $ escaping (#278)

Fixed wrong $ escaping

fixes #273
This commit is contained in:
Marcin Grzejszczak
2016-05-17 11:04:58 +02:00
parent 7d2f01aad2
commit 4f74c0b26e
5 changed files with 78 additions and 9 deletions

View File

@@ -121,7 +121,9 @@ abstract class MethodBodyBuilder {
appendJsonPath(bb, getResponseAsString())
JsonPaths jsonPaths = JsonToJsonPathsConverter.transformToJsonPathWithTestsSideValues(responseBody)
jsonPaths.each {
bb.addLine("assertThatJson(parsedJson)" + it.method())
String method = it.method()
String postProcessedMethod = postProcessJsonPathCall(method)
bb.addLine("assertThatJson(parsedJson)" + postProcessedMethod)
addColonIfRequired(bb)
}
processBodyElement(bb, "", responseBody)
@@ -136,6 +138,10 @@ abstract class MethodBodyBuilder {
}
}
protected String postProcessJsonPathCall(String jsonPath) {
return jsonPath
}
protected void appendJsonPath(BlockBuilder blockBuilder, String json) {
blockBuilder.addLine(("DocumentContext parsedJson = JsonPath.parse($json)"))
addColonIfRequired(blockBuilder)

View File

@@ -48,4 +48,9 @@ class MockMvcSpockMethodRequestProcessingBodyBuilder extends SpockMethodRequestP
blockBuilder.addLine("response.header('$property') ${convertHeaderComparison(value)}")
}
}
// #273 - should escape $ for Groovy since it will try to make it a GString
@Override
protected String postProcessJsonPathCall(String jsonPath) {
return jsonPath.replace('$', '\\$')
}
}

View File

@@ -159,4 +159,10 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
return "==~ java.util.regex.Pattern.compile('$headerValue')"
}
// #273 - should escape $ for Groovy since it will try to make it a GString
@Override
protected String postProcessJsonPathCall(String method) {
return method.replace('$', '\\$')
}
}

View File

@@ -32,7 +32,7 @@ class DelegatingJsonVerifiable implements MethodBufferingJsonVerifiable {
private static String wrapValueWithQuotes(Object value) {
return value instanceof String ?
"\"" + stringWithEscapedQuotes(value).replaceAll("\\$", "\\\\\\$") + "\"" :
"\"" + stringWithEscapedQuotes((String) value) + "\"" :
value.toString();
}

View File

@@ -1101,7 +1101,7 @@ World.'''"""
}
@Issue('#216')
def "should parse JSON with arrays using #methodBuilderName"() {
def "should parse JSON with arrays using Spock"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
@@ -1125,17 +1125,47 @@ World.'''"""
)
}
}
MethodBodyBuilder builder = methodBuilder(contractDsl)
MethodBodyBuilder builder = new MockMvcSpockMethodRequestProcessingBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('''assertThatJson(parsedJson).array("authorities").arrayField().matches("^[a-zA-Z0-9_\\\\- ]+\\$").value()''')
where:
methodBuilderName | methodBuilder
"MockMvcSpockMethodBuilder" | { GroovyDsl dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl) }
"MockMvcJUnitMethodBuilder" | { GroovyDsl dsl -> new MockMvcJUnitMethodBodyBuilder(dsl) }
}
@Issue('#216')
def "should parse JSON with arrays using JUnit"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "GET"
urlPath('/auth/oauth/check_token') {
queryParameters {
parameter 'token':
value(
client(regex('^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}')),
server('6973b31d-7140-402a-bca6-1cdb954e03a7')
)
}
}
}
response {
status 200
body(
authorities: [
value(stub('ROLE_ADMIN'), test(regex('^[a-zA-Z0-9_\\- ]+$')))
]
)
}
}
MethodBodyBuilder builder = new MockMvcJUnitMethodBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('''assertThatJson(parsedJson).array("authorities").arrayField().matches("^[a-zA-Z0-9_\\\\- ]+$").value()''')
}
def "should work with execution property"() {
@@ -1324,6 +1354,28 @@ World.'''"""
"MockMvcJUnitMethodBuilder" | { GroovyDsl dsl -> new MockMvcJUnitMethodBodyBuilder(dsl) }
}
@Issue('#273')
def "should not escape dollar in Spock regex tests"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method 'GET'
urlPath '/get'
}
response {
status 200
body( code: 9, message: $(client('Wrong credentials'), server(regex('^(?!\\s*$).+'))) )
}
}
MethodBodyBuilder builder = new MockMvcSpockMethodRequestProcessingBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.then(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('assertThatJson(parsedJson).field("message").matches("^(?!\\\\s*\\$).+")')
}
GroovyDsl dslForDocs =
// tag::dsl_example[]