Generated messaging tests could create compilation errors for certain regexes in headers (#622)

* Fixed an issue with the generation of messaging tests where the regex pattern was creating compilation errors

fixes gh-620

(cherry picked from commit f64feaf)
This commit is contained in:
Tim Ysewyn
2018-04-18 23:17:59 +02:00
committed by Marcin Grzejszczak
parent d467341427
commit 5dbac1f210
3 changed files with 64 additions and 4 deletions

View File

@@ -41,6 +41,7 @@ import static org.springframework.cloud.contract.verifier.config.TestFramework.J
*
* @author Marcin Grzejszczak
* @author Jakub Kubrynski, codearte.io
* @author Tim Ysewyn
*
* @since 1.0.0
*/
@@ -244,8 +245,7 @@ class JUnitMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
}
protected String createHeaderComparison(Pattern headerValue) {
String escapedHeader = convertUnicodeEscapesIfRequired("$headerValue")
String escapedJavaHeader = escapeJava(escapedHeader)
String escapedJavaHeader = escapeJava(headerValue.pattern())
return "matches(\"$escapedJavaHeader\");"
}

View File

@@ -35,6 +35,7 @@ import static org.apache.commons.text.StringEscapeUtils.escapeJava
/**
* @author Jakub Kubrynski, codearte.io
* @author Tim Ysewyn
*/
@PackageScope
@TypeChecked
@@ -229,8 +230,8 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
}
protected String convertHeaderComparison(Pattern headerValue) {
String converted = escapeJava(convertUnicodeEscapesIfRequired(headerValue.toString()))
return "==~ java.util.regex.Pattern.compile('$converted')"
String converted = escapeJava(convertUnicodeEscapesIfRequired(headerValue.pattern()))
return "==~ java.util.regex.Pattern.compile('${converted}')"
}
// #273 - should escape $ for Groovy since it will try to make it a GString

View File

@@ -794,4 +794,63 @@ Contract.make {
stripped(test) == stripped(expectedMsg)
}
@Issue('#620')
def "should generate tests with message headers containing regular expression which compile for [#methodBuilderName]"() {
given:
Contract contractDsl = Contract.make {
label 'shouldPublishMessage'
// input to the contract
input {
// the contract will be triggered by a method
triggeredBy('foo()')
}
// output message of the contract
outputMessage {
// destination to which the output message will be sent
sentTo('messageExchange')
// the body of the output message
body([
"field": "value"
])
headers {
header('Authorization', value(regex('Bearer [A-Za-z0-9\\-\\._~\\+\\/]+=*')))
}
}
}
MethodBodyBuilder builder = methodBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
!test.contains('cursor')
!test.contains('REGEXP>>')
test == expectedTest
where:
methodBuilderName | methodBuilder | expectedTest
"SpockMessagingMethodBodyBuilder" | { Contract dsl -> new SpockMessagingMethodBodyBuilder(dsl, properties) } | ''' when:
foo()
then:
ContractVerifierMessage response = contractVerifierMessaging.receive('messageExchange')
assert response != null
response.getHeader('Authorization')?.toString() ==~ java.util.regex.Pattern.compile('Bearer [A-Za-z0-9-._~+/]+=*')
and:
DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.payload))
assertThatJson(parsedJson).field("['field']").isEqualTo("value")
'''
"JUnitMessagingMethodBodyBuilder" | { Contract dsl -> new JUnitMessagingMethodBodyBuilder(dsl, properties) } | ''' // when:
foo();
// then:
ContractVerifierMessage response = contractVerifierMessaging.receive("messageExchange");
assertThat(response).isNotNull();
assertThat(response.getHeader("Authorization")).isNotNull();
assertThat(response.getHeader("Authorization").toString()).matches("Bearer [A-Za-z0-9\\\\-\\\\._~\\\\+\\\\/]+=*");
// and:
DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.getPayload()));
assertThatJson(parsedJson).field("['field']").isEqualTo("value");
'''
}
}