Fix encoding issue

This commit is contained in:
Denis Stepanov
2015-10-07 17:14:25 +02:00
parent f5da5c4278
commit bfd72c1275
2 changed files with 46 additions and 1 deletions

View File

@@ -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) {

View File

@@ -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")
}
}