def test_process_origin_allow_all(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() cors = CORS(allow_all_origins=True) cors._set_allow_origin = mock.Mock() self.assertEqual( cors._process_origin(fake_req, fake_resp, 'rackspace.com'), True) cors._set_allow_origin.assert_called_once_with(fake_resp, '*') cors._set_allow_origin = mock.Mock() cors.supports_credentials = True self.assertEqual( cors._process_origin(fake_req, fake_resp, 'rackspace.com'), True) cors._set_allow_origin.assert_called_once_with(fake_resp, 'rackspace.com')
def test_process_origin_regex_none(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() cors = CORS() cors._set_allow_origin = mock.Mock() self.assertEqual( cors._process_origin(fake_req, fake_resp, 'rackspace.com'), False) self.assertEqual(cors._set_allow_origin.call_count, 0)
def test_process_origin_return(self): cors = CORS(allow_origins_list=['test.com']) cors._process_origin = mock.Mock(return_value=False) cors._process_credentials = mock.Mock() headers = [('origin', 'rackspace.com')] self.simulate_cors_request(cors, headers=headers, preflight=False) self.assertEqual(cors._process_origin.call_count, 1) self.assertEqual(cors._process_credentials.call_count, 0)
def test_process_origin_allow_regex(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() cors = CORS(allow_origins_regex='rack.*\.com') cors._set_allow_origin = mock.Mock() self.assertEqual( cors._process_origin(fake_req, fake_resp, 'rackspace.com'), True) cors._set_allow_origin.assert_called_once_with(fake_resp, 'rackspace.com')
def test_process_origin_deny(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() cors = CORS(allow_origins_list=['rackspace.com'], allow_origins_regex='rack.*\.com') cors._set_allow_origin = mock.Mock() self.assertEqual( cors._process_origin(fake_req, fake_resp, 'not_rackspace.com'), False) self.assertEqual(cors._set_allow_origin.call_count, 0)
def test_no_requested_method(self): cors = CORS(allow_all_origins=True) cors._get_requested_headers = mock.Mock() cors._process_origin = mock.Mock(return_value=True) headers = [('origin', 'rackspace.com')] self.simulate_cors_request(cors, headers=headers, preflight=True, add_request_method=False) self.assertEqual(cors._process_origin.call_count, 1) self.assertEqual(cors._get_requested_headers.call_count, 0)
def test_no_origin_return(self): cors = CORS() cors._process_origin = mock.Mock() self.simulate_cors_request(cors, preflight=False) self.assertEqual(cors._process_origin.call_count, 0)