-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtest.py
38 lines (28 loc) · 1.04 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import json
import boto3
import asyncio
import websockets
from six.moves.queue import Queue
def test_websocket_api():
client = boto3.client('apigatewayv2', endpoint_url="http://localhost:4566")
queue = Queue()
msg = {'action': 'test-action'}
async def start_client(uri):
async with websockets.connect(uri) as websocket:
print('Sending message to websocket')
await websocket.send(json.dumps(msg))
print('Waiting for response message from websocket ...')
result = await websocket.recv()
print('Received message from websocket: %s' % result)
queue.put(json.loads(result))
apis = client.get_apis()['Items']
api = [a for a in apis if 'localstack-websockets' in a['Name']][0]
url = api['ApiEndpoint']
print('Connecting to websocket URL %s' % url)
asyncio.get_event_loop().run_until_complete(start_client(url))
result = queue.get(timeout=3)
assert result == msg
def main():
test_websocket_api()
if __name__ == '__main__':
main()