Skip to content

Commit

Permalink
feat: Update Groovy message builder to be able to create a message wi…
Browse files Browse the repository at this point in the history
…th a NULL body #1637
  • Loading branch information
rholshausen committed Jan 4, 2023
1 parent 49cacec commit 09bb4af
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class PactMessageBuilder extends GroovyBuilder {
* @param metaData
*/
PactMessageBuilder withMetaData(Map metadata) {
this.withMetadata(metadata)
}

/**
* Metadata attached to the message
* @param metaData
*/
PactMessageBuilder withMetadata(Map metadata) {
if (messages.empty) {
throw new InvalidPactException('expectsToReceive is required before withMetaData')
}
Expand All @@ -90,7 +98,7 @@ class PactMessageBuilder extends GroovyBuilder {
* - contentType: optional content type of the message
* - prettyPrint: if the message content should be pretty printed
*/
PactMessageBuilder withContent(Map options = [:], Closure closure) {
PactMessageBuilder withContent(Map options = [:], def value) {
if (messages.empty) {
throw new InvalidPactException('expectsToReceive is required before withContent')
}
Expand All @@ -103,11 +111,16 @@ class PactMessageBuilder extends GroovyBuilder {
contentType = messages.last().metadata.contentType
}

def body = new PactBodyBuilder(mimetype: contentType, prettyPrintBody: options.prettyPrint)
closure.delegate = body
closure.call()
messages.last().contents = OptionalBody.body(body.body.bytes, new ContentType(contentType))
messages.last().matchingRules.addCategory(body.matchers)
if (value instanceof Closure) {
Closure closure = value as Closure
def body = new PactBodyBuilder(mimetype: contentType, prettyPrintBody: options.prettyPrint)
closure.delegate = body
closure.call()
messages.last().contents = OptionalBody.body(body.body.bytes, new ContentType(contentType))
messages.last().matchingRules.addCategory(body.matchers)
} else {
messages.last().contents = OptionalBody.body(value.toString().bytes, new ContentType(contentType))
}

this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,57 @@ class PactMessageBuilderSpec extends Specification {
assert message.jsonContents
}
}

def 'receiving a message with a NULL body'() {
given:
builder {
expectsToReceive 'a confirmation delete message'
withMetadata(contentType: 'application/json', messageId: '12345678')
withContent(null)
}

when:
builder.run { Message message ->
def content = new JsonSlurper().parse(message.contentsAsBytes())
assert content == null
}

then:
true
}

def 'receiving a message with an empty body'() {
given:
builder {
expectsToReceive 'a confirmation delete message'
withMetadata(contentType: 'application/json', messageId: '12345678')
withContent { }
}

when:
builder.run { Message message ->
def content = new JsonSlurper().parse(message.contentsAsBytes())
assert content.size() == 0
}

then:
true
}

def 'receiving a message with a missing body'() {
given:
builder {
expectsToReceive 'a confirmation delete message'
withMetadata(messageId: '12345678')
}

when:
builder.run { Message message ->
assert message.contentsAsBytes().size() == 0
assert message.metadata.messageId == '12345678'
}

then:
true
}
}

0 comments on commit 09bb4af

Please sign in to comment.