@@ -8,6 +8,7 @@ import au.com.dius.pact.core.model.generators.Generators
8
8
import au.com.dius.pact.core.model.matchingrules.MatchingRules
9
9
import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl
10
10
import au.com.dius.pact.core.support.Json
11
+ import au.com.dius.pact.core.support.isNotEmpty
11
12
import com.github.salomonbrys.kotson.array
12
13
import com.github.salomonbrys.kotson.obj
13
14
import com.google.gson.JsonObject
@@ -26,18 +27,22 @@ class Message @JvmOverloads constructor(
26
27
var contents : OptionalBody = OptionalBody .missing(),
27
28
var matchingRules : MatchingRules = MatchingRulesImpl (),
28
29
var generators : Generators = Generators (),
29
- var metaData : Map <String , Any ?> = mapOf (),
30
+ var metaData : MutableMap <String , Any ?> = mutableMapOf (),
30
31
override val interactionId : String? = null
31
32
) : Interaction {
32
33
33
34
fun contentsAsBytes () = contents.orEmpty()
34
35
35
36
fun contentsAsString () = contents.valueAsString()
36
37
37
- fun getContentType () = Companion .getContentType(metaData)
38
+ fun getContentType () = if (contents.isPresent() && contents.contentType.contentType.isNotEmpty()) {
39
+ contents.contentType.contentType
40
+ } else {
41
+ getContentType(metaData)
42
+ }
38
43
39
44
@Deprecated(" Use the content type associated with the message body" )
40
- fun getParsedContentType () = parseContentType(this .getContentType())
45
+ fun getParsedContentType () = parseContentType(this .getContentType() ? : " " )
41
46
42
47
override fun toMap (pactSpecVersion : PactSpecVersion ): Map <String , Any ?> {
43
48
val map: MutableMap <String , Any ?> = mutableMapOf (
@@ -47,7 +52,7 @@ class Message @JvmOverloads constructor(
47
52
if (! contents.isMissing()) {
48
53
map[" contents" ] = when {
49
54
isJsonContents() -> {
50
- val json = JsonParser ().parse (contents.valueAsString())
55
+ val json = JsonParser .parseString (contents.valueAsString())
51
56
if (json.isJsonPrimitive && json.asJsonPrimitive.isString) {
52
57
contents.valueAsString()
53
58
} else {
@@ -71,19 +76,23 @@ class Message @JvmOverloads constructor(
71
76
72
77
private fun isJsonContents (): Boolean {
73
78
return if (contents.isPresent()) {
74
- val mimeType = contents.contentType.asMimeType()
75
- isJson(mimeType)
79
+ val contentType = getContentType(metaData)
80
+ if (contentType.isNotEmpty()) {
81
+ isJson(contentType)
82
+ } else {
83
+ isJson(contents.contentType.asMimeType())
84
+ }
76
85
} else {
77
86
false
78
87
}
79
88
}
80
89
81
90
fun formatContents (): String {
82
91
return if (contents.isPresent()) {
83
- val mimeType = contents.contentType.asMimeType()
92
+ val contentType = getContentType(metaData) ? : contents.contentType.asMimeType()
84
93
when {
85
- isJson(mimeType ) -> Json .gsonPretty.toJson(JsonParser ().parse (contents.valueAsString()))
86
- isOctetStream(mimeType ) -> Base64 .encodeBase64String(contentsAsBytes())
94
+ isJson(contentType ) -> Json .gsonPretty.toJson(JsonParser .parseString (contents.valueAsString()))
95
+ isOctetStream(contentType ) -> Base64 .encodeBase64String(contentsAsBytes())
87
96
else -> contents.valueAsString()
88
97
}
89
98
} else {
@@ -128,12 +137,13 @@ class Message @JvmOverloads constructor(
128
137
}
129
138
130
139
fun withMetaData (metadata : Map <String , Any >): Message {
131
- this .metaData = metadata
140
+ this .metaData = metadata.toMutableMap()
132
141
return this
133
142
}
134
143
135
144
companion object : KLogging () {
136
145
const val JSON = " application/json"
146
+ const val TEXT = " text/plain"
137
147
138
148
/* *
139
149
* Builds a message from a Map
@@ -171,23 +181,28 @@ class Message @JvmOverloads constructor(
171
181
else Generators ()
172
182
173
183
return Message (Json .toString(json[" description" ]), providerStates,
174
- contents, matchingRules, generators, metaData, Json .toString(json[" _id" ]))
184
+ contents, matchingRules, generators, metaData.toMutableMap() , Json .toString(json[" _id" ]))
175
185
}
176
186
177
187
@Deprecated(" Use the content type associated with the message body" )
178
- private fun parseContentType (contentType : String ): ContentType ? {
179
- return try {
180
- ContentType .parse(contentType)
181
- } catch (e: RuntimeException ) {
182
- logger.debug(e) { " Failed to parse content type '$contentType '" }
188
+ private fun parseContentType (contentType : String? ): ContentType ? {
189
+ return if (contentType.isNotEmpty()) {
190
+ try {
191
+ ContentType .parse(contentType)
192
+ } catch (e: RuntimeException ) {
193
+ logger.debug(e) { " Failed to parse content type '$contentType '" }
194
+ null
195
+ }
196
+ } else {
183
197
null
184
198
}
185
199
}
186
200
187
- fun getContentType (metaData : Map <String , Any ?>): String {
188
- return metaData.entries.find {
201
+ @Deprecated(" Use the content type associated with the message body" )
202
+ fun getContentType (metaData : Map <String , Any ?>): String? {
203
+ return parseContentType(metaData.entries.find {
189
204
it.key.toLowerCase() == " contenttype" || it.key.toLowerCase() == " content-type"
190
- }?.value?.toString() ? : JSON
205
+ }?.value?.toString())?.mimeType
191
206
}
192
207
193
208
private fun isJson (contentType : String? ) =
0 commit comments