Skip to content

Commit

Permalink
feat: Ignore missing part content type headers with multipart bodies #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jul 24, 2021
1 parent 05cafce commit 29b7638
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ class PactBuilder extends GroovyBuilder {
void withFileUpload(String partName, String fileName, String fileContentType, byte[] data) {
def multipart = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody(partName, data, ContentType.create(fileContentType), fileName)
.addBinaryBody(partName, data, fileContentType ? ContentType.create(fileContentType) :
ContentType.DEFAULT_TEXT, fileName)
.build()
ByteArrayOutputStream os = new ByteArrayOutputStream()
multipart.writeTo(os)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ protected void setupDefaultValues() {
}
}

protected void setupFileUpload(String partName, String fileName, String fileContentType, byte[] data) throws IOException {
protected void setupFileUpload(
String partName,
String fileName,
String fileContentType,
byte[] data
) throws IOException {
ContentType contentType = ContentType.DEFAULT_TEXT;
if (!fileContentType.isEmpty()) {
contentType = ContentType.create(fileContentType);
}
HttpEntity multipart = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody(partName, data, ContentType.create(fileContentType), fileName)
.addBinaryBody(partName, data, contentType, fileName)
.build();
ByteArrayOutputStream os = new ByteArrayOutputStream();
multipart.writeTo(os);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package au.com.dius.pact.core.matchers

import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.matchingrules.MatchingRules
import mu.KLogging
import java.util.Enumeration
import javax.mail.BodyPart
import javax.mail.Header
Expand Down Expand Up @@ -56,8 +57,12 @@ class MultipartMessageBodyMatcher : BodyMatcher {
"Expected a multipart header '${it.name}' with value '${it.value}', but was '$actualValue'"))))
}
} else {
mismatches.add(BodyItemMatchResult(it.name, listOf(BodyMismatch(it.toString(), null,
"Expected a multipart header '${it.name}', but was missing"))))
if (it.name.equals("Content-Type", ignoreCase = true)) {
logger.debug { "Ignoring missing Content-Type header" }
} else {
mismatches.add(BodyItemMatchResult(it.name, listOf(BodyMismatch(it.toString(), null,
"Expected a multipart header '${it.name}', but was missing"))))
}
}
}

Expand All @@ -68,4 +73,6 @@ class MultipartMessageBodyMatcher : BodyMatcher {
val multipart = MimeMultipart(ByteArrayDataSource(body, contentType))
return multipart.getBodyPart(0)
}

companion object : KLogging()
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class MultipartMessageBodyMatcherSpec extends Specification {
expectedBody = multipart('form-data', 'file', '476.csv', 'text/plain', 'Test: true\n', '1234')
}

def 'Ignores missing content type header, which is optional'() {
expect:
matcher.matchBody(expectedBody, actualBody, context).mismatches.empty

where:

actualBody = multipart('form-data', 'file', '476.csv', null, '', '1234')
expectedBody = multipart('form-data', 'file', '476.csv', 'text/plain', '', '1234')
}

def 'returns a mismatch - when the headers do not match'() {
expect:
matcher.matchBody(expectedBody, actualBody, true, new MatchingRulesImpl()).mismatches*.mismatch == [
Expand Down Expand Up @@ -85,16 +95,26 @@ class MultipartMessageBodyMatcherSpec extends Specification {

@SuppressWarnings('ParameterCount')
OptionalBody multipart(disposition, name, filename, contentType, headers, body) {
def contentTypeLine = ''
def headersLine = ''
if (contentType) {
contentTypeLine = "Content-Type: $contentType"
if (headers) {
headersLine = "$contentTypeLine\n$headers"
} else {
headersLine = contentTypeLine
}
} else if (headers) {
headersLine = headers
}
OptionalBody.body(
"""--XXX
|Content-Disposition: $disposition; name=\"$name\"; filename=\"$filename\"
|Content-Type: $contentType
|$headers
|$headersLine
|
|$body
|--XXX
""".stripMargin().bytes, new ContentType('multipart/form-data; boundary=XXX')
)
}

}

0 comments on commit 29b7638

Please sign in to comment.