Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion goblet/handlers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,15 @@ def _apply_cors(self, resp):
if isinstance(resp, goblet.Response):
resp.headers.update(self.cors.get_access_control_headers())
if isinstance(resp, tuple):
resp[2].update(self.cors.get_access_control_headers())
# Flask custom Tuple response: body, status code, headers ({}, 200, {})
if len(resp) > 2:
resp[2].update(self.cors.get_access_control_headers())
else:
resp = goblet.Response(
resp[0],
status_code=int(resp[1]) or 200,
headers=self.cors.get_access_control_headers(),
)
if isinstance(resp, str):
resp = goblet.Response(resp, headers=self.cors.get_access_control_headers())
return resp
Expand Down
63 changes: 63 additions & 0 deletions goblet/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,69 @@ def test_add_multiple_routes(self):
assert gateway.resources["/home"]["GET"]
assert gateway.resources["/home2"]["GET"]

def test_call_tuple_response(self):
app = Goblet(function_name="goblet_example")

@app.route("/test", methods=["POST"])
def mock_function():
return "success", 201

mock_event1 = Mock()
mock_event1.path = "/test"
mock_event1.method = "POST"
mock_event1.headers = {}
mock_event1.json = {}
resp = app(mock_event1, None)
assert resp == ("success", 201)

def test_call_tuple_with_cors_response(self):
app = Goblet(function_name="goblet_example", cors=True)

@app.route("/test", methods=["POST"])
def mock_function():
return "success", 201

mock_event1 = Mock()
mock_event1.path = "/test"
mock_event1.method = "POST"
mock_event1.headers = {}
mock_event1.json = {}
resp = app(mock_event1, None)
assert resp.body == "success"
assert resp.status_code == 201

def test_call_tuple_with_headers_response(self):
app = Goblet(function_name="goblet_example")

@app.route("/test", methods=["POST"])
def mock_function():
return "success", 201, {"x-header": "test"}

mock_event1 = Mock()
mock_event1.path = "/test"
mock_event1.method = "POST"
mock_event1.headers = {}
mock_event1.json = {}
resp = app(mock_event1, None)
assert resp == ("success", 201, {"x-header": "test"})

def test_call_tuple_with_cors_and_headers_response(self):
app = Goblet(function_name="goblet_example", cors=True)

@app.route("/test", methods=["POST"])
def mock_function():
return "success", 201, {"x-header": "test"}

mock_event1 = Mock()
mock_event1.path = "/test"
mock_event1.method = "POST"
mock_event1.headers = {}
mock_event1.json = {}
resp = app(mock_event1, None)
assert resp[0] == "success"
assert resp[1] == 201
assert resp[2].get("x-header") == "test"

def test_call_route(self):
app = Goblet(function_name="goblet_example")
mock = Mock()
Expand Down