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
This commit is contained in:
committed by
Marcin Grzejszczak
parent
25b6041036
commit
f64feaf84d
@@ -41,6 +41,7 @@ import static org.springframework.cloud.contract.verifier.config.TestFramework.J
|
|||||||
*
|
*
|
||||||
* @author Marcin Grzejszczak
|
* @author Marcin Grzejszczak
|
||||||
* @author Jakub Kubrynski, codearte.io
|
* @author Jakub Kubrynski, codearte.io
|
||||||
|
* @author Tim Ysewyn
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@@ -244,11 +245,7 @@ class JUnitMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String createHeaderComparison(Pattern headerValue) {
|
protected String createHeaderComparison(Pattern headerValue) {
|
||||||
String escapedJavaHeader = escapeJava(headerValue.toString())
|
String escapedJavaHeader = escapeJava(headerValue.pattern())
|
||||||
return "matches(\"$escapedJavaHeader\");"
|
return "matches(\"$escapedJavaHeader\");"
|
||||||
}
|
}
|
||||||
|
|
||||||
private String patternText(Pattern value) {
|
|
||||||
return "==~ java.util.regex.Pattern.compile('$value')"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,8 +30,12 @@ import org.springframework.cloud.contract.verifier.config.ContractVerifierConfig
|
|||||||
import org.springframework.cloud.contract.verifier.util.MapConverter
|
import org.springframework.cloud.contract.verifier.util.MapConverter
|
||||||
|
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
import static org.apache.commons.text.StringEscapeUtils.escapeJava
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Jakub Kubrynski, codearte.io
|
* @author Jakub Kubrynski, codearte.io
|
||||||
|
* @author Tim Ysewyn
|
||||||
*/
|
*/
|
||||||
@PackageScope
|
@PackageScope
|
||||||
@TypeChecked
|
@TypeChecked
|
||||||
@@ -226,7 +230,8 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String convertHeaderComparison(Pattern headerValue) {
|
protected String convertHeaderComparison(Pattern headerValue) {
|
||||||
return "==~ java.util.regex.Pattern.compile('$headerValue')"
|
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
|
// #273 - should escape $ for Groovy since it will try to make it a GString
|
||||||
|
|||||||
@@ -824,4 +824,63 @@ Contract.make {
|
|||||||
stripped(test) == stripped(expectedMsg)
|
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");
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user