diff --git a/mixmatch/proxy.py b/mixmatch/proxy.py index dac70f8..0c5b09b 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 27aabe4..ecbb491 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.get_data(as_text=True)) 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))