Skip to content

Commit

Permalink
feat: support JSON encoded bodies with V4 Pact files
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jan 31, 2023
1 parent 3e63af6 commit f4d0171
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,27 @@ data class OptionalBody @JvmOverloads constructor(
return when (state) {
State.PRESENT -> {
if (value!!.isNotEmpty()) {
if (contentTypeHint == ContentTypeHint.BINARY || contentType.isBinaryType()) {
if (contentType.isJson()) {
if (contentTypeHint == ContentTypeHint.BINARY) {
mapOf(
"content" to valueAsString(),
"contentType" to contentType.toString(),
"encoded" to "JSON"
)
} else {
mapOf(
"content" to JsonParser.parseString(valueAsString()),
"contentType" to contentType.toString(),
"encoded" to false
)
}
} else if (contentTypeHint == ContentTypeHint.BINARY || contentType.isBinaryType()) {
mapOf(
"content" to valueAsBase64(),
"contentType" to contentType.toString(),
"encoded" to "base64",
"contentTypeHint" to contentTypeHint.name
)
} else if (contentType.isJson()) {
mapOf(
"content" to JsonParser.parseString(valueAsString()),
"contentType" to contentType.toString(),
"encoded" to false
)
} else {
mapOf(
"content" to valueAsString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ class OptionalBodySpec extends Specification {
body.toV4Format() == v4Format

where:
body | v4Format
OptionalBody.missing() | [:]
OptionalBody.body(''.bytes, ContentType.UNKNOWN) | [content: '']
OptionalBody.body('{}'.bytes, ContentType.UNKNOWN) | [content: new JsonValue.Object(), contentType: 'application/json', encoded: false]
OptionalBody.body('{}'.bytes, ContentType.JSON) | [content: new JsonValue.Object(), contentType: 'application/json', encoded: false]
OptionalBody.body([0xff, 0xd8, 0xff, 0xe0] as byte[], new ContentType('image/jpeg')) | [content: '/9j/4A==', contentType: 'image/jpeg', encoded: 'base64', contentTypeHint: 'DEFAULT']
body | v4Format
OptionalBody.missing() | [:]
OptionalBody.body(''.bytes, ContentType.UNKNOWN) | [content: '']
OptionalBody.body('{}'.bytes, ContentType.UNKNOWN) | [content: new JsonValue.Object(), contentType: 'application/json', encoded: false]
OptionalBody.body('{}'.bytes, ContentType.JSON) | [content: new JsonValue.Object(), contentType: 'application/json', encoded: false]
OptionalBody.body([0xff, 0xd8, 0xff, 0xe0] as byte[], new ContentType('image/jpeg')) | [content: '/9j/4A==', contentType: 'image/jpeg', encoded: 'base64', contentTypeHint: 'DEFAULT']
OptionalBody.body('kjlkjlkjkl'.bytes, new ContentType('application/other'), ContentTypeHint.BINARY) | [content: 'a2psa2psa2prbA==', contentType: 'application/other', encoded: 'base64', contentTypeHint: 'BINARY']
OptionalBody.body('{}'.bytes, ContentType.JSON, ContentTypeHint.BINARY) | [content: '{}', contentType: 'application/json', encoded: 'JSON']
}

private static OptionalBody bodyFromFile(String file) {
Expand Down

0 comments on commit f4d0171

Please sign in to comment.