|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from ..base_test import FlaskCorsTestCase |
| 4 | +from flask import Flask |
| 5 | + |
| 6 | +from flask_cors import * |
| 7 | +from flask_cors.core import * |
| 8 | + |
| 9 | + |
| 10 | +class SupportsCredentialsCase(FlaskCorsTestCase): |
| 11 | + def setUp(self): |
| 12 | + self.app = Flask(__name__) |
| 13 | + |
| 14 | + @self.app.route('/test_allow_private_network_access_supported') |
| 15 | + @cross_origin(allow_private_network=True) |
| 16 | + def test_private_network_supported(): |
| 17 | + return 'Private network!' |
| 18 | + |
| 19 | + @self.app.route('/test_allow_private_network_access_unsupported') |
| 20 | + @cross_origin(allow_private_network=False) |
| 21 | + def test_credentials_unsupported(): |
| 22 | + return 'Private network!' |
| 23 | + |
| 24 | + @self.app.route('/test_default') |
| 25 | + @cross_origin() |
| 26 | + def test_default(): |
| 27 | + return 'Open!' |
| 28 | + |
| 29 | + def test_credentials_supported(self): |
| 30 | + """ The specified route should return the |
| 31 | + Access-Control-Allow-Credentials header. |
| 32 | + """ |
| 33 | + resp = self.get('/test_allow_private_network_access_supported', origin='www.example.com', headers={ACL_REQUEST_HEADER_PRIVATE_NETWORK:'true'}) |
| 34 | + self.assertEqual(resp.headers.get(ACL_RESPONSE_PRIVATE_NETWORK), 'true') |
| 35 | + |
| 36 | + def test_default(self): |
| 37 | + """ The default behavior should be to allow private network access. |
| 38 | + """ |
| 39 | + resp = self.get('/test_default', origin='www.example.com', headers={ACL_REQUEST_HEADER_PRIVATE_NETWORK:'true'}) |
| 40 | + self.assertTrue(ACL_RESPONSE_PRIVATE_NETWORK in resp.headers) |
| 41 | + |
| 42 | + resp = self.get('/test_default') |
| 43 | + self.assertFalse(ACL_RESPONSE_PRIVATE_NETWORK in resp.headers) |
| 44 | + |
| 45 | + def test_credentials_unsupported(self): |
| 46 | + """ If private network access is disabled, the header should never be sent.""" |
| 47 | + resp = self.get('/test_allow_private_network_access_unsupported', origin='www.example.com') |
| 48 | + self.assertFalse(ACL_RESPONSE_PRIVATE_NETWORK in resp.headers) |
| 49 | + |
| 50 | + resp = self.get('/test_allow_private_network_access_unsupported', origin='www.example.com', headers={ACL_REQUEST_HEADER_PRIVATE_NETWORK:'true'}) |
| 51 | + self.assertEqual(resp.headers.get(ACL_RESPONSE_PRIVATE_NETWORK), 'false') |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + unittest.main() |
0 commit comments