|
| 1 | +package au.com.dius.pact.consumer.dsl |
| 2 | + |
| 3 | +import au.com.dius.pact.consumer.Headers |
| 4 | +import au.com.dius.pact.core.model.ContentType |
| 5 | +import au.com.dius.pact.core.model.OptionalBody |
| 6 | +import au.com.dius.pact.core.model.generators.Generators |
| 7 | +import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory |
| 8 | +import au.com.dius.pact.core.model.matchingrules.MatchingRules |
| 9 | +import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl |
| 10 | +import au.com.dius.pact.core.model.matchingrules.RegexMatcher |
| 11 | +import org.apache.hc.client5.http.entity.mime.HttpMultipartMode |
| 12 | +import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder |
| 13 | +import org.apache.hc.core5.http.HttpEntity |
| 14 | +import java.io.ByteArrayOutputStream |
| 15 | +import java.io.InputStream |
| 16 | +import java.nio.charset.Charset |
| 17 | + |
| 18 | +/** |
| 19 | + * Builder class for constructing multipart/\* bodies. |
| 20 | + */ |
| 21 | +open class MultipartBuilder: BodyBuilder { |
| 22 | + private val builder = MultipartEntityBuilder.create() |
| 23 | + private var entity: HttpEntity? = null |
| 24 | + val matchingRules: MatchingRules = MatchingRulesImpl() |
| 25 | + private val generators = Generators() |
| 26 | + |
| 27 | + init { |
| 28 | + builder.setMode(HttpMultipartMode.EXTENDED) |
| 29 | + } |
| 30 | + |
| 31 | + override fun getMatchers(): MatchingRuleCategory { |
| 32 | + build() |
| 33 | + return matchingRules.rulesForCategory("body") |
| 34 | + } |
| 35 | + |
| 36 | + override fun getHeaderMatchers(): MatchingRuleCategory { |
| 37 | + build() |
| 38 | + return matchingRules.rulesForCategory("header") |
| 39 | + } |
| 40 | + |
| 41 | + override fun getGenerators(): Generators { |
| 42 | + build() |
| 43 | + return generators |
| 44 | + } |
| 45 | + |
| 46 | + override fun getContentType(): ContentType { |
| 47 | + build() |
| 48 | + return ContentType(entity!!.contentType) |
| 49 | + } |
| 50 | + |
| 51 | + private fun build() { |
| 52 | + if (entity == null) { |
| 53 | + entity = builder.build() |
| 54 | + val headerRules = matchingRules.addCategory("header") |
| 55 | + headerRules.addRule("Content-Type", RegexMatcher(Headers.MULTIPART_HEADER_REGEX, entity!!.contentType)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + override fun buildBody(): ByteArray { |
| 60 | + build() |
| 61 | + val stream = ByteArrayOutputStream() |
| 62 | + entity!!.writeTo(stream) |
| 63 | + return stream.toByteArray() |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Adds the contents of an input stream as a binary part with the given name and file name |
| 68 | + */ |
| 69 | + @JvmOverloads |
| 70 | + fun filePart( |
| 71 | + partName: String, |
| 72 | + fileName: String? = null, |
| 73 | + inputStream: InputStream, |
| 74 | + contentType: String? = null |
| 75 | + ): MultipartBuilder { |
| 76 | + val ct = if (contentType.isNullOrEmpty()) { |
| 77 | + null |
| 78 | + } else { |
| 79 | + org.apache.hc.core5.http.ContentType.create(contentType) |
| 80 | + } |
| 81 | + builder.addBinaryBody(partName, inputStream.use { it.readAllBytes() }, ct, fileName) |
| 82 | + return this |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Adds the contents of a byte array as a binary part with the given name and file name |
| 87 | + */ |
| 88 | + @JvmOverloads |
| 89 | + fun binaryPart( |
| 90 | + partName: String, |
| 91 | + fileName: String? = null, |
| 92 | + bytes: ByteArray, |
| 93 | + contentType: String? = null |
| 94 | + ): MultipartBuilder { |
| 95 | + val ct = if (contentType.isNullOrEmpty()) { |
| 96 | + null |
| 97 | + } else { |
| 98 | + org.apache.hc.core5.http.ContentType.create(contentType) |
| 99 | + } |
| 100 | + builder.addBinaryBody(partName, bytes, ct, fileName) |
| 101 | + return this |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Adds a JSON document as a part, using the standard Pact JSON DSL |
| 106 | + */ |
| 107 | + fun jsonPart(partName: String, part: DslPart): MultipartBuilder { |
| 108 | + val parent = part.close()!! |
| 109 | + matchingRules.addCategory(parent.matchers.copyWithUpdatedMatcherRootPrefix("\$.$partName")) |
| 110 | + generators.addGenerators(parent.generators) |
| 111 | + builder.addTextBody(partName, part.body.toString(), org.apache.hc.core5.http.ContentType.APPLICATION_JSON) |
| 112 | + return this |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Adds the contents of a string as a text part with the given name |
| 117 | + */ |
| 118 | + @JvmOverloads |
| 119 | + fun textPart( |
| 120 | + partName: String, |
| 121 | + value: String, |
| 122 | + contentType: String? = null |
| 123 | + ): MultipartBuilder { |
| 124 | + val ct = if (contentType.isNullOrEmpty()) { |
| 125 | + null |
| 126 | + } else { |
| 127 | + org.apache.hc.core5.http.ContentType.create(contentType) |
| 128 | + } |
| 129 | + builder.addTextBody(partName, value, ct) |
| 130 | + return this |
| 131 | + } |
| 132 | +} |
0 commit comments