Fix the charset
[fwx] / src / fwx / __init__.py
index 097de12..2cbe707 100644 (file)
@@ -132,8 +132,8 @@ class Response(_Response):
         content_type = kwargs.pop('content_type')
         assert isinstance(content_type, str)
 
-        extra_headers = kwargs.pop('extra_headers', ())
-        assert isinstance(extra_headers, tuple)
+        extra_headers = kwargs.pop('extra_headers', {})
+        assert isinstance(extra_headers, dict)
 
         assert len(kwargs) == 0
 
@@ -147,9 +147,25 @@ class Response(_Response):
 
     @property
     def headers(self):
-        return (
-            ('Content-Type', self.content_type),
-        )
+        # Start with the defaults
+        result = {
+            'X-Content-Type-Options': 'nosniff',
+        }
+
+        result = {**result, **(self.extra_headers)}
+
+        builtin_headers = {
+            'Content-Type': self.content_type,
+        }
+
+        for key, value in builtin_headers.items():
+            if key in result:
+                raise Exception('Header "{}" defined twice'.format(key))
+            else:
+                result[key] = value
+
+        return tuple(sorted(result.items()))
+
 
 class HTMLResponse(Response):
     def __new__(cls, content, **kwargs):
@@ -158,7 +174,7 @@ class HTMLResponse(Response):
         return super().__new__(
             cls,
             content,
-            content_type='text/html',
+            content_type='text/html; charset=utf-8',
             **kwargs,
         )
 
@@ -170,7 +186,7 @@ class JSONResponse(Response):
         self = super().__new__(
             cls,
             content=json.dumps(content_json),
-            content_type='application/json',
+            content_type='application/json; charset=utf-8',
             **kwargs,
         )
         self.content_json = content_json
@@ -183,7 +199,7 @@ class TextResponse(Response):
         return super().__new__(
             cls,
             content,
-            content_type='text/plain',
+            content_type='text/plain; charset=utf-8',
             **kwargs,
         )