/
MultipartMessageContentMatcher.kt
executable file
·133 lines (119 loc) · 5.1 KB
/
MultipartMessageContentMatcher.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package au.com.dius.pact.core.matchers
import au.com.dius.pact.core.model.ContentType
import au.com.dius.pact.core.model.HttpRequest
import au.com.dius.pact.core.model.IHttpPart
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.support.Result
import io.pact.plugins.jvm.core.InteractionContents
import io.github.oshai.kotlinlogging.KLogging
import java.util.Enumeration
import javax.mail.BodyPart
import javax.mail.Header
import javax.mail.internet.MimeMultipart
import javax.mail.util.ByteArrayDataSource
class MultipartMessageContentMatcher : ContentMatcher {
override fun matchBody(
expected: OptionalBody,
actual: OptionalBody,
context: MatchingContext
): BodyMatchResult {
return when {
expected.isMissing() -> BodyMatchResult(null, emptyList())
expected.isPresent() && actual.isNotPresent() -> BodyMatchResult(null,
listOf(BodyItemMatchResult("$", listOf(BodyMismatch(expected.orEmpty(),
null, "Expected a multipart body but was missing")))))
expected.isEmpty() && actual.isEmpty() -> BodyMatchResult(null, emptyList())
else -> {
val expectedMultipart = MimeMultipart(ByteArrayDataSource(expected.orEmpty(), expected.contentType.toString()))
val actualMultipart = MimeMultipart(ByteArrayDataSource(actual.orEmpty(), actual.contentType.toString()))
BodyMatchResult(null, compareParts(expectedMultipart, actualMultipart, context))
}
}
}
private fun compareParts(
expectedMultipart: MimeMultipart,
actualMultipart: MimeMultipart,
context: MatchingContext
): List<BodyItemMatchResult> {
val matchResults = mutableListOf<BodyItemMatchResult>()
logger.debug { "Comparing multiparts: expected has ${expectedMultipart.count} part(s), " +
"actual has ${actualMultipart.count} part(s)" }
if (expectedMultipart.count != actualMultipart.count) {
matchResults.add(BodyItemMatchResult("$", listOf(BodyMismatch(expectedMultipart.count, actualMultipart.count,
"Expected a multipart message with ${expectedMultipart.count} part(s), " +
"but received one with ${actualMultipart.count} part(s)"))))
}
for (i in 0 until expectedMultipart.count) {
val expectedPart = expectedMultipart.getBodyPart(i)
if (i < actualMultipart.count) {
val actualPart = actualMultipart.getBodyPart(i)
val path = "\$.$i"
val headerResult = compareHeaders(path, expectedPart, actualPart, context)
logger.debug { "Comparing part $i: header mismatches ${headerResult.size}" }
val bodyMismatches = compareContents(path, expectedPart, actualPart, context)
logger.debug { "Comparing part $i: content mismatches ${bodyMismatches.size}" }
matchResults.add(BodyItemMatchResult(path, headerResult + bodyMismatches))
}
}
return matchResults
}
override fun setupBodyFromConfig(
bodyConfig: Map<String, Any?>
): Result<List<InteractionContents>, String> {
return Result.Ok(listOf(InteractionContents("",
OptionalBody.body(
bodyConfig["body"].toString().toByteArray(),
ContentType("multipart/form-data")
)
)))
}
@Suppress("UnusedPrivateMember")
private fun compareContents(
path: String,
expectedMultipart: BodyPart,
actualMultipart: BodyPart,
context: MatchingContext
): List<BodyMismatch> {
val expected = bodyPartTpHttpPart(expectedMultipart)
val actual = bodyPartTpHttpPart(actualMultipart)
logger.debug { "Comparing multipart contents: ${expected.determineContentType()} -> ${actual.determineContentType()}" }
val result = Matching.matchBody(expected, actual, context)
return result.bodyResults.flatMap { matchResult ->
matchResult.result.map {
it.copy(path = path + it.path.removePrefix("$"))
}
}
}
private fun bodyPartTpHttpPart(multipart: BodyPart): IHttpPart {
return HttpRequest(headers = mutableMapOf("content-type" to listOf(multipart.contentType)),
body = OptionalBody.body(multipart.inputStream.readAllBytes(), ContentType(multipart.contentType)))
}
private fun compareHeaders(
path: String,
expectedMultipart: BodyPart,
actualMultipart: BodyPart,
context: MatchingContext
): List<BodyMismatch> {
val mismatches = mutableListOf<BodyMismatch>()
(expectedMultipart.allHeaders as Enumeration<Header>).asSequence().forEach {
val header = actualMultipart.getHeader(it.name)
if (header != null) {
val actualValue = header.joinToString(separator = ", ")
if (actualValue != it.value) {
mismatches.add(BodyMismatch(it.toString(), null,
"Expected a multipart header '${it.name}' with value '${it.value}', but was '$actualValue'",
path + "." + it.name))
}
} else {
if (it.name.equals("Content-Type", ignoreCase = true)) {
logger.debug { "Ignoring missing Content-Type header" }
} else {
mismatches.add(BodyMismatch(it.toString(), null,
"Expected a multipart header '${it.name}', but was missing", path + "." + it.name))
}
}
}
return mismatches
}
companion object : KLogging()
}