From bfd72c1275d996d7dacf4e9736438a5b272ef0fb Mon Sep 17 00:00:00 2001 From: Denis Stepanov Date: Wed, 7 Oct 2015 17:14:25 +0200 Subject: [PATCH] Fix encoding issue --- .../builder/SpockMethodBodyBuilder.groovy | 10 ++++- .../MockMvcSpockMethodBuilderSpec.groovy | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy index 2789e52a2d..618398ad5b 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy @@ -1,5 +1,7 @@ package io.codearte.accurest.builder + import groovy.json.JsonOutput +import groovy.json.StringEscapeUtils import groovy.transform.PackageScope import groovy.transform.TypeChecked import io.codearte.accurest.dsl.GroovyDsl @@ -117,7 +119,13 @@ abstract class SpockMethodBodyBuilder { protected String getBodyAsString() { Object bodyValue = extractServerValueFromBody(request.body.serverValue) - return trimRepeatedQuotes(new JsonOutput().toJson(bodyValue)) + String json = new JsonOutput().toJson(bodyValue) + json = convertUnicodeEscapes(json) + return trimRepeatedQuotes(json) + } + + protected String convertUnicodeEscapes(String json) { + return StringEscapeUtils.unescapeJavaScript(json) } protected String trimRepeatedQuotes(String toTrim) { 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 44cb4e235f..fce1bdff84 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 @@ -7,6 +7,8 @@ import spock.lang.Issue import spock.lang.Specification import spock.lang.Unroll +import java.util.regex.Pattern + /** * @author Jakub Kubrynski */ @@ -669,4 +671,39 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu then: spockTest.contains('''assertThatRejectionReasonIsNull(parsedJson.read('$.rejectionReason'))''') } + + def "shouldn't generate unicode escape characters"() { + given: + Pattern ONLY_ALPHA_UNICODE = Pattern.compile(/[\p{L}]*/) + + GroovyDsl contractDsl = GroovyDsl.make { + request { + method "PUT" + url "/v1/payments/e86df6f693de4b35ae648464c5b0dc09/енев" + headers { + header('Content-Type': 'application/json') + } + body( + client: [ + first_name: $(stub(ONLY_ALPHA_UNICODE), test('Пенева')), + last_name : $(stub(ONLY_ALPHA_UNICODE), test('Пенева')) + ] + ) + } + response { + status 200 + headers { + header('Content-Type': 'application/json') + } + } + } + MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl) + BlockBuilder blockBuilder = new BlockBuilder(" ") + when: + builder.appendTo(blockBuilder) + def spockTest = blockBuilder.toString() + then: + !spockTest.contains("\\u041f") + } + }