diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt index e50f34554..60f10c366 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt @@ -179,7 +179,7 @@ data class OptionalBody @JvmOverloads constructor( mapOf( "content" to JsonParser.parseString(valueAsString()), "contentType" to contentType.toString(), - "encoded" to "json" + "encoded" to false ) } else { mapOf( diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt index 28de57ea2..5cd76f349 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt @@ -48,7 +48,7 @@ fun bodyFromJson(field: String, json: JsonValue, headers: Map): Opt val (encoded, encoding) = if (jsonBody.has("encoded")) { when (val encodedValue = jsonBody["encoded"]) { - is JsonValue.StringValue -> true to encodedValue.toString() + is JsonValue.StringValue -> true to encodedValue.toString().lowercase() JsonValue.True -> true to "base64" else -> false to "" } @@ -59,7 +59,7 @@ fun bodyFromJson(field: String, json: JsonValue, headers: Map): Opt val bodyBytes = if (encoded) { when (encoding) { "base64" -> Base64.getDecoder().decode(Json.toString(jsonBody["content"])) - "json" -> jsonBody["content"].serialise().toByteArray(contentType.asCharset()) + "json" -> Json.toString(jsonBody["content"]).toByteArray(contentType.asCharset()) else -> { logger.warn { "Unrecognised body encoding scheme '$encoding', will use the raw body" } Json.toString(jsonBody["content"]).toByteArray(contentType.asCharset()) diff --git a/core/model/src/test/groovy/au/com/dius/pact/core/model/OptionalBodySpec.groovy b/core/model/src/test/groovy/au/com/dius/pact/core/model/OptionalBodySpec.groovy index 0dbbb72dc..ba5f9fd7f 100644 --- a/core/model/src/test/groovy/au/com/dius/pact/core/model/OptionalBodySpec.groovy +++ b/core/model/src/test/groovy/au/com/dius/pact/core/model/OptionalBodySpec.groovy @@ -163,8 +163,8 @@ class OptionalBodySpec extends Specification { body | v4Format OptionalBody.missing() | [:] OptionalBody.body(''.bytes, ContentType.UNKNOWN) | [content: ''] - OptionalBody.body('{}'.bytes, ContentType.UNKNOWN) | [content: new JsonValue.Object(), contentType: 'application/json', encoded: 'json'] - OptionalBody.body('{}'.bytes, ContentType.JSON) | [content: new JsonValue.Object(), contentType: 'application/json', encoded: 'json'] + 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'] } diff --git a/core/model/src/test/groovy/au/com/dius/pact/core/model/PactWriterSpec.groovy b/core/model/src/test/groovy/au/com/dius/pact/core/model/PactWriterSpec.groovy index d4d43b005..f5bd3a520 100644 --- a/core/model/src/test/groovy/au/com/dius/pact/core/model/PactWriterSpec.groovy +++ b/core/model/src/test/groovy/au/com/dius/pact/core/model/PactWriterSpec.groovy @@ -345,7 +345,7 @@ class PactWriterSpec extends Specification { | "contents": { | "content": "this is a message", | "contentType": "application/json", - | "encoded": "json" + | "encoded": false | } | }, | "response": [ @@ -353,7 +353,7 @@ class PactWriterSpec extends Specification { | "contents": { | "content": "this is a response", | "contentType": "application/json", - | "encoded": "json" + | "encoded": false | } | } | ], diff --git a/core/model/src/test/groovy/au/com/dius/pact/core/model/V4PactKtSpec.groovy b/core/model/src/test/groovy/au/com/dius/pact/core/model/V4PactKtSpec.groovy index 377493b5b..764422473 100644 --- a/core/model/src/test/groovy/au/com/dius/pact/core/model/V4PactKtSpec.groovy +++ b/core/model/src/test/groovy/au/com/dius/pact/core/model/V4PactKtSpec.groovy @@ -36,8 +36,11 @@ class V4PactKtSpec extends Specification { new JsonValue.Object([:]) | JsonValue.False.INSTANCE | 'application/json' | OptionalBody.body('{}'.bytes, ContentType.JSON) new JsonValue.Object([:]) | JsonValue.False.INSTANCE | '' | OptionalBody.body('{}'.bytes, ContentType.JSON) new JsonValue.StringValue('ABC') | JsonValue.False.INSTANCE | 'application/json' | OptionalBody.body('"ABC"'.bytes, ContentType.JSON) - new JsonValue.StringValue('ABC') | new JsonValue.StringValue('json') | 'application/json' | OptionalBody.body('"ABC"'.bytes, ContentType.JSON) + new JsonValue.StringValue('\"ABC\"') | JsonValue.False.INSTANCE | 'application/json' | OptionalBody.body('"\\"ABC\\""'.bytes, ContentType.JSON) + new JsonValue.StringValue('\"ABC\"') | new JsonValue.StringValue('json') | 'application/json' | OptionalBody.body('"ABC"'.bytes, ContentType.JSON) + new JsonValue.StringValue('\"ABC\"') | new JsonValue.StringValue('JSON') | 'application/json' | OptionalBody.body('"ABC"'.bytes, ContentType.JSON) new JsonValue.StringValue('IkFCQyI=') | JsonValue.True.INSTANCE | 'application/json' | OptionalBody.body('"ABC"'.bytes, ContentType.JSON) new JsonValue.StringValue('IkFCQyI=') | new JsonValue.StringValue('base64') | 'application/json' | OptionalBody.body('"ABC"'.bytes, ContentType.JSON) + new JsonValue.StringValue('IkFCQyI=') | new JsonValue.StringValue('BASE64') | 'application/json' | OptionalBody.body('"ABC"'.bytes, ContentType.JSON) } }