From 87f89d1f746d03b3f02b8f051f9aa5f012a65ba5 Mon Sep 17 00:00:00 2001 From: Eric Juma Date: Wed, 26 Jul 2017 06:15:45 -0400 Subject: [PATCH] Don't stream json Makes json data that would normally be streamed not streamed so that it can be modified. Change-Id: I9690ee1841258e4e6a4d1bfea68e6505484f2216 --- mixmatch/proxy.py | 11 ++++++++--- mixmatch/tests/unit/test_request_handler.py | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/mixmatch/proxy.py b/mixmatch/proxy.py index c24d85c..cbc2bb9 100644 --- a/mixmatch/proxy.py +++ b/mixmatch/proxy.py @@ -71,6 +71,10 @@ def get_details(method, orig_path, headers): 'path': orig_path} +def is_json_response(response): + return response.headers.get('Content-Type') == 'application/json' + + def is_token_header_key(string): return string.lower() in ['x-auth-token', 'x-service-token'] @@ -202,8 +206,9 @@ class RequestHandler(object): return resp def _finalize(self, response): - if self.stream: - text = flask.stream_with_context(stream_response(response)) + if self.stream and not is_json_response(response): + text = flask.stream_with_context( + stream_response(response)) else: text = response.text @@ -318,7 +323,7 @@ class RequestHandler(object): @utils.CachedProperty def stream(self): - return True if self.details['method'] in ['GET'] else False + return self.details['method'] == 'GET' @utils.CachedProperty def fallback_to_local(self): diff --git a/mixmatch/tests/unit/test_request_handler.py b/mixmatch/tests/unit/test_request_handler.py index a8fba2c..0238498 100644 --- a/mixmatch/tests/unit/test_request_handler.py +++ b/mixmatch/tests/unit/test_request_handler.py @@ -14,6 +14,7 @@ import uuid import json +import requests.models from oslo_config import fixture as config_fixture @@ -134,3 +135,10 @@ class TestRequestHandler(BaseTest): 'CONTENT-TYPE': 'application/json'}) actual = json.loads(response.data.decode("ascii")) self.assertEqual(actual, {'images': []}) + + def test_is_json_response(self): + response = requests.models.Response() + response.headers['Content-Type'] = 'application/json' + self.assertTrue(proxy.is_json_response(response)) + response.headers['Content-Type'] = 'application/text' + self.assertFalse(proxy.is_json_response(response))