def test_allowAllOrigins(self): options = cors_options.CorsOptions() f = filters.AllowOriginFilter(options) # Test with no Origin request = cors_handler.CorsRequest() response = cors_handler.CorsResponse() error = f.filter(request, response) self.assertIsNone(error) self.assertEquals('*', response.allow_origin) # Test CORS request request = cors_handler.CorsRequest( 'GET', {constants.ORIGIN: 'http://foo.com'}) response = cors_handler.CorsResponse() error = f.filter(request, response) self.assertIsNone(error) self.assertEquals('*', response.allow_origin) # Test CORS preflight request request = cors_handler.CorsRequest( 'OPTIONS', { constants.ORIGIN: 'http://foo.com', constants.ACCESS_CONTROL_REQUEST_METHOD: 'GET' }) response = cors_handler.CorsResponse() error = f.filter(request, response) self.assertIsNone(error) self.assertEquals('*', response.allow_origin)
def test_notPreflight1(self): headers = {} headers[constants.ORIGIN] = 'http://github.com' headers[constants.ACCESS_CONTROL_REQUEST_METHOD] = 'PUT' req = cors_handler.CorsRequest('GET', headers) self.assertTrue(req.is_cors) self.assertFalse(req.is_preflight)
def test_defaultInstance(self): req = cors_handler.CorsRequest() self.assertIsNone(req.http_method) self.assertIsNone(req.origin) self.assertIsNone(req.request_method) self.assertIsNone(req.request_headers) self.assertFalse(req.is_cors) self.assertFalse(req.is_preflight)
def test_validOrigin(self): options = cors_options.CorsOptions(allow_origins=['http://foo.com']) f = filters.AllowOriginFilter(options) request = cors_handler.CorsRequest( 'GET', {constants.ORIGIN: 'http://foo.com'}) response = cors_handler.CorsResponse() error = f.filter(request, response) self.assertIsNone(error) self.assertEquals('http://foo.com', response.allow_origin)
def test_headers(self): headers = {} headers[constants.ORIGIN] = 'http://github.com' headers[constants.ACCESS_CONTROL_REQUEST_METHOD] = 'GET' headers[constants.ACCESS_CONTROL_REQUEST_HEADERS] = 'Header1, Header2' req = cors_handler.CorsRequest('GET', headers) self.assertEquals('http://github.com', req.origin) self.assertEquals('GET', req.request_method) self.assertEquals(['Header1', 'Header2'], req.request_headers)
def test_notPreflight2(self): headers = {} headers['Origin'] = 'http://github.com' req = cors_handler.CorsRequest('OPTIONS', headers) self.assertTrue(req.is_cors) self.assertFalse(req.is_preflight)
def test_isCors(self): headers = {} headers[constants.ORIGIN] = 'http://github.com' req = cors_handler.CorsRequest('GET', headers) self.assertTrue(req.is_cors) self.assertFalse(req.is_preflight)
def test_setMethod(self): req = cors_handler.CorsRequest('GET') self.assertEquals('GET', req.http_method)