|
| 1 | +from labthings.server.sockets import base, gevent as gsocket |
| 2 | + |
| 3 | +import json |
| 4 | +from flask import Blueprint |
| 5 | +from werkzeug.routing import Map |
| 6 | + |
| 7 | + |
| 8 | +def test_socket_subscriber_property_notify(view_cls, fake_websocket): |
| 9 | + setattr(view_cls, "endpoint", "index") |
| 10 | + ws = fake_websocket("", recieve_once=True) |
| 11 | + sub = base.SocketSubscriber(ws) |
| 12 | + |
| 13 | + sub.property_notify(view_cls) |
| 14 | + assert json.loads(ws.response) == { |
| 15 | + "messageType": "propertyStatus", |
| 16 | + "data": {"index": "GET"}, |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | +def test_socket_subscriber_property_notify_empty_view(flask_view_cls, fake_websocket): |
| 21 | + ws = fake_websocket("", recieve_once=True) |
| 22 | + sub = base.SocketSubscriber(ws) |
| 23 | + |
| 24 | + sub.property_notify(flask_view_cls) |
| 25 | + assert json.loads(ws.response) == { |
| 26 | + "messageType": "propertyStatus", |
| 27 | + "data": {flask_view_cls.__name__: None}, |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | +def test_socket_subscriber_event_notify(fake_websocket): |
| 32 | + ws = fake_websocket("", recieve_once=True) |
| 33 | + sub = base.SocketSubscriber(ws) |
| 34 | + |
| 35 | + data = {"key": "value"} |
| 36 | + |
| 37 | + sub.event_notify(data) |
| 38 | + assert json.loads(ws.response) == {"messageType": "event", "data": data} |
| 39 | + |
| 40 | + |
| 41 | +def test_sockets_flask_init(app): |
| 42 | + original_wsgi_app = app.wsgi_app |
| 43 | + socket = gsocket.Sockets(app) |
| 44 | + assert socket |
| 45 | + # Check new wsgi_app |
| 46 | + assert isinstance(app.wsgi_app, gsocket.SocketMiddleware) |
| 47 | + # Check "fallback" wsgi_app. This should be the original app.wsgi_app |
| 48 | + assert app.wsgi_app.wsgi_app == original_wsgi_app |
| 49 | + |
| 50 | + |
| 51 | +def test_sockets_flask_delayed_init(app): |
| 52 | + original_wsgi_app = app.wsgi_app |
| 53 | + socket = gsocket.Sockets() |
| 54 | + socket.init_app(app) |
| 55 | + assert socket |
| 56 | + # Check new wsgi_app |
| 57 | + assert isinstance(app.wsgi_app, gsocket.SocketMiddleware) |
| 58 | + # Check "fallback" wsgi_app. This should be the original app.wsgi_app |
| 59 | + assert app.wsgi_app.wsgi_app == original_wsgi_app |
| 60 | + |
| 61 | + |
| 62 | +def test_sockets_flask_route(app): |
| 63 | + socket = gsocket.Sockets(app) |
| 64 | + |
| 65 | + @socket.route("/ws") |
| 66 | + def ws_view_func(ws): |
| 67 | + pass |
| 68 | + |
| 69 | + # Assert ws_view_func was added to the Sockets URL map |
| 70 | + passed = False |
| 71 | + for rule in socket.url_map.iter_rules(): |
| 72 | + if rule.endpoint == ws_view_func: |
| 73 | + passed = True |
| 74 | + assert passed |
| 75 | + |
| 76 | + |
| 77 | +def test_sockets_flask_blueprint(app): |
| 78 | + socket = gsocket.Sockets(app) |
| 79 | + |
| 80 | + bp = Blueprint("blueprint", __name__) |
| 81 | + |
| 82 | + @bp.route("/ws") |
| 83 | + def ws_view_func(ws): |
| 84 | + pass |
| 85 | + |
| 86 | + socket.register_blueprint(bp, url_prefix="/") |
| 87 | + |
| 88 | + # Assert ws_view_func was added to the Sockets URL map |
| 89 | + passed = False |
| 90 | + for rule in socket.url_map.iter_rules(): |
| 91 | + if rule.endpoint == ws_view_func: |
| 92 | + passed = True |
| 93 | + assert passed |
| 94 | + |
| 95 | + # Test re-register same blueprint (should pass) |
| 96 | + socket.register_blueprint(bp, url_prefix="/") |
| 97 | + |
| 98 | + |
| 99 | +### Will need regular updating as new message handlers are added |
| 100 | +def test_process_socket_message(): |
| 101 | + assert base.process_socket_message("message") is None |
| 102 | + assert base.process_socket_message(None) is None |
0 commit comments