Skip to content

Commit

Permalink
fix: support multipart form posts with multiple parts #1574
Browse files Browse the repository at this point in the history
  • Loading branch information
uglyog committed Jul 26, 2022
1 parent 67b2a8c commit fe762dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PostImageBodyTest {
.method('POST')
.path('/images')
.withFileUpload('photo', 'ron.jpg', 'image/jpeg', stream.bytes)
.withFileUpload('text', 'ron.txt', 'text/plain', 'hello world!'.bytes)
.willRespondWith()
.status(200)
.body(new PactDslJsonBody()
Expand All @@ -45,6 +46,7 @@ class PostImageBodyTest {
def data = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody('photo', stream, ContentType.create('image/jpeg'), 'ron.jpg')
.addBinaryBody('text', 'some text stuff'.bytes, ContentType.create('text/plain'), 'ron.txt')
.build()
def request = RequestBuilder
.post(mockServer.url + '/images')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ open class PactDslRequestBase(
@JvmField
var requestGenerators = Generators()

var multipartBuilder: MultipartEntityBuilder? = null

protected fun setupDefaultValues() {
if (defaultRequestValues != null) {
if (StringUtils.isNotEmpty(defaultRequestValues.requestMethod)) {
Expand All @@ -67,21 +69,29 @@ open class PactDslRequestBase(
ContentType.create(fileContentType)
else
ContentType.DEFAULT_TEXT
val multipart = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.EXTENDED)
.addBinaryBody(partName, data, contentType, fileName)
setupMultipart(multipart)
if (multipartBuilder == null) {
multipartBuilder = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.EXTENDED)
.addBinaryBody(partName, data, contentType, fileName)
} else {
multipartBuilder!!.addBinaryBody(partName, data, contentType, fileName)
}
setupMultipart(multipartBuilder!!)
}

fun setupMultipart(multipart: MultipartEntityBuilder) {
val entity = multipart.build()
val os = ByteArrayOutputStream()
entity.writeTo(os)
requestBody = body(os.toByteArray(),
au.com.dius.pact.core.model.ContentType(entity.contentType))
requestMatchers.addCategory("header").addRule(CONTENT_TYPE, RegexMatcher(MULTIPART_HEADER_REGEX,
entity.contentType))
requestHeaders[CONTENT_TYPE] = listOf(entity.contentType)
requestBody = body(os.toByteArray(), au.com.dius.pact.core.model.ContentType(entity.contentType))
val matchingRuleCategory = requestMatchers.addCategory("header")
if (!matchingRuleCategory.matchingRules.containsKey(CONTENT_TYPE)) {
matchingRuleCategory.addRule(CONTENT_TYPE, RegexMatcher(MULTIPART_HEADER_REGEX,
entity.contentType))
}
if (!requestHeaders.containsKey(CONTENT_TYPE)) {
requestHeaders[CONTENT_TYPE] = listOf(entity.contentType)
}
}

protected fun queryMatchingDateBase(field: String, pattern: String?, example: String?): PactDslRequestBase {
Expand Down

0 comments on commit fe762dc

Please sign in to comment.