Fixed wrong wrapping of execute() in JaxRs test generation

fixes gh-1252
This commit is contained in:
Marcin Grzejszczak
2019-11-08 14:12:35 +01:00
parent cacd9bb938
commit 7326dec3d1
4 changed files with 54 additions and 4 deletions

View File

@@ -154,7 +154,7 @@ class JaxRsClientJUnitMethodBodyBuilder extends JUnitMethodBodyBuilder {
if (header.name == 'Content-Type' || header.name == 'Accept') {
return
}
bb.addLine(".header(\"${header.name}\", ${quotedAndEscaped(header.serverValue as String)})")
bb.addLine(".header(\"${header.name}\", ${quotedAndEscaped(header.serverValue)})")
}
}

View File

@@ -20,6 +20,7 @@ import java.util.regex.Pattern
import groovy.transform.PackageScope
import groovy.transform.TypeChecked
import org.apache.commons.text.StringEscapeUtils
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.spec.internal.Cookie
@@ -160,7 +161,7 @@ class JaxRsClientSpockMethodRequestProcessingBodyBuilder extends SpockMethodRequ
if (header.name == 'Content-Type' || header.name == 'Accept') {
return
} // Particular headers are set via 'request' / 'entity' methods
bb.addLine(".header('${header.name}', '${header.serverValue}')")
bb.addLine(".header('${header.name}', ${quotedAndEscaped(header.serverValue)})".toString())
}
}
@@ -174,6 +175,14 @@ class JaxRsClientSpockMethodRequestProcessingBodyBuilder extends SpockMethodRequ
}
}
@Override
protected String quotedAndEscaped(Object object) {
if (object instanceof ExecutionProperty) {
return object.executionCommand
}
return "'" + StringEscapeUtils.escapeJava(object.toString()) + "'"
}
protected String getHeader(String name) {
return request.headers?.entries?.find { it.name == name }?.serverValue
}

View File

@@ -138,8 +138,11 @@ abstract class MethodBodyBuilder implements ClassVerifier {
return ContentType.UNKNOWN
}
protected String quotedAndEscaped(String string) {
return '"' + StringEscapeUtils.escapeJava(string) + '"'
protected String quotedAndEscaped(Object object) {
if (object instanceof ExecutionProperty) {
return object.executionCommand
}
return '"' + StringEscapeUtils.escapeJava(object.toString()) + '"'
}
/**

View File

@@ -3033,4 +3033,42 @@ DocumentContext parsedJson = JsonPath.parse(json);
SpockMessagingMethodBodyBuilder.simpleName | { Contract dsl -> new SpockMessagingMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
JUnitMessagingMethodBodyBuilder.simpleName | { Contract dsl -> new JUnitMessagingMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
}
@Issue('#1252')
def 'should call execute in headers instead of quoting it [#methodBuilderName]'() {
given:
Contract contractDsl = Contract.make {
request {
method PUT()
url '/frauds/name'
headers {
header(authorization(), value(client(anyNonBlankString()), server(execute("authToken()"))))
}
}
response {
status OK()
headers {
header(contentType(), "${fromRequest().header(contentType())}")
}
}
}
MethodBodyBuilder builder = methodBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
and:
builder.appendTo(blockBuilder)
String test = blockBuilder.toString()
when:
SyntaxChecker.tryToRun(methodBuilderName, test.join("\n"))
then:
!test.contains('''"authToken()"''')
!test.contains("""'authToken()'""")
where:
methodBuilderName | methodBuilder
HttpSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new HttpSpockMethodRequestProcessingBodyBuilder(dsl, properties, generatedClassDataForMethod) }
MockMvcJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
JaxRsClientSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientSpockMethodRequestProcessingBodyBuilder(dsl, properties, generatedClassDataForMethod) }
JaxRsClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
WebTestClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new WebTestClientJUnitMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
}
}