From a1cc214bbef7330c78268f9385adc1c80d14e75c Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Sun, 13 Mar 2016 09:34:32 -0400 Subject: [PATCH] ContentMixin: Set the Vary header. This lets any transparent caches that our responses will vary based on the Accept header. Without this, caches will assume that a cached response can be used even if the Accept header differs. --- docs/history.rst | 9 ++++++++- sprockets/mixins/mediatype/content.py | 1 + tests.py | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/history.rst b/docs/history.rst index aaf132c..1d94bca 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -1,6 +1,10 @@ Version History =============== +`Next Release`_ +--------------- +- Set the _`Vary` header if we are setting the content type. + `2.0.1`_ (29 Feb 2016) ---------------------- - Removed deprecation wrapper since it seems to cause really interesting @@ -38,10 +42,13 @@ Version History --------------------- - Initial Release -.. _Next Release: https://github.com/sprockets/sprockets.http/compare/2.0.0...HEAD +.. _Next Release: https://github.com/sprockets/sprockets.http/compare/2.0.1...HEAD +.. _2.0.1: https://github.com/sprockets/sprockets.http/compare/2.0.0...2.0.1 .. _2.0.0: https://github.com/sprockets/sprockets.http/compare/1.0.4...2.0.0 .. _1.0.4: https://github.com/sprockets/sprockets.http/compare/1.0.3...1.0.4 .. _1.0.3: https://github.com/sprockets/sprockets.http/compare/1.0.2...1.0.3 .. _1.0.2: https://github.com/sprockets/sprockets.http/compare/1.0.1...1.0.2 .. _1.0.1: https://github.com/sprockets/sprockets.http/compare/1.0.0...1.0.1 .. _1.0.0: https://github.com/sprockets/sprockets.http/compare/0.0.0...1.0.0 + +.. _Vary: http://tools.ietf.org/html/rfc7234#section-4.1 diff --git a/sprockets/mixins/mediatype/content.py b/sprockets/mixins/mediatype/content.py index 73dc0ef..430552a 100644 --- a/sprockets/mixins/mediatype/content.py +++ b/sprockets/mixins/mediatype/content.py @@ -303,4 +303,5 @@ class ContentMixin(object): content_type, data_bytes = handler.to_bytes(body) if set_content_type: self.set_header('Content-Type', content_type) + self.add_header('Vary', 'Accept') self.write(data_bytes) diff --git a/tests.py b/tests.py index 76cb2a4..192f4da 100644 --- a/tests.py +++ b/tests.py @@ -95,6 +95,12 @@ class SendResponseTests(testing.AsyncHTTPTestCase): self.assertEqual(response.headers['Content-Type'], 'application/json; charset="utf-8"') + def test_that_vary_header_is_set(self): + response = self.fetch('/', method='POST', body=umsgpack.packb({}), + headers={'Content-Type': 'application/msgpack'}) + self.assertEqual(response.code, 200) + self.assertEqual(response.headers['Vary'], 'Accept') + class GetRequestBodyTests(testing.AsyncHTTPTestCase):