Repository: falconry/falcon
Pull request #1476 (2019-03-08 00:21:49)

refactor(default_serialize_error): Do not include charset param in media type

codecov[bot]: # [Codecov](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=h1) Report
> Merging [#1476](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=desc) into [master](https://codecov.io/gh/falconry/falcon/commit/f11aa48dc917fb57facd64c534bd9c7551eaec8e?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/falconry/falcon/pull/1476/graphs/tree.svg?width=650&token=zOtkfNQ3h6&height=150&src=pr)](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1476   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          39      39           
  Lines        2613    2613           
  Branches      416     416           
======================================
  Hits         2613    2613
```


| [Impacted Files](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [falcon/api\_helpers.py](https://codecov.io/gh/falconry/falcon/pull/1476/diff?src=pr&el=tree#diff-ZmFsY29uL2FwaV9oZWxwZXJzLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=footer). Last update [f11aa48...2862b8f](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=h1) Report
> Merging [#1476](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=desc) into [master](https://codecov.io/gh/falconry/falcon/commit/f11aa48dc917fb57facd64c534bd9c7551eaec8e?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/falconry/falcon/pull/1476/graphs/tree.svg?width=650&token=zOtkfNQ3h6&height=150&src=pr)](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1476   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          39      39           
  Lines        2613    2613           
  Branches      416     416           
======================================
  Hits         2613    2613
```


| [Impacted Files](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [falcon/api\_helpers.py](https://codecov.io/gh/falconry/falcon/pull/1476/diff?src=pr&el=tree#diff-ZmFsY29uL2FwaV9oZWxwZXJzLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=footer). Last update [f11aa48...2862b8f](https://codecov.io/gh/falconry/falcon/pull/1476?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 3ed7c55..5c5ada4 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -178,6 +178,11 @@ Breaking Changes
 - When setting the ``Response.etag`` header property, the value will
   now be wrapped with double-quotes (if not already present) to ensure
   compliance with RFC 7232.
+- The default error serializer no longer sets the `charset` parameter for the
+  media type returned in the Content-Type header, since UTF-8 is the default
+  encoding for both JSON and XML media types. This should not break
+  well-behaved clients, but could impact test cases in apps that
+  assert on the exact value of the Content-Type header.
 
 Changes to Supported Platforms
 ------------------------------
@@ -297,6 +302,9 @@ New & Improved
   value of the If-Match or If-None-Match headers, respectively.
 - ``API.add_error_handler()`` now supports specifying an iterable of
   exception types to match.
+- The default error serializer no longer sets the `charset` parameter for the
+  media type returned in the Content-Type header, since UTF-8 is the default
+  encoding for both JSON and XML media types.
 
 Fixed
 -----
diff --git a/docs/changes/2.0.0.rst b/docs/changes/2.0.0.rst
index 5233dea..bd1748b 100644
--- a/docs/changes/2.0.0.rst
+++ b/docs/changes/2.0.0.rst
@@ -185,6 +185,11 @@ Breaking Changes
 - When setting the :attr:`~.Response.etag` header property, the value will
   now be wrapped with double-quotes (if not already present) to ensure
   compliance with RFC 7232.
+- The default error serializer no longer sets the `charset` parameter for the
+  media type returned in the Content-Type header, since UTF-8 is the default
+  encoding for both JSON and XML media types. This should not break
+  well-behaved clients, but could impact test cases in apps that
+  assert on the exact value of the Content-Type header.
 
 Changes to Supported Platforms
 ------------------------------
@@ -324,6 +329,9 @@ New & Improved
   value of the If-Match or If-None-Match headers, respectively.
 - :meth:`~.API.add_error_handler` now supports specifying an iterable of
   exception types to match.
+- The default error serializer no longer sets the `charset` parameter for the
+  media type returned in the Content-Type header, since UTF-8 is the default
+  encoding for both JSON and XML media types.
 
 Fixed
 -----
diff --git a/falcon/api_helpers.py b/falcon/api_helpers.py
index 4745c6d..97d2f2f 100644
--- a/falcon/api_helpers.py
+++ b/falcon/api_helpers.py
@@ -131,7 +131,10 @@ def default_serialize_error(req, resp, exception):
             representation = exception.to_xml()
 
         resp.body = representation
-        resp.content_type = preferred + '; charset=UTF-8'
+
+        # NOTE(kgriffs): No need to append the charset param, since
+        #   utf-8 is the default for both JSON and XML.
+        resp.content_type = preferred
 
     resp.append_header('Vary', 'Accept')
 


Test patch:
diff --git a/tests/test_httperror.py b/tests/test_httperror.py
index 004017c..f3369ae 100644
--- a/tests/test_httperror.py
+++ b/tests/test_httperror.py
@@ -287,6 +287,7 @@ class TestHTTPError(object):
         response = client.simulate_patch('/fail')
         assert response.status == falcon.HTTP_400
         assert response.json == {'title': '400 Bad Request'}
+        assert response.headers['Content-Type'] == 'application/json'
 
     def test_no_description_xml(self, client):
         response = client.simulate_patch(
@@ -298,6 +299,7 @@ class TestHTTPError(object):
                         b'<title>400 Bad Request</title></error>')
 
         assert response.content == expected_xml
+        assert response.headers['Content-Type'] == 'application/xml'
 
     def test_client_does_not_accept_json_or_xml(self, client):
         headers = {