diff --git a/consumer/junit/src/test/groovy/au/com/dius/pact/consumer/junit/MessagePactBuilderSpec.groovy b/consumer/junit/src/test/groovy/au/com/dius/pact/consumer/junit/MessagePactBuilderSpec.groovy index d67ff4d672..1d0dae0edb 100644 --- a/consumer/junit/src/test/groovy/au/com/dius/pact/consumer/junit/MessagePactBuilderSpec.groovy +++ b/consumer/junit/src/test/groovy/au/com/dius/pact/consumer/junit/MessagePactBuilderSpec.groovy @@ -205,4 +205,19 @@ class MessagePactBuilderSpec extends Specification { then: generators.categories[category] == ['$.DT': new DateTimeGenerator("yyyy-MM-dd'T'HH:mm:ss")] } + + @Issue('#1619') + def 'support non-json text formats'() { + when: + def pact = new MessagePactBuilder() + .consumer('MessagePactBuilderSpec') + .expectsToReceive('a message with text contents') + .withContent('a=b&c=d', 'application/x-www-form-urlencoded') + .toPact() + Message message = pact.interactions.first() + + then: + message.contents.valueAsString() == 'a=b&c=d' + message.contents.contentType.toString() == 'application/x-www-form-urlencoded' + } } diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/MessagePactBuilder.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/MessagePactBuilder.kt index a0e92f384e..ef24a58d28 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/MessagePactBuilder.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/MessagePactBuilder.kt @@ -17,11 +17,11 @@ import au.com.dius.pact.core.model.messaging.MessagePact /** * PACT DSL builder for v3 specification */ -class MessagePactBuilder( +class MessagePactBuilder @JvmOverloads constructor( /** * The consumer for the pact. */ - private var consumer: Consumer, + private var consumer: Consumer = Consumer(), /** * The provider for the pact. @@ -38,7 +38,6 @@ class MessagePactBuilder( */ private var messages: MutableList = mutableListOf() ) { - /** * Name the provider that the consumer has a pact with. * @@ -198,6 +197,24 @@ class MessagePactBuilder( return this } + /** + * Adds the text as the message contents + */ + @JvmOverloads + fun withContent(contents: String, contentType: String = "text/plain"): MessagePactBuilder { + if (messages.isEmpty()) { + throw InvalidPactException("expectsToReceive is required before withMetaData") + } + + val message = messages.last() + message.metaData["contentType"] = contentType + + val ct = ContentType(contentType) + message.contents = OptionalBody.body(contents.toByteArray(ct.asCharset()), ct) + + return this + } + /** * Convert this builder into a Pact */