Add support for multipart on server side

This commit is contained in:
Wiktor Szwechowicz
2015-12-01 11:38:00 +01:00
committed by Wiktor Szwechowicz
parent c481572c32
commit 683fb9b732
4 changed files with 23 additions and 11 deletions

View File

@@ -30,7 +30,7 @@ class MockMvcSpockMethodBodyBuilder extends SpockMethodBodyBuilder {
bb.addLine(".body('''$bodyAsString''')")
}
if (request.multipart) {
bb.addLine(".multiPart('''$fileAsString''', '''$filenameAsString''', '''$fileContentAsString'''.bytes)")
multipartParameters.each { entry -> bb.addLine(getMultipartParameterLine(entry)) }
}
bb.unindent()
}

View File

@@ -122,16 +122,15 @@ abstract class SpockMethodBodyBuilder {
return trimRepeatedQuotes(json)
}
protected String getFileContentAsString() {
return request.multipart.serverValue //TODO replace to working extraction
protected Map<String, Object> getMultipartParameters() {
return (Map<String, Object>)request.multipart.serverValue
}
protected String getFilenameAsString() {
return request.multipart.serverValue //TODO replace to working extraction
}
protected String getFileAsString() {
return request.multipart.serverValue //TODO replace to working extraction
protected String getMultipartParameterLine(Map.Entry<String, Object> parameter) {
if (parameter.value instanceof NamedProperty) {
return ".multiPart(${getMultipartFileParameterContent(parameter.key, (NamedProperty) parameter.value)})"
}
return ".param('$parameter.key', '$parameter.value')"
}
protected String convertUnicodeEscapes(String json) {

View File

@@ -8,6 +8,7 @@ import io.codearte.accurest.dsl.internal.DslProperty
import io.codearte.accurest.dsl.internal.ExecutionProperty
import io.codearte.accurest.dsl.internal.Headers
import io.codearte.accurest.dsl.internal.MatchingStrategy
import io.codearte.accurest.dsl.internal.NamedProperty
import io.codearte.accurest.dsl.internal.OptionalProperty
import org.codehaus.groovy.runtime.GStringImpl
@@ -320,4 +321,8 @@ class ContentUtils {
return ContentType.UNKNOWN
}
static String getMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
return "'$propertyName', '$propertyValue.name.serverValue', '$propertyValue.value.serverValue'.bytes"
}
}

View File

@@ -838,14 +838,18 @@ World.''')
World.'''""")
}
@Issue('180')
def "should generate proper test code when having multipart parameters"(){
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "PUT"
url "/multipart"
headers {
header('content-type', 'multipart/form-data;boundary=AaB03x')
}
multipart(
formParameter: value(client(regex('".+"')), server('"formParameterValue"')),
formParameter: value(client(regex('.+')), server('"formParameterValue"')),
someBooleanParameter: value(client(regex('(true|false)')), server('true')),
file: named(value(client(regex('.+')), server('filename.csv')), value(client(regex('.+')), server('file content')))
)
@@ -860,9 +864,13 @@ World.'''""")
builder.given(blockBuilder)
def spockTest = blockBuilder.toString()
then:
spockTest.contains('.multiPart')
spockTest.contains("""'content-type', 'multipart/form-data;boundary=AaB03x'""")
spockTest.contains(""".param('formParameter', '"formParameterValue"'""")
spockTest.contains(""".param('someBooleanParameter', 'true')""")
spockTest.contains(""".multiPart('file', 'filename.csv', 'file content'.bytes)""")
}
@Issue('180')
def "should generate proper test code when having multipart parameters with named as map"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {