diff --git a/spring-cloud-contract.html b/spring-cloud-contract.html index 9e276ab8b5..3c7c8b6e64 100644 --- a/spring-cloud-contract.html +++ b/spring-cloud-contract.html @@ -6777,6 +6777,96 @@ or just set the ignored property on the contract itself:

} +
+

Request may contain multipart elements. Just call the multipart() method.

+
+
+
+
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('.+')), p('filename.csv')),
+						// content of the file
+						content: $(c(regex('.+')), p('file content')))
+		)
+	}
+	response {
+		status 200
+	}
+}
+
+
+
+

In this example we defined parameters either directly by using the map notation, +where the value can be a dynamic property (e.g. formParameter: $(consumer(…​), producer(…​))) + or by using the named(…​) method that allows you to set a named parameter. + A named parameter can set a name and content. You can call it either via + a method with 2 arguments: e.g. named("fileName", "fileContent") or + via a map notation named(name: "fileName", content: "fileContent").

+
+
+

From this contract the generated test will look more or less like this:

+
+
+
+
// given:
+ MockMvcRequestSpecification request = given()
+   .header("Content-Type", "multipart/form-data;boundary=AaB03x")
+   .param("formParameter", "\"formParameterValue\"")
+   .param("someBooleanParameter", "true")
+   .multiPart("file", "filename.csv", "file content".getBytes());
+
+// when:
+ ResponseOptions response = given().spec(request)
+   .put("/multipart");
+
+// then:
+ assertThat(response.statusCode()).isEqualTo(200);
+
+
+
+

The WireMock stub will look more or less like this:

+
+
+
+
			'''
+{
+  "request" : {
+	"url" : "/multipart",
+	"method" : "PUT",
+	"headers" : {
+	  "Content-Type" : {
+		"matches" : "multipart/form-data;boundary=AaB03x.*"
+	  }
+	},
+	"bodyPatterns" : [ {
+		"matches" : ".*--(.*)\\r\\nContent-Disposition: form-data; name=\\"formParameter\\"\\r\\n(Content-Type: .*\\r\\n)?(Content-Length: \\\\d+\\r\\n)?\\r\\n\\".+\\"\\r\\n--\\\\1.*"
+  		}, {
+    			"matches" : ".*--(.*)\\r\\nContent-Disposition: form-data; name=\\"someBooleanParameter\\"\\r\\n(Content-Type: .*\\r\\n)?(Content-Length: \\\\d+\\r\\n)?\\r\\n(true|false)\\r\\n--\\\\1.*"
+  		}, {
+	  "matches" : ".*--(.*)\\r\\nContent-Disposition: form-data; name=\\"file\\"; filename=\\".+\\"\\r\\n(Content-Type: .*\\r\\n)?(Content-Length: \\\\d+\\r\\n)?\\r\\n.+\\r\\n--\\\\1.*"
+	} ]
+  },
+  "response" : {
+	"status" : 200,
+	"transformers" : [ "response-template" ]
+  }
+}
+	'''
+
+

Response