mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

Set utf-8 as default encoding

Open Bessonov opened this issue 5 years ago • 6 comments

Describe the issue This is a follow up of https://github.com/mock-server/mockserver/issues/797 . It seems like https://github.com/mock-server/mockserver/commit/7408afbb03523736cf050a8d98d81589fcc4415b do following:

  • application/json; charset=utf-8 is set: works as expected, utf8 encoding is used
  • no Content-Type header is set: works as expected, utf8 encoding is used
  • application/json is set: utf8 isn't used. ISO-8859-1 is still used

From my point of view, the last one should use utf8.

What you are trying to do Mock a request with utf8 encoding and Content-Type: application/json header. Mock Server stores it in wrong encoding.

MockServer version 5.11.2

To Reproduce Steps to reproduce the issue:

  • Send an utf8 encoded request with Content-Type: application/json header
  • Get all recorded requests from mock server

Expected behaviour utf8 should be assumed even without explicit ; charset=utf-8 part the Content-Type header

MockServer Log

Bessonov avatar Dec 17 '20 11:12 Bessonov

I'm facing the same problem: the unittests on my project were continuously failing because of this.

fr4ncidir avatar Jan 13 '21 12:01 fr4ncidir

:+1: Don't see any reason for not using utf-8 as default encoding with application/json in Content-Type header set.

almostvoidcrusade avatar Feb 12 '21 08:02 almostvoidcrusade

This problem is especially painful when using mock-server with spring. The common restTemplate media type is APPLICATION_JSON, so you have to change it to deprecated APPLICATION_JSON_UTF8. But it causes new problems: header in Spring is 'application/json;charset=UTF-8" while in the mock server - 'application/json; charset=utf-8" so you are not able to validate it using request().withContentType(...) expectation.

Anshelen avatar Mar 23 '21 10:03 Anshelen

Still have this issue in dockerized mock-server also.

prakafox avatar Dec 20 '21 14:12 prakafox

I'm pretty sure this is the correct standard for HTTP according to the exact specification but I can double check and potentially change it to the way you suggested as it's more practical.

jamesdbloom avatar Feb 06 '22 19:02 jamesdbloom

Please take a look

https://datatracker.ietf.org/doc/html/rfc8259#section-11

Note: No "charset" parameter is defined for this registration. Adding one really has no effect on compliant recipients.

https://datatracker.ietf.org/doc/html/rfc8259#section-8.1

JSON text exchanged between systems that are not part of a closed ecosystem MUST be encoded using UTF-8

https://www.rfc-editor.org/errata/eid5853 (Notes section)

vrobertson avatar Feb 07 '22 07:02 vrobertson