1
+ from integration .helpers .base_test import BaseTest
1
2
import requests
3
+ from unittest .case import skipIf
2
4
3
5
from 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
4
8
from integration .helpers .deployer .utils .retry import retry
5
9
from parameterized import parameterized
6
10
7
- from integration .helpers .exception import StatusCodeError
8
-
9
11
ALL_METHODS = "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT"
10
12
11
13
14
+ @skipIf (current_region_does_not_support ([REST_API ]), "Rest API is not supported in this testing region" )
12
15
class TestApiWithCors (BaseTest ):
13
16
@parameterized .expand (
14
17
[
@@ -26,8 +29,8 @@ def test_cors(self, file_name):
26
29
allow_headers = "headers"
27
30
max_age = "600"
28
31
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 )
31
34
32
35
def test_cors_with_shorthand_notation (self ):
33
36
self .create_and_verify_stack ("combination/api_with_cors_shorthand" )
@@ -38,8 +41,8 @@ def test_cors_with_shorthand_notation(self):
38
41
allow_headers = None # This should be absent from response
39
42
max_age = None # This should be absent from response
40
43
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 )
43
46
44
47
def test_cors_with_only_methods (self ):
45
48
self .create_and_verify_stack ("combination/api_with_cors_only_methods" )
@@ -51,8 +54,8 @@ def test_cors_with_only_methods(self):
51
54
allow_headers = None # This should be absent from response
52
55
max_age = None # This should be absent from response
53
56
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 )
56
59
57
60
def test_cors_with_only_headers (self ):
58
61
self .create_and_verify_stack ("combination/api_with_cors_only_headers" )
@@ -63,8 +66,8 @@ def test_cors_with_only_headers(self):
63
66
allow_headers = "headers"
64
67
max_age = None # This should be absent from response
65
68
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 )
68
71
69
72
def test_cors_with_only_max_age (self ):
70
73
self .create_and_verify_stack ("combination/api_with_cors_only_max_age" )
@@ -75,17 +78,12 @@ def test_cors_with_only_max_age(self):
75
78
allow_headers = None
76
79
max_age = "600"
77
80
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 )
80
83
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 )
87
86
88
- self .assertEqual (status , 200 , "Options request must be successful and return HTTP 200" )
89
87
headers = response .headers
90
88
self .assertEqual (
91
89
headers .get ("Access-Control-Allow-Methods" ), allow_methods , "Allow-Methods header must have proper value"
0 commit comments