Escaping new lines for json; fixes gh-802
This commit is contained in:
@@ -159,7 +159,8 @@ abstract class JUnitMethodBodyBuilder extends RequestProcessingMethodBodyBuilder
|
||||
if (body instanceof ExecutionProperty) {
|
||||
value = body.toString()
|
||||
} else {
|
||||
value = "\"$body\""
|
||||
String escaped = escapeRequestSpecialChars(body.toString())
|
||||
value = "\"$escaped\""
|
||||
}
|
||||
return ".body($value)"
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.builder
|
||||
|
||||
import org.springframework.cloud.contract.spec.internal.Cookie
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import com.jayway.jsonpath.DocumentContext
|
||||
@@ -33,6 +31,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.spec.ContractTemplate
|
||||
import org.springframework.cloud.contract.spec.internal.BodyMatcher
|
||||
import org.springframework.cloud.contract.spec.internal.BodyMatchers
|
||||
import org.springframework.cloud.contract.spec.internal.Cookie
|
||||
import org.springframework.cloud.contract.spec.internal.DslProperty
|
||||
import org.springframework.cloud.contract.spec.internal.ExecutionProperty
|
||||
import org.springframework.cloud.contract.spec.internal.Header
|
||||
@@ -92,6 +91,10 @@ abstract class MethodBodyBuilder {
|
||||
return new HandlebarsTemplateProcessor()
|
||||
}
|
||||
|
||||
protected ContentType getRequestContentType() {
|
||||
return ContentType.UNKNOWN
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the response body validation code block
|
||||
*/
|
||||
|
||||
@@ -63,6 +63,14 @@ abstract class RequestProcessingMethodBodyBuilder extends MethodBodyBuilder {
|
||||
this.response = stubDefinition.response
|
||||
}
|
||||
|
||||
protected String escapeRequestSpecialChars(String string) {
|
||||
if (getRequestContentType() == ContentType.JSON) {
|
||||
return string
|
||||
.replaceAll('\\\\n', '\\\\\\\\n')
|
||||
}
|
||||
return string
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns code used to retrieve a response for the given {@link Request}
|
||||
*/
|
||||
|
||||
@@ -134,7 +134,8 @@ abstract class SpockMethodRequestProcessingBodyBuilder extends RequestProcessing
|
||||
if (body instanceof ExecutionProperty) {
|
||||
value = body.toString()
|
||||
} else {
|
||||
value = "'''$body'''"
|
||||
String escaped = escapeRequestSpecialChars(body.toString())
|
||||
value = "'''$escaped'''"
|
||||
}
|
||||
return ".body($value)"
|
||||
}
|
||||
|
||||
@@ -16,17 +16,18 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.builder
|
||||
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
|
||||
import org.junit.Rule
|
||||
import spock.lang.Issue
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
import org.springframework.boot.test.rule.OutputCapture
|
||||
import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
|
||||
import org.springframework.cloud.contract.verifier.dsl.WireMockStubVerifier
|
||||
import org.springframework.cloud.contract.verifier.util.SyntaxChecker
|
||||
import spock.lang.Issue
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
|
||||
class MethodBodyBuilderSpec extends Specification implements WireMockStubVerifier {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user