Added support for multipart with content type, fixes gh-599

This commit is contained in:
Marcin Grzejszczak
2018-04-11 15:37:13 +02:00
parent 34f367e62b
commit b965c49a7e
13 changed files with 241 additions and 10 deletions

View File

@@ -136,7 +136,9 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
if (request.multipart.clientValue instanceof Map) {
List<StringValuePattern> multipartPatterns = (request.multipart.clientValue as Map).collect {
(it.value instanceof NamedProperty
? WireMock.matching(RegexPatterns.multipartFile(it.key, (it.value as NamedProperty).name.clientValue, (it.value as NamedProperty).value.clientValue))
? WireMock.matching(RegexPatterns.multipartFile(it.key, (it.value as NamedProperty).name.clientValue,
(it.value as NamedProperty).value.clientValue,
(it.value as NamedProperty).contentType?.clientValue))
: WireMock.matching(RegexPatterns.multipartParam(it.key, it.value)) )
}
multipartPatterns.each {

View File

@@ -399,11 +399,13 @@ class ContentUtils {
}
static String getGroovyMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
return "'$propertyName', ${namedPropertyName(propertyValue, "'")}, ${groovyNamedPropertyValue(propertyValue, "'")}"
return "'$propertyName', ${namedPropertyName(propertyValue, "'")}, " +
"${groovyNamedPropertyValue(propertyValue, "'")}" + namedContentTypeNameIfPresent(propertyValue, "'")
}
static String getJavaMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
return """"${escapeJava(propertyName)}", ${namedPropertyName(propertyValue, '"')}, ${javaNamedPropertyValue(propertyValue, '"')}"""
return """"${escapeJava(propertyName)}", ${namedPropertyName(propertyValue, '"')}, """ +
"""${javaNamedPropertyValue(propertyValue, '"')}${namedContentTypeNameIfPresent(propertyValue, '"')}"""
}
static String namedPropertyName(NamedProperty property, String quote) {
@@ -411,6 +413,15 @@ class ContentUtils {
property.name.serverValue.toString() : quote + escapeJava(property.name.serverValue.toString()) + quote
}
static String namedContentTypeNameIfPresent(NamedProperty property, String quote) {
if (!property.contentType) {
return ""
}
String contentType = property.contentType.serverValue instanceof ExecutionProperty ?
property.contentType.serverValue.toString() : quote + escapeJava(property.contentType.serverValue.toString()) + quote
return ", " + contentType
}
static String groovyNamedPropertyValue(NamedProperty property, String quote) {
if (property.value.serverValue instanceof ExecutionProperty) {
return property.value.serverValue.toString()

View File

@@ -1295,7 +1295,9 @@ World.'''"""
// name of the file
name: $(c(regex(nonEmpty())), p('filename.csv')),
// content of the file
content: $(c(regex(nonEmpty())), p('file content')))
content: $(c(regex(nonEmpty())), p('file content')),
// content type for the part
contentType: $(c(regex(nonEmpty())), p('application/json')))
)
}
response {
@@ -1316,6 +1318,56 @@ World.'''"""
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', 'filename.csv', 'file content'.bytes, 'application/json')"""]
"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(), "application/json");']
}
@Issue('180')
def "should generate proper test code when having multipart parameters without content type 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(
// key (parameter name), value (parameter value) pair
formParameter: $(c(regex('".+"')), p('"formParameterValue"')),
someBooleanParameter: $(c(regex(anyBoolean())), p('true')),
// a named parameter (e.g. with `file` name) that represents file with
// `name` and `content`. You can also call `named("fileName", "fileContent")`
file: named(
// name of the file
name: $(c(regex(nonEmpty())), p('filename.csv')),
// content of the file
content: $(c(regex(nonEmpty())), p('file content')))
)
}
response {
status OK()
}
}
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')""",