def test_process_methods_resource(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() fake_resource = mock.MagicMock() cors = CORS(allow_all_methods=True) cors._get_requested_method = mock.Mock(return_value='GET') cors._get_resource_methods = mock.Mock(return_value=['POST']) cors._set_allowed_methods = mock.Mock() self.assertEqual( cors._process_methods(fake_req, fake_resp, fake_resource), False) cors._set_allowed_methods.assert_called_once_with(fake_resp, ['POST'])
def test_process_credentials_called(self): cors = CORS(allow_all_origins=True, allow_all_methods=True, allow_all_headers=True) cors._process_methods = mock.Mock(return_value=True) cors._process_credentials = mock.Mock() headers = [('origin', 'rackspace.com')] self.simulate_cors_request(cors, headers=headers, preflight=True) # print(cors._process_methods.call_count) cors._process_credentials.assert_called_once_with( self.resource.req, self.resource.resp, 'rackspace.com')
def test_process_methods_not_requested(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() fake_resource = mock.MagicMock() cors = CORS(allow_all_methods=True) cors._get_requested_method = mock.Mock(return_value=None) cors._set_allowed_methods = mock.Mock() self.assertEqual( cors._process_methods(fake_req, fake_resp, fake_resource), False) self.assertEqual(cors._set_allowed_methods.call_count, 0) cors._get_requested_method.assert_called_once_with(fake_req)
def test_header_not_allowed(self): cors = CORS(allow_all_origins=True, allow_all_methods=True, allow_headers_list=['test_header']) cors._process_methods = mock.Mock(return_value=True) cors._process_credentials = mock.Mock() headers = [('origin', 'rackspace.com'), ('access-control-request-headers', 'not_allowed_header')] self.simulate_cors_request(cors, headers=headers, preflight=True) self.assertEqual(cors._process_methods.call_count, 1) self.assertEqual(cors._process_credentials.call_count, 0)
def test_process_methods_notfound(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() fake_resource = mock.MagicMock() cors = CORS(allow_methods_list=['GET', 'POST', 'PUT', 'DELETE']) cors._set_allowed_methods = mock.Mock() cors._get_requested_method = mock.Mock(return_value='POST') cors._get_resource_methods = mock.Mock(return_value=['GET', 'PUT']) self.assertEqual( cors._process_methods(fake_req, fake_resp, fake_resource), False) cors._set_allowed_methods.assert_called_once_with( fake_resp, ['GET', 'PUT'])