From bf43ca3c7429b7ed652e9557bc4964037422bad3 Mon Sep 17 00:00:00 2001 From: Mariusz Smykula Date: Fri, 2 Oct 2015 17:07:00 +0200 Subject: [PATCH] Ugly fix for escaping bug #143 #126 --- .../accurest/util/ContentUtils.groovy | 4 +-- .../MockMvcSpockMethodBuilderSpec.groovy | 36 ++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy index a7e66f9ec3..8aae7a832f 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy @@ -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] diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy index e3e4120e0d..9267dfb559 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy @@ -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()) }