Skip to content

Commit 3010a10

Browse files
committed
feat: Add support for JSONObject with MessagePactBuilder #1669
1 parent 2a9ac69 commit 3010a10

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

consumer/junit/src/test/groovy/au/com/dius/pact/consumer/junit/MessagePactBuilderSpec.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import au.com.dius.pact.core.model.generators.DateTimeGenerator
99
import au.com.dius.pact.core.model.messaging.Message
1010
import groovy.json.JsonSlurper
1111
import groovy.xml.XmlParser
12+
import org.json.JSONObject
1213
import spock.lang.Issue
1314
import spock.lang.Specification
1415
import spock.lang.Unroll
@@ -218,4 +219,22 @@ class MessagePactBuilderSpec extends Specification {
218219
message.contents.valueAsString() == 'a=b&c=d'
219220
message.contents.contentType.toString() == 'application/x-www-form-urlencoded'
220221
}
222+
223+
@Issue('#1669')
224+
def 'support content with JSONObject'() {
225+
given:
226+
JSONObject jsonObject = new JSONObject().put('JSON', 'Hello, World!')
227+
228+
when:
229+
def pact = new MessagePactBuilder()
230+
.consumer('MessagePactBuilderSpec')
231+
.expectsToReceive('a message with text contents')
232+
.withContent(jsonObject)
233+
.toPact()
234+
Message message = pact.interactions.first()
235+
236+
then:
237+
message.contents.valueAsString() == '{"JSON":"Hello, World!"}'
238+
message.contents.contentType.toString() == 'application/json'
239+
}
221240
}

consumer/src/main/kotlin/au/com/dius/pact/consumer/MessagePactBuilder.kt

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import au.com.dius.pact.core.model.generators.Category
1818
import au.com.dius.pact.core.model.messaging.Message
1919
import au.com.dius.pact.core.model.messaging.MessagePact
2020
import au.com.dius.pact.core.model.v4.MessageContents
21+
import org.json.JSONObject
2122
import java.util.Locale
2223

2324
/**
@@ -163,7 +164,7 @@ class MessagePactBuilder @JvmOverloads constructor(
163164
*/
164165
fun withContent(body: DslPart): MessagePactBuilder {
165166
if (messages.isEmpty()) {
166-
throw InvalidPactException("expectsToReceive is required before withMetaData")
167+
throw InvalidPactException("expectsToReceive is required before withContent")
167168
}
168169

169170
val message = messages.last()
@@ -197,7 +198,7 @@ class MessagePactBuilder @JvmOverloads constructor(
197198
*/
198199
fun withContent(xmlBuilder: PactXmlBuilder): MessagePactBuilder {
199200
if (messages.isEmpty()) {
200-
throw InvalidPactException("expectsToReceive is required before withMetaData")
201+
throw InvalidPactException("expectsToReceive is required before withContent")
201202
}
202203

203204
val message = messages.last()
@@ -231,7 +232,7 @@ class MessagePactBuilder @JvmOverloads constructor(
231232
@JvmOverloads
232233
fun withContent(contents: String, contentType: String = "text/plain"): MessagePactBuilder {
233234
if (messages.isEmpty()) {
234-
throw InvalidPactException("expectsToReceive is required before withMetaData")
235+
throw InvalidPactException("expectsToReceive is required before withContent")
235236
}
236237

237238
val message = messages.last()
@@ -247,6 +248,35 @@ class MessagePactBuilder @JvmOverloads constructor(
247248
return this
248249
}
249250

251+
/**
252+
* Adds the JSON body as the message content
253+
*/
254+
fun withContent(json: JSONObject): MessagePactBuilder {
255+
if (messages.isEmpty()) {
256+
throw InvalidPactException("expectsToReceive is required before withContent")
257+
}
258+
259+
val message = messages.last()
260+
val metadata = message.contents.metadata.toMutableMap()
261+
val contentTypeEntry = metadata.entries.find {
262+
it.key.lowercase() == "contenttype" || it.key.lowercase() == "content-type"
263+
}
264+
265+
var contentType = ContentType.JSON
266+
if (contentTypeEntry == null) {
267+
metadata["contentType"] = contentType.toString()
268+
} else {
269+
contentType = ContentType(contentTypeEntry.value.toString())
270+
}
271+
272+
message.contents = message.contents.copy(
273+
contents = OptionalBody.body(json.toString().toByteArray(contentType.asCharset()), contentType),
274+
metadata = metadata
275+
)
276+
277+
return this
278+
}
279+
250280
/**
251281
* Terminates the DSL and builds a pact to represent the interactions
252282
*/

0 commit comments

Comments
 (0)