Allows execute property in multipart; fixes gh-541
This commit is contained in:
@@ -386,12 +386,21 @@ class ContentUtils {
|
||||
}
|
||||
|
||||
static String getGroovyMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
|
||||
return "'$propertyName', '$propertyValue.name.serverValue', '$propertyValue.value.serverValue'.bytes"
|
||||
return "'$propertyName', ${namedPropertyName(propertyValue, "'")}, ${namedPropertyValue(propertyValue, "'")}.bytes"
|
||||
}
|
||||
|
||||
static String getJavaMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
|
||||
return """"${escapeJava(propertyName)}", "${escapeJava(propertyValue.name.serverValue as String)}", "${escapeJava(propertyValue.value.serverValue as String)}".getBytes()"""
|
||||
return """"${escapeJava(propertyName)}", ${namedPropertyName(propertyValue, '"')}, ${namedPropertyValue(propertyValue, '"')}.getBytes()"""
|
||||
}
|
||||
|
||||
static String namedPropertyName(NamedProperty property, String quote) {
|
||||
return property.name.serverValue instanceof ExecutionProperty ?
|
||||
property.name.serverValue.toString() : quote + escapeJava(property.name.serverValue.toString()) + quote
|
||||
}
|
||||
|
||||
static String namedPropertyValue(NamedProperty property, String quote) {
|
||||
return property.value.serverValue instanceof ExecutionProperty ?
|
||||
property.value.serverValue.toString() : quote + escapeJava(property.value.serverValue.toString()) + quote
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1201,22 +1201,67 @@ World.'''"""
|
||||
def test = blockBuilder.toString()
|
||||
then:
|
||||
for (String requestString : requestStrings) {
|
||||
test.contains(requestString)
|
||||
assert test.contains(requestString)
|
||||
}
|
||||
and:
|
||||
SyntaxChecker.tryToCompile(methodBuilderName, blockBuilder.toString())
|
||||
where:
|
||||
methodBuilderName | methodBuilder | requestStrings
|
||||
"MockMvcSpockMethodBuilder" | { Contract dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | ["""'content-type', 'multipart/form-data;boundary=AaB03x'""",
|
||||
"MockMvcSpockMethodBuilder" | { Contract dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | ['"Content-Type", "multipart/form-data;boundary=AaB03x"',
|
||||
""".param('formParameter', '"formParameterValue"'""",
|
||||
""".param('someBooleanParameter', 'true')""",
|
||||
""".multiPart('file', 'filename.csv', 'file content'.bytes)"""]
|
||||
"MockMvcJUnitMethodBuilder" | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties) } | ['"content-type", "multipart/form-data;boundary=AaB03x"',
|
||||
"MockMvcJUnitMethodBuilder" | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties) } | ['"Content-Type", "multipart/form-data;boundary=AaB03x"',
|
||||
'.param("formParameter", "\\"formParameterValue\\"")',
|
||||
'.param("someBooleanParameter", "true")',
|
||||
'.multiPart("file", "filename.csv", "file content".getBytes());']
|
||||
}
|
||||
|
||||
@Issue('541')
|
||||
def "should generate proper test code when having multipart parameters that use execute with #methodBuilderName"() {
|
||||
given:
|
||||
org.springframework.cloud.contract.spec.Contract contractDsl = org.springframework.cloud.contract.spec.Contract.make {
|
||||
request {
|
||||
method "PUT"
|
||||
url "/multipart"
|
||||
headers {
|
||||
contentType('multipart/form-data;boundary=AaB03x')
|
||||
}
|
||||
multipart(
|
||||
formParameter: $(c(regex('".+"')), p('"formParameterValue"')),
|
||||
someBooleanParameter: $(c(regex(anyBoolean())), p('true')),
|
||||
file: named(
|
||||
name: $(c(regex(nonEmpty())), p(execute("toString()"))),
|
||||
content: $(c(regex(nonEmpty())), p('file content')))
|
||||
)
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
MethodBodyBuilder builder = methodBuilder(contractDsl)
|
||||
BlockBuilder blockBuilder = new BlockBuilder(" ")
|
||||
when:
|
||||
builder.appendTo(blockBuilder)
|
||||
def test = blockBuilder.toString()
|
||||
then:
|
||||
for (String requestString : requestStrings) {
|
||||
assert test.contains(requestString)
|
||||
}
|
||||
and:
|
||||
SyntaxChecker.tryToCompile(methodBuilderName, blockBuilder.toString())
|
||||
where:
|
||||
methodBuilderName | methodBuilder | requestStrings
|
||||
"MockMvcSpockMethodBuilder" | { Contract dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | ['"Content-Type", "multipart/form-data;boundary=AaB03x"',
|
||||
""".param('formParameter', '"formParameterValue"'""",
|
||||
""".param('someBooleanParameter', 'true')""",
|
||||
""".multiPart('file', toString(), 'file content'.bytes)"""]
|
||||
"MockMvcJUnitMethodBuilder" | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties) } | ['"Content-Type", "multipart/form-data;boundary=AaB03x"',
|
||||
'.param("formParameter", "\\"formParameterValue\\"")',
|
||||
'.param("someBooleanParameter", "true")',
|
||||
'.multiPart("file", toString(), "file content".getBytes());']
|
||||
}
|
||||
|
||||
@Issue('180')
|
||||
def "should generate proper test code when having multipart parameters with named as map with #methodBuilderName"() {
|
||||
given:
|
||||
|
||||
Reference in New Issue
Block a user