Skip to content

Commit 4bf009e

Browse files
committed
making sure cors makes sense
1 parent cc601e1 commit 4bf009e

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

acai_aws/apigateway/requirements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def requirements(**kwargs):
88
def decorator_func(func):
99

1010
def raise_timeout(*_):
11-
raise ApiTimeOutException
11+
raise ApiTimeOutException()
1212

1313
def start_timeout(timeout=None):
1414
if kwargs.get('timeout') is not None or timeout is not None:

acai_aws/apigateway/response.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
class Response:
99

10-
def __init__(self):
10+
def __init__(self, **kwargs):
1111
self.__code = 200
1212
self.__is_json = True
13-
self.__open_cors = True
13+
self.__cors = kwargs.get('cors', True)
1414
self.__base64_encoded = False
1515
self.__compress = False
1616
self.__content_type = ''
@@ -19,8 +19,8 @@ def __init__(self):
1919

2020
@property
2121
def headers(self):
22-
if self.open_cors:
23-
self.__set_open_cors()
22+
if self.cors:
23+
self.__set_cors()
2424
return self.__headers
2525

2626
@headers.setter
@@ -38,13 +38,21 @@ def content_type(self):
3838
def content_type(self, content_type ):
3939
self.__content_type = content_type
4040

41+
@property
42+
def cors(self):
43+
return self.__cors
44+
45+
@cors.setter
46+
def cors(self, access):
47+
self.__cors = access
48+
4149
@property
4250
def open_cors(self):
43-
return self.__open_cors
51+
return self.__cors
4452

4553
@open_cors.setter
4654
def open_cors(self, access):
47-
self.__open_cors = access
55+
self.__cors = access
4856

4957
@property
5058
def base64_encoded(self):
@@ -123,7 +131,7 @@ def __compress_body(self, body):
123131
file.write(body.encode('utf-8'))
124132
return base64.b64encode(bytes_io.getvalue()).decode('ascii')
125133

126-
def __set_open_cors(self):
134+
def __set_cors(self):
127135
self.__headers['Access-Control-Allow-Origin'] = '*'
128136
self.__headers['Access-Control-Allow-Headers'] = '*'
129137

acai_aws/apigateway/router.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(self, **kwargs):
1818
self.__with_auth = kwargs.get('with_auth')
1919
self.__on_error = kwargs.get('on_error')
2020
self.__on_timeout = kwargs.get('on_timeout')
21+
self.__cors = kwargs.get('cors', True)
2122
self.__timeout = kwargs.get('timeout', None)
2223
self.__output_error = kwargs.get('output_error', False)
2324
self.__verbose = kwargs.get('verbose_logging', False)
@@ -32,7 +33,7 @@ def auto_load(self):
3233

3334
def route(self, event, context):
3435
request = Request(event, context, self.__timeout)
35-
response = Response()
36+
response = Response(cors=self.__cors)
3637
try:
3738
self.__log_verbose(title='request-received', log={'request': request})
3839
self.__run_route_procedure(request, response)

tests/acai_aws/apigateway/test_response.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def test_default_headers(self):
3535
)
3636

3737
def test_closed_cors_headers(self):
38+
self.response.cors = False
39+
self.assertDictEqual(self.response.headers, {})
40+
41+
def test_closed_open_cors_headers(self):
3842
self.response.open_cors = False
3943
self.assertDictEqual(self.response.headers, {})
4044

0 commit comments

Comments
 (0)