From 4f74c0b26efcb3a992c20b47072c2f9d21fcc5a3 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 17 May 2016 11:04:58 +0200 Subject: [PATCH] Fixed wrong $ escaping (#278) Fixed wrong $ escaping fixes #273 --- .../accurest/builder/MethodBodyBuilder.groovy | 8 ++- ...kMethodRequestProcessingBodyBuilder.groovy | 7 +- .../SpockMessagingMethodBodyBuilder.groovy | 6 ++ .../util/DelegatingJsonVerifiable.java | 2 +- .../MockMvcMethodBodyBuilderSpec.groovy | 64 +++++++++++++++++-- 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBodyBuilder.groovy index 8ca6aade46..044394a1aa 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBodyBuilder.groovy @@ -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) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodRequestProcessingBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodRequestProcessingBodyBuilder.groovy index bf3cdeb509..5d983c3e6c 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodRequestProcessingBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodRequestProcessingBodyBuilder.groovy @@ -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('$', '\\$') + } +} \ No newline at end of file diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMessagingMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMessagingMethodBodyBuilder.groovy index e9bbb2e1db..0c9a0b5f83 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMessagingMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMessagingMethodBodyBuilder.groovy @@ -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('$', '\\$') + } + } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/DelegatingJsonVerifiable.java b/accurest-core/src/main/groovy/io/codearte/accurest/util/DelegatingJsonVerifiable.java index cf96e2822d..bd41fa1c78 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/util/DelegatingJsonVerifiable.java +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/DelegatingJsonVerifiable.java @@ -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(); } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy index 533eaf7a9a..f42b7ef95a 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy @@ -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[]