1+ from integration .helpers .base_test import BaseTest
12import requests
3+ from unittest .case import skipIf
24
35from integration .helpers .base_test import BaseTest
6+ from integration .helpers .resource import current_region_does_not_support
7+ from integration .config .service_names import REST_API
48from integration .helpers .deployer .utils .retry import retry
59from parameterized import parameterized
610
7- from integration .helpers .exception import StatusCodeError
8-
911ALL_METHODS = "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT"
1012
1113
14+ @skipIf (current_region_does_not_support ([REST_API ]), "Rest API is not supported in this testing region" )
1215class TestApiWithCors (BaseTest ):
1316 @parameterized .expand (
1417 [
@@ -26,8 +29,8 @@ def test_cors(self, file_name):
2629 allow_headers = "headers"
2730 max_age = "600"
2831
29- self .verify_options_request (base_url + "/apione" , allow_methods , allow_origin , allow_headers , max_age )
30- self .verify_options_request (base_url + "/apitwo" , allow_methods , allow_origin , allow_headers , max_age )
32+ self .verify_cors_options_request (base_url + "/apione" , allow_methods , allow_origin , allow_headers , max_age )
33+ self .verify_cors_options_request (base_url + "/apitwo" , allow_methods , allow_origin , allow_headers , max_age )
3134
3235 def test_cors_with_shorthand_notation (self ):
3336 self .create_and_verify_stack ("combination/api_with_cors_shorthand" )
@@ -38,8 +41,8 @@ def test_cors_with_shorthand_notation(self):
3841 allow_headers = None # This should be absent from response
3942 max_age = None # This should be absent from response
4043
41- self .verify_options_request (base_url + "/apione" , ALL_METHODS , allow_origin , allow_headers , max_age )
42- self .verify_options_request (base_url + "/apitwo" , "OPTIONS,POST" , allow_origin , allow_headers , max_age )
44+ self .verify_cors_options_request (base_url + "/apione" , ALL_METHODS , allow_origin , allow_headers , max_age )
45+ self .verify_cors_options_request (base_url + "/apitwo" , "OPTIONS,POST" , allow_origin , allow_headers , max_age )
4346
4447 def test_cors_with_only_methods (self ):
4548 self .create_and_verify_stack ("combination/api_with_cors_only_methods" )
@@ -51,8 +54,8 @@ def test_cors_with_only_methods(self):
5154 allow_headers = None # This should be absent from response
5255 max_age = None # This should be absent from response
5356
54- self .verify_options_request (base_url + "/apione" , allow_methods , allow_origin , allow_headers , max_age )
55- self .verify_options_request (base_url + "/apitwo" , allow_methods , allow_origin , allow_headers , max_age )
57+ self .verify_cors_options_request (base_url + "/apione" , allow_methods , allow_origin , allow_headers , max_age )
58+ self .verify_cors_options_request (base_url + "/apitwo" , allow_methods , allow_origin , allow_headers , max_age )
5659
5760 def test_cors_with_only_headers (self ):
5861 self .create_and_verify_stack ("combination/api_with_cors_only_headers" )
@@ -63,8 +66,8 @@ def test_cors_with_only_headers(self):
6366 allow_headers = "headers"
6467 max_age = None # This should be absent from response
6568
66- self .verify_options_request (base_url + "/apione" , ALL_METHODS , allow_origin , allow_headers , max_age )
67- self .verify_options_request (base_url + "/apitwo" , "OPTIONS,POST" , allow_origin , allow_headers , max_age )
69+ self .verify_cors_options_request (base_url + "/apione" , ALL_METHODS , allow_origin , allow_headers , max_age )
70+ self .verify_cors_options_request (base_url + "/apitwo" , "OPTIONS,POST" , allow_origin , allow_headers , max_age )
6871
6972 def test_cors_with_only_max_age (self ):
7073 self .create_and_verify_stack ("combination/api_with_cors_only_max_age" )
@@ -75,17 +78,12 @@ def test_cors_with_only_max_age(self):
7578 allow_headers = None
7679 max_age = "600"
7780
78- self .verify_options_request (base_url + "/apione" , ALL_METHODS , allow_origin , allow_headers , max_age )
79- self .verify_options_request (base_url + "/apitwo" , "OPTIONS,POST" , allow_origin , allow_headers , max_age )
81+ self .verify_cors_options_request (base_url + "/apione" , ALL_METHODS , allow_origin , allow_headers , max_age )
82+ self .verify_cors_options_request (base_url + "/apitwo" , "OPTIONS,POST" , allow_origin , allow_headers , max_age )
8083
81- @retry (StatusCodeError , 3 )
82- def verify_options_request (self , url , allow_methods , allow_origin , allow_headers , max_age ):
83- response = requests .options (url )
84- status = response .status_code
85- if status != 200 :
86- raise StatusCodeError ("Request to {} failed with status: {}, expected status: 200" .format (url , status ))
84+ def verify_cors_options_request (self , url , allow_methods , allow_origin , allow_headers , max_age ):
85+ response = self .verify_options_request (url , 200 )
8786
88- self .assertEqual (status , 200 , "Options request must be successful and return HTTP 200" )
8987 headers = response .headers
9088 self .assertEqual (
9189 headers .get ("Access-Control-Allow-Methods" ), allow_methods , "Allow-Methods header must have proper value"
0 commit comments