1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
# -*- coding: utf-8 -*-
"""
test
~~~~
Flask-CORS is a simple extension to Flask allowing you to support cross
origin resource sharing (CORS) using a simple decorator.
:copyright: (c) 2016 by Cory Dolphin.
:license: MIT, see LICENSE for more details.
"""
from ..base_test import FlaskCorsTestCase
from flask import Flask, Response
from flask_cors import *
from flask_cors.core import *
class VaryHeaderTestCase(FlaskCorsTestCase):
def setUp(self):
self.app = Flask(__name__)
@self.app.route('/')
@cross_origin()
def wildcard():
return 'Welcome!'
@self.app.route('/test_consistent_origin')
@cross_origin(origins='http://foo.com')
def test_consistent():
return 'Welcome!'
@self.app.route('/test_vary')
@cross_origin(origins=["http://foo.com", "http://bar.com"])
def test_vary():
return 'Welcome!'
@self.app.route('/test_existing_vary_headers')
@cross_origin(origins=["http://foo.com", "http://bar.com"])
def test_existing_vary_headers():
return Response('', status=200,
headers={'Vary': 'Accept-Encoding'})
def test_default(self):
'''
By default, allow all domains, which means the Vary:Origin header
should be set.
'''
for resp in self.iter_responses('/', origin="http://foo.com"):
self.assertTrue('Vary' in resp.headers)
def test_consistent_origin(self):
'''
If the Access-Control-Allow-Origin header will change dynamically,
the Vary:Origin header should be set.
'''
for resp in self.iter_responses('/test_consistent_origin', origin="http://foo.com"):
self.assertFalse('Vary' in resp.headers)
def test_varying_origin(self):
''' Resources that wish to enable themselves to be shared with
multiple Origins but do not respond uniformly with "*" must
in practice generate the Access-Control-Allow-Origin header
dynamically in response to every request they wish to allow.
As a consequence, authors of such resources should send a Vary:
Origin HTTP header or provide other appropriate control directives
to prevent caching of such responses, which may be inaccurate if
re-used across-origins.
'''
example_origin = 'http://foo.com'
for resp in self.iter_responses('/test_vary', origin=example_origin):
self.assertHasACLOrigin(resp)
self.assertEqual(resp.headers.get('Vary'), 'Origin')
def test_consistent_origin_concat(self):
'''
If Flask-Cors adds a Vary header and there is already a Vary
header set, the headers should be combined and comma-separated.
'''
resp = self.get('/test_existing_vary_headers', origin="http://foo.com")
self.assertEqual(set(resp.headers.getlist('Vary')),
{'Origin', 'Accept-Encoding'})
if __name__ == "__main__":
unittest.main()
|