@@ -204,12 +204,17 @@ public PactDslResponse bodyWithSingleQuotes(String body, ContentType contentType
204
204
* @param body Response body in JSON form
205
205
*/
206
206
public PactDslResponse body (JSONObject body ) {
207
- this .responseBody = OptionalBody .body (body .toString ().getBytes (),
208
- au .com .dius .pact .core .model .ContentType .Companion .getJSON ());
209
- if (!responseHeaders .containsKey (CONTENT_TYPE )) {
210
- matchHeader (CONTENT_TYPE , DEFAULT_JSON_CONTENT_TYPE_REGEX , ContentType .APPLICATION_JSON .toString ());
211
- }
212
- return this ;
207
+ if (!responseHeaders .containsKey (CONTENT_TYPE )) {
208
+ matchHeader (CONTENT_TYPE , DEFAULT_JSON_CONTENT_TYPE_REGEX , ContentType .APPLICATION_JSON .toString ());
209
+ this .responseBody = OptionalBody .body (body .toString ().getBytes ());
210
+ } else {
211
+ String contentType = responseHeaders .get (CONTENT_TYPE ).get (0 );
212
+ ContentType ct = ContentType .parse (contentType );
213
+ Charset charset = ct .getCharset () != null ? ct .getCharset () : Charset .defaultCharset ();
214
+ this .responseBody = OptionalBody .body (body .toString ().getBytes (charset ),
215
+ new au .com .dius .pact .core .model .ContentType (contentType ));
216
+ }
217
+ return this ;
213
218
}
214
219
215
220
/**
@@ -218,25 +223,33 @@ public PactDslResponse body(JSONObject body) {
218
223
* @param body Response body built using the Pact body DSL
219
224
*/
220
225
public PactDslResponse body (DslPart body ) {
221
- DslPart parent = body .close ();
226
+ DslPart parent = body .close ();
222
227
223
- if (parent instanceof PactDslJsonRootValue ) {
224
- ((PactDslJsonRootValue )parent ).setEncodeJson (true );
225
- }
228
+ if (parent instanceof PactDslJsonRootValue ) {
229
+ ((PactDslJsonRootValue )parent ).setEncodeJson (true );
230
+ }
226
231
227
- responseMatchers .addCategory (parent .getMatchers ());
228
- responseGenerators .addGenerators (parent .generators );
229
- if (parent .getBody () != null ) {
230
- responseBody = OptionalBody .body (parent .getBody ().toString ().getBytes (),
231
- au .com .dius .pact .core .model .ContentType .Companion .getJSON ());
232
- } else {
233
- responseBody = OptionalBody .nullBody ();
234
- }
232
+ responseMatchers .addCategory (parent .getMatchers ());
233
+ responseGenerators .addGenerators (parent .generators );
234
+
235
+ Charset charset = Charset .defaultCharset ();
236
+ String contentType = ContentType .APPLICATION_JSON .toString ();
237
+ if (!responseHeaders .containsKey (CONTENT_TYPE )) {
238
+ matchHeader (CONTENT_TYPE , DEFAULT_JSON_CONTENT_TYPE_REGEX , contentType );
239
+ } else {
240
+ contentType = responseHeaders .get (CONTENT_TYPE ).get (0 );
241
+ ContentType ct = ContentType .parse (contentType );
242
+ charset = ct .getCharset () != null ? ct .getCharset () : Charset .defaultCharset ();
243
+ }
235
244
236
- if (!responseHeaders .containsKey (CONTENT_TYPE )) {
237
- matchHeader (CONTENT_TYPE , DEFAULT_JSON_CONTENT_TYPE_REGEX , ContentType .APPLICATION_JSON .toString ());
238
- }
239
- return this ;
245
+ if (parent .getBody () != null ) {
246
+ responseBody = OptionalBody .body (parent .getBody ().toString ().getBytes (charset ),
247
+ new au .com .dius .pact .core .model .ContentType (contentType ));
248
+ } else {
249
+ responseBody = OptionalBody .nullBody ();
250
+ }
251
+
252
+ return this ;
240
253
}
241
254
242
255
/**
@@ -245,11 +258,18 @@ public PactDslResponse body(DslPart body) {
245
258
* @param body Response body as an XML Document
246
259
*/
247
260
public PactDslResponse body (Document body ) throws TransformerException {
261
+ if (!responseHeaders .containsKey (CONTENT_TYPE )) {
262
+ responseHeaders .put (CONTENT_TYPE , Collections .singletonList (ContentType .APPLICATION_XML .toString ()));
248
263
responseBody = OptionalBody .body (ConsumerPactBuilder .xmlToString (body ).getBytes ());
249
- if (!responseHeaders .containsKey (CONTENT_TYPE )) {
250
- responseHeaders .put (CONTENT_TYPE , Collections .singletonList (ContentType .APPLICATION_XML .toString ()));
251
- }
252
- return this ;
264
+ } else {
265
+ String contentType = responseHeaders .get (CONTENT_TYPE ).get (0 );
266
+ ContentType ct = ContentType .parse (contentType );
267
+ Charset charset = ct .getCharset () != null ? ct .getCharset () : Charset .defaultCharset ();
268
+ responseBody = OptionalBody .body (ConsumerPactBuilder .xmlToString (body ).getBytes (charset ),
269
+ new au .com .dius .pact .core .model .ContentType (contentType ));
270
+ }
271
+
272
+ return this ;
253
273
}
254
274
255
275
/**
0 commit comments