-
-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for adding multiparts that can use JSON DSL #1642
- Loading branch information
1 parent
f98f1ad
commit b1806ab
Showing
10 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/MultipartRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package au.com.dius.pact.consumer.junit5; | ||
|
||
import au.com.dius.pact.consumer.MockServer; | ||
import au.com.dius.pact.consumer.dsl.LambdaDsl; | ||
import au.com.dius.pact.consumer.dsl.MultipartBuilder; | ||
import au.com.dius.pact.consumer.dsl.PactBuilder; | ||
import au.com.dius.pact.core.model.V4Pact; | ||
import au.com.dius.pact.core.model.annotations.Pact; | ||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder; | ||
import org.apache.hc.client5.http.fluent.Request; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@ExtendWith(PactConsumerTestExt.class) | ||
@PactTestFor(providerName = "MultipartProvider") | ||
public class MultipartRequestTest { | ||
@Pact(consumer = "MultipartConsumer") | ||
public V4Pact pact(PactBuilder builder) { | ||
return builder | ||
.expectsToReceiveHttpInteraction("multipart request", interactionBuilder -> | ||
interactionBuilder | ||
.withRequest(requestBuilder -> requestBuilder | ||
.path("/path") | ||
.method("POST") | ||
.body(new MultipartBuilder() | ||
.filePart("file-part", "RAT.JPG", getClass().getResourceAsStream("/RAT.JPG"), "image/jpeg") | ||
.jsonPart("json-part", LambdaDsl.newJsonBody(body -> body | ||
.stringMatcher("a", "\\w+", "B") | ||
.integerType("c", 100)).build()) | ||
) | ||
) | ||
.willRespondWith(responseBuilder -> responseBuilder.status(201)) | ||
) | ||
.toPact(); | ||
} | ||
|
||
@Test | ||
@PactTestFor | ||
void testArticles(MockServer mockServer) throws IOException { | ||
ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.post(mockServer.getUrl() + "/path") | ||
.body( | ||
MultipartEntityBuilder.create() | ||
.addBinaryBody("file-part", getClass().getResourceAsStream("/RAT.JPG"), ContentType.IMAGE_JPEG, "RAT.JPG") | ||
.addTextBody("json-part", "{\"a\": \"B\", \"c\": 1234}", ContentType.APPLICATION_JSON) | ||
.build() | ||
) | ||
.execute() | ||
.returnResponse(); | ||
assertThat(httpResponse.getCode(), is(equalTo(201))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/MultipartBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package au.com.dius.pact.consumer.dsl | ||
|
||
import au.com.dius.pact.consumer.Headers | ||
import au.com.dius.pact.core.model.ContentType | ||
import au.com.dius.pact.core.model.OptionalBody | ||
import au.com.dius.pact.core.model.generators.Generators | ||
import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory | ||
import au.com.dius.pact.core.model.matchingrules.MatchingRules | ||
import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl | ||
import au.com.dius.pact.core.model.matchingrules.RegexMatcher | ||
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode | ||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder | ||
import org.apache.hc.core5.http.HttpEntity | ||
import java.io.ByteArrayOutputStream | ||
import java.io.InputStream | ||
import java.nio.charset.Charset | ||
|
||
/** | ||
* Builder class for constructing multipart/\* bodies. | ||
*/ | ||
open class MultipartBuilder: BodyBuilder { | ||
private val builder = MultipartEntityBuilder.create() | ||
private var entity: HttpEntity? = null | ||
val matchingRules: MatchingRules = MatchingRulesImpl() | ||
private val generators = Generators() | ||
|
||
init { | ||
builder.setMode(HttpMultipartMode.EXTENDED) | ||
} | ||
|
||
override fun getMatchers(): MatchingRuleCategory { | ||
build() | ||
return matchingRules.rulesForCategory("body") | ||
} | ||
|
||
override fun getHeaderMatchers(): MatchingRuleCategory { | ||
build() | ||
return matchingRules.rulesForCategory("header") | ||
} | ||
|
||
override fun getGenerators(): Generators { | ||
build() | ||
return generators | ||
} | ||
|
||
override fun getContentType(): ContentType { | ||
build() | ||
return ContentType(entity!!.contentType) | ||
} | ||
|
||
private fun build() { | ||
if (entity == null) { | ||
entity = builder.build() | ||
val headerRules = matchingRules.addCategory("header") | ||
headerRules.addRule("Content-Type", RegexMatcher(Headers.MULTIPART_HEADER_REGEX, entity!!.contentType)) | ||
} | ||
} | ||
|
||
override fun buildBody(): ByteArray { | ||
build() | ||
val stream = ByteArrayOutputStream() | ||
entity!!.writeTo(stream) | ||
return stream.toByteArray() | ||
} | ||
|
||
/** | ||
* Adds the contents of an input stream as a binary part with the given name and file name | ||
*/ | ||
@JvmOverloads | ||
fun filePart( | ||
partName: String, | ||
fileName: String? = null, | ||
inputStream: InputStream, | ||
contentType: String? = null | ||
): MultipartBuilder { | ||
val ct = if (contentType.isNullOrEmpty()) { | ||
null | ||
} else { | ||
org.apache.hc.core5.http.ContentType.create(contentType) | ||
} | ||
builder.addBinaryBody(partName, inputStream.use { it.readAllBytes() }, ct, fileName) | ||
return this | ||
} | ||
|
||
/** | ||
* Adds the contents of a byte array as a binary part with the given name and file name | ||
*/ | ||
@JvmOverloads | ||
fun binaryPart( | ||
partName: String, | ||
fileName: String? = null, | ||
bytes: ByteArray, | ||
contentType: String? = null | ||
): MultipartBuilder { | ||
val ct = if (contentType.isNullOrEmpty()) { | ||
null | ||
} else { | ||
org.apache.hc.core5.http.ContentType.create(contentType) | ||
} | ||
builder.addBinaryBody(partName, bytes, ct, fileName) | ||
return this | ||
} | ||
|
||
/** | ||
* Adds a JSON document as a part, using the standard Pact JSON DSL | ||
*/ | ||
fun jsonPart(partName: String, part: DslPart): MultipartBuilder { | ||
val parent = part.close()!! | ||
matchingRules.addCategory(parent.matchers.copyWithUpdatedMatcherRootPrefix("\$.$partName")) | ||
generators.addGenerators(parent.generators) | ||
builder.addTextBody(partName, part.body.toString(), org.apache.hc.core5.http.ContentType.APPLICATION_JSON) | ||
return this | ||
} | ||
|
||
/** | ||
* Adds the contents of a string as a text part with the given name | ||
*/ | ||
@JvmOverloads | ||
fun textPart( | ||
partName: String, | ||
value: String, | ||
contentType: String? = null | ||
): MultipartBuilder { | ||
val ct = if (contentType.isNullOrEmpty()) { | ||
null | ||
} else { | ||
org.apache.hc.core5.http.ContentType.create(contentType) | ||
} | ||
builder.addTextBody(partName, value, ct) | ||
return this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters