Added support for byte multipart content

fixes gh-546
This commit is contained in:
Marcin Grzejszczak
2018-03-19 10:54:02 +01:00
parent cd723739b5
commit 65cd71f38e
3 changed files with 63 additions and 6 deletions

View File

@@ -399,11 +399,11 @@ class ContentUtils {
}
static String getGroovyMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
return "'$propertyName', ${namedPropertyName(propertyValue, "'")}, ${namedPropertyValue(propertyValue, "'")}.bytes"
return "'$propertyName', ${namedPropertyName(propertyValue, "'")}, ${groovyNamedPropertyValue(propertyValue, "'")}"
}
static String getJavaMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
return """"${escapeJava(propertyName)}", ${namedPropertyName(propertyValue, '"')}, ${namedPropertyValue(propertyValue, '"')}.getBytes()"""
return """"${escapeJava(propertyName)}", ${namedPropertyName(propertyValue, '"')}, ${javaNamedPropertyValue(propertyValue, '"')}"""
}
static String namedPropertyName(NamedProperty property, String quote) {
@@ -411,9 +411,24 @@ class ContentUtils {
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
static String groovyNamedPropertyValue(NamedProperty property, String quote) {
if (property.value.serverValue instanceof ExecutionProperty) {
return property.value.serverValue.toString()
} else if (property.value.serverValue instanceof byte[]) {
byte[] bytes = (byte[]) property.value.serverValue
return "[" + bytes.collect { it }.join(", ") + "] as byte[]"
}
return quote + escapeJava(property.value.serverValue.toString()) + quote + ".bytes"
}
static String javaNamedPropertyValue(NamedProperty property, String quote) {
if (property.value.serverValue instanceof ExecutionProperty) {
return property.value.serverValue.toString()
} else if (property.value.serverValue instanceof byte[]) {
byte[] bytes = (byte[]) property.value.serverValue
return "new byte[] {" + bytes.collect { it }.join(", ") + "}"
}
return quote + escapeJava(property.value.serverValue.toString()) + quote + ".getBytes()"
}
}

View File

@@ -1218,6 +1218,48 @@ World.'''"""
'.multiPart("file", "filename.csv", "file content".getBytes());']
}
@Issue('546')
def "should generate test code when having multipart parameters with byte array #methodBuilderName"() {
given:
// tag::multipartdsl[]
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(
file: named(
name: value(stub(regex('.+')), test('file')),
content: value(stub(regex('.+')), test([100, 117, 112, 97] as byte[]))
)
)
}
response {
status 200
}
}
// end::multipartdsl[]
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"',
""".multiPart('file', 'file', [100, 117, 112, 97] as byte[])"""]
"MockMvcJUnitMethodBuilder" | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties) } | ['"Content-Type", "multipart/form-data;boundary=AaB03x"',
'.multiPart("file", "file", new byte[] {100, 117, 112, 97});']
}
@Issue('541')
def "should generate proper test code when having multipart parameters that use execute with #methodBuilderName"() {
given:

View File

@@ -1,6 +1,6 @@
package org.springframework.cloud.contract.wiremock;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
`import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;