From 65cd71f38e19f795bd7a9740973f5c3b3d54c8e5 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 19 Mar 2018 10:54:02 +0100 Subject: [PATCH] Added support for byte multipart content fixes gh-546 --- .../verifier/util/ContentUtils.groovy | 25 ++++++++--- .../MockMvcMethodBodyBuilderSpec.groovy | 42 +++++++++++++++++++ ...AutoConfigureWireMockApplicationTests.java | 2 +- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy index fb59ff32c4..3f28c382db 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy @@ -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()" } } diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderSpec.groovy index bc93875aea..bea77944fa 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderSpec.groovy @@ -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: diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockApplicationTests.java index b7df3fdf09..d992c9bb28 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockApplicationTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockApplicationTests.java @@ -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;