def test_decorator_automatic_options(self, mock_request, mock_default): mock_request.method = "OPTIONS" decorators.crossdomain( origin='*', automatic_options=True )(lambda x: x)('foo') self.assertTrue(mock_default.called)
def test_decorator_custom_methods(self): custom_methods = ["HEAD", "OPTIONS", "GET", "POST", "PATCH"] response = decorators.crossdomain( origin='*', methods=custom_methods)( lambda x: x)('foo') self._compare_headers(response, '*', custom_methods, 21600)
def test_decorator_default_args(self): response = decorators.crossdomain(origin='*')(lambda x: x)('foo') self.assertEqual(response.status_code, 200) self._compare_headers( response, '*', ['OPTIONS', 'HEAD', "GET"], 21600 )
def test_custom_origin(self): custom_origins = ['website1', 'website2'] response = decorators.crossdomain( origin=custom_origins )(lambda x: x)('foo') self._compare_headers( response, ', '.join(custom_origins), self.methods, 21600 )
def test_attach_to_all(self): response = decorators.crossdomain(origin='*', attach_to_all=False)( lambda x: x)('foo') access_control_headers = { 'Access-Control-Allow-Origin', 'Access-Control-Allow-Methods', 'Access-Control-Max-Age', 'Access-Control-Allow-Headers' } for header in access_control_headers: self.assertNotIn(header, response.headers)
def test_custom_headers(self): custom_headers = {"CUSTOM-HEADER-KEY"} response = decorators.crossdomain( origin='*', headers=custom_headers )(lambda x: x)('foo') self._compare_headers(response, '*', self.methods, 21600) self.assertEqual( set(custom_headers), set(response.headers['Access-Control-Allow-Headers'].split(', ')) )
def test_max_age(self): custom_max_age = 1000 max_age = timedelta(seconds=custom_max_age) response = decorators.crossdomain( origin='*', max_age=max_age )(lambda x: x)('foo') self.assertEqual( response.headers['Access-Control-Max-Age'], str(float(custom_max_age)) )