-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client.py
50 lines (36 loc) · 1.49 KB
/
test_client.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
39
40
41
42
43
44
45
46
47
48
49
50
import autobahn
from autobahn.asyncio.websocket import WebSocketClientProtocol, \
WebSocketClientFactory
import sys
from tests import *
class TestClientProtocol(WebSocketClientProtocol):
tests = [Config(), ExistingSwitch(), NotExistingSwitch(), PublicMethod(), PrivateMethod(), GetState()]
def onOpen(self):
print("Connected, running tests")
self.sendTest()
def onMessage(self, msg, isBinary):
if not isBinary:
for index,test in enumerate(self.tests):
#print(test)
if test.validateResponse(msg):
print("Test passed: {}".format(test.__class__.__name__))
self.tests.pop(index)
if len(self.tests) == 0:
print("Run tests")
sys.exit(0)
else:
self.sendTest()
def sendTest(self):
test = self.tests[-1]
self.sendMessage(test.getPayload())
def onClose(self, wasClean, code, reason):
print("Connection to {url} closed: {error_message}".format(url=self.factory.url, error_message=reason))
if __name__ == '__main__':
import asyncio
factory = WebSocketClientFactory(u"ws://127.0.0.1:9000", useragent="Kommandozentrale TestClient 0.1")
factory.protocol = TestClientProtocol
loop = asyncio.get_event_loop()
coro = loop.create_connection(factory, '127.0.0.1', 9000)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()