Skip to content

Commit

Permalink
add html and message receive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed May 24, 2024
1 parent 70e86bc commit ac12663
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import threading
from websocket import create_connection
import urllib.request

_SIMPLE_ARGS = dict(states=['A', 'B', 'C'], initial='A', name='Simple Machine',
ordered_transitions=True, ignore_invalid_triggers=True, auto_transitions=False)
Expand Down Expand Up @@ -66,3 +67,20 @@ def test_client_transition(self):
assert config["transition"]["trigger"] == "next_state"
assert config["state"] != _SIMPLE_ARGS["initial"]
ws.close()

def test_http(self):
self.machine = WebMachine(**_SIMPLE_ARGS) # type: ignore
time.sleep(_INIT_DELAY)
fp = urllib.request.urlopen(f"http://localhost:{self.machine.port}")
answer = fp.read().decode("utf8")
assert f"<title>{_SIMPLE_ARGS['name']}</title>" in answer

def test_receive_message(self):
self.machine = WebMachine(**_SIMPLE_ARGS) # type: ignore
time.sleep(_INIT_DELAY)
ws = create_connection(f"ws://localhost:{self.machine.port}/ws")
_ = ws.recv()
ws.send(json.dumps({"method": "trigger", "arg": "next_state"}))
time.sleep(_INIT_DELAY)
assert self.machine.state != _SIMPLE_ARGS["initial"]
ws.close()

0 comments on commit ac12663

Please sign in to comment.