diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBodyBuilder.groovy index a80ce77f89..8e8b7a9f4b 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBodyBuilder.groovy @@ -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() } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy index ddd905e711..8bbd5a0a07 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy @@ -122,16 +122,15 @@ abstract class SpockMethodBodyBuilder { return trimRepeatedQuotes(json) } - protected String getFileContentAsString() { - return request.multipart.serverValue //TODO replace to working extraction + protected Map getMultipartParameters() { + return (Map)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 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) { diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy index 1b370b7126..e73990d9f4 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy @@ -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" + } + } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy index 195886ce0e..f5217c6266 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy @@ -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 {