Skip to content

Commit 41e4366

Browse files
egojamesls
authored andcommitted
Add tests.
1 parent 3cc127d commit 41e4366

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

chalice/app.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,15 +1615,20 @@ def __call__(self, event, context):
16151615
)
16161616

16171617
# Responce from user handler.
1618-
responce = super(EventSourceHandler, self).__call__(event, context)
1618+
responce = super(
1619+
WebsocketEventSourceHandler, self).__call__(event, context)
16191620

16201621
data = None
16211622
if isinstance(responce, Response):
16221623
data = responce.to_dict()
16231624
elif isinstance(responce, dict):
16241625
data = responce
16251626

1626-
if data:
1627+
if isinstance(data, dict):
1628+
if "statusCode" not in data:
1629+
data = {**self.WEBSOCKET_API_RESPONCE, **data}
1630+
return data
1631+
elif data:
16271632
return data
16281633
return self.WEBSOCKET_API_RESPONCE
16291634

tests/unit/test_app.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,55 @@ def test_can_route_websocket_connect_message(sample_websocket_app,
27212721
assert event.connection_id == 'ABCD1234='
27222722

27232723

2724+
def test_can_route_websocket_connect_response_dict(create_websocket_event):
2725+
demo = app.Chalice('app-name')
2726+
client = FakeClient()
2727+
demo.websocket_api.session = FakeSession(client)
2728+
2729+
@demo.on_ws_connect()
2730+
def connect(event):
2731+
return dict(
2732+
headers={"Sec-WebSocket-Protocol": "Test-Protocol"},
2733+
statusCode=200,
2734+
body="Connected.",
2735+
)
2736+
2737+
event = create_websocket_event('$connect')
2738+
handler = websocket_handler_for_route('$connect', demo)
2739+
response = handler(event, context=None)
2740+
assert response == {
2741+
'headers': {'Sec-WebSocket-Protocol': 'Test-Protocol'},
2742+
'statusCode': 200,
2743+
'body': 'Connected.'
2744+
}
2745+
2746+
2747+
def test_can_route_websocket_connect_response_obj(create_websocket_event):
2748+
demo = app.Chalice('app-name')
2749+
client = FakeClient()
2750+
demo.websocket_api.session = FakeSession(client)
2751+
2752+
@demo.on_ws_connect()
2753+
def connect(event):
2754+
return Response(
2755+
"Connected.",
2756+
status_code=200,
2757+
headers={
2758+
"Sec-WebSocket-Protocol": "Test-Protocol",
2759+
},
2760+
)
2761+
2762+
event = create_websocket_event('$connect')
2763+
handler = websocket_handler_for_route('$connect', demo)
2764+
response = handler(event, context=None)
2765+
assert response == {
2766+
'headers': {'Sec-WebSocket-Protocol': 'Test-Protocol'},
2767+
'multiValueHeaders': {},
2768+
'statusCode': 200,
2769+
'body': 'Connected.',
2770+
}
2771+
2772+
27242773
def test_can_route_websocket_disconnect_message(sample_websocket_app,
27252774
create_websocket_event):
27262775
demo, calls = sample_websocket_app
@@ -3365,7 +3414,7 @@ def myfunction(event):
33653414
}
33663415
response = c.lambda_.invoke('myfunction', event)
33673416

3368-
assert response.payload == {'statusCode': 200}
3417+
assert response.payload == {'foo': 'bar', 'statusCode': 200}
33693418
assert called == [
33703419
{'name': 'mymiddleware', 'event': event},
33713420
{'name': 'myfunction', 'event': event},

0 commit comments

Comments
 (0)