Skip to content

Commit

Permalink
Makes set_cors_headers method in core.py compatible with resp.head… (#…
Browse files Browse the repository at this point in the history
…172)

* Made the set_cors_headers method in core.py compatible with resp.headers objects that are standard python dicts, by copying the underlying data into a new MultiDict.
  • Loading branch information
BillBrower authored and corydolphin committed Aug 17, 2016
1 parent 7a82aeb commit aaaf904
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion flask_cors/core.py
Expand Up @@ -13,7 +13,7 @@
from datetime import timedelta
from six import string_types
from flask import request, current_app
from werkzeug.datastructures import MultiDict
from werkzeug.datastructures import Headers, MultiDict

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -226,6 +226,13 @@ def set_cors_headers(resp, options):
LOG.debug('CORS have been already evaluated, skipping')
return resp

# Some libraries, like OAuthlib, set resp.headers to non Multidict
# objects (Werkzeug Headers work as well). This is a problem because
# headers allow repeated values.
if (not isinstance(resp.headers, Headers)
and not isinstance(resp.headers, MultiDict)):
resp.headers = MultiDict(resp.headers)

headers_to_set = get_cors_headers(options, request.headers, request.method)

LOG.debug('Settings CORS headers: %s', str(headers_to_set))
Expand Down
33 changes: 33 additions & 0 deletions tests/core/test_override_headers.py
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
test
~~~~
Flask-Cors tests module
"""

from ..base_test import FlaskCorsTestCase
from flask import Flask, Response

from flask_cors import *
from flask_cors.core import *

class ResponseHeadersOverrideTestCaseIntegration(FlaskCorsTestCase):
def setUp(self):
self.app = Flask(__name__)
CORS(self.app)

@self.app.route('/')
def index():
response = Response(headers={"custom": "dictionary"})
return 'Welcome'

def test_override_headers(self):
'''
Ensure we work even if response.headers is set to something other than a MultiDict.
'''
for resp in self.iter_responses('/'):
self.assertTrue(ACL_ORIGIN in resp.headers)

if __name__ == "__main__":
unittest.main()

0 comments on commit aaaf904

Please sign in to comment.