Skip to content

Commit

Permalink
fix: correct how the bodies are presisted as per the spec #1658
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jan 30, 2023
1 parent 4c74ef9 commit 3e63af6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun bodyFromJson(field: String, json: JsonValue, headers: Map<String, Any>): 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 ""
}
Expand All @@ -59,7 +59,7 @@ fun bodyFromJson(field: String, json: JsonValue, headers: Map<String, Any>): 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,15 @@ class PactWriterSpec extends Specification {
| "contents": {
| "content": "this is a message",
| "contentType": "application/json",
| "encoded": "json"
| "encoded": false
| }
| },
| "response": [
| {
| "contents": {
| "content": "this is a response",
| "contentType": "application/json",
| "encoded": "json"
| "encoded": false
| }
| }
| ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 3e63af6

Please sign in to comment.