Beispiel #1
0
    def _handle_invoke_action(self, req):
        """Handler for the 'invoke_action' method."""

        params = req.params

        try:
            validate(params, SCHEMA_PARAMS_INVOKE_ACTION)
        except ValidationError as ex:
            self._write_error(str(ex),
                              WebsocketErrors.INVALID_METHOD_PARAMS,
                              msg_id=req.id)
            return

        try:
            input_value = params.get("parameters")
            action_result = yield self.exposed_thing.invoke_action(
                params["name"], input_value)
        except Exception as ex:
            self._write_error(str(ex),
                              WebsocketErrors.INTERNAL_ERROR,
                              msg_id=req.id)
            return

        res = WebsocketMessageResponse(result=action_result, msg_id=req.id)
        self.write_message(res.to_json())
Beispiel #2
0
    def test_coroutine():
        observe_msg_id = Faker().pyint()
        dispose_msg_id = Faker().pyint()

        conn = yield tornado.websocket.websocket_connect(url_thing_01)

        msg_observe_req = WebsocketMessageRequest(
            method=WebsocketMethods.ON_PROPERTY_CHANGE,
            params={"name": prop_name},
            msg_id=observe_msg_id)

        conn.write_message(msg_observe_req.to_json())

        msg_observe_resp_raw = yield conn.read_message()
        msg_observe_resp = WebsocketMessageResponse.from_raw(
            msg_observe_resp_raw)

        assert msg_observe_resp.id == observe_msg_id

        subscription_id = msg_observe_resp.result

        yield exposed_thing_01.write_property(prop_name, Faker().sentence())

        msg_emitted_raw = yield conn.read_message()
        msg_emitted = WebsocketMessageEmittedItem.from_raw(msg_emitted_raw)

        assert msg_emitted.subscription_id == subscription_id

        msg_dispose_req = WebsocketMessageRequest(
            method=WebsocketMethods.DISPOSE,
            params={"subscription": subscription_id},
            msg_id=dispose_msg_id)

        conn.write_message(msg_dispose_req.to_json())

        msg_dispose_resp_raw = yield conn.read_message()
        msg_dispose_resp = WebsocketMessageResponse.from_raw(
            msg_dispose_resp_raw)

        assert msg_dispose_resp.result == subscription_id

        conn.write_message(msg_dispose_req.to_json())

        msg_dispose_resp_02_raw = yield conn.read_message()
        msg_dispose_resp_02 = WebsocketMessageResponse.from_raw(
            msg_dispose_resp_02_raw)

        assert not msg_dispose_resp_02.result

        yield exposed_thing_01.write_property(prop_name, Faker().pystr())
        yield exposed_thing_01.write_property(prop_name, Faker().pystr())

        with pytest.raises(tornado.gen.TimeoutError):
            yield tornado.gen.with_timeout(
                timeout=datetime.timedelta(milliseconds=200),
                future=conn.read_message())
Beispiel #3
0
    def test_coroutine():
        conn = yield tornado.websocket.websocket_connect(url_thing_01)

        input_val = Faker().pystr()

        expected_out = yield exposed_thing_01.invoke_action(
            action_name, input_val)

        msg_id = Faker().pyint()

        msg_invoke_req = WebsocketMessageRequest(
            method=WebsocketMethods.INVOKE_ACTION,
            params={
                "name": action_name,
                "parameters": input_val
            },
            msg_id=msg_id)

        conn.write_message(msg_invoke_req.to_json())

        msg_invoke_resp_raw = yield conn.read_message()
        msg_invoke_resp = WebsocketMessageResponse.from_raw(
            msg_invoke_resp_raw)

        assert msg_invoke_resp.id == msg_id
        assert msg_invoke_resp.result == expected_out

        yield conn.close()
Beispiel #4
0
    def test_coroutine():
        conns = yield [
            tornado.websocket.websocket_connect(url_thing_01),
            tornado.websocket.websocket_connect(url_thing_02)
        ]

        request_id_01 = Faker().pyint()
        request_id_02 = Faker().pyint()
        request_id_03 = Faker().pyint()

        ws_request_prop_01 = WebsocketMessageRequest(
            method=WebsocketMethods.READ_PROPERTY,
            params={"name": prop_name_01},
            msg_id=request_id_01)

        ws_request_prop_02 = WebsocketMessageRequest(
            method=WebsocketMethods.READ_PROPERTY,
            params={"name": prop_name_02},
            msg_id=request_id_02)

        ws_request_prop_03 = WebsocketMessageRequest(
            method=WebsocketMethods.READ_PROPERTY,
            params={"name": prop_name_03},
            msg_id=request_id_03)

        conns[0].write_message(ws_request_prop_01.to_json())
        conns[0].write_message(ws_request_prop_02.to_json())
        conns[1].write_message(ws_request_prop_03.to_json())

        raw_resp_01 = yield conns[0].read_message()
        raw_resp_02 = yield conns[0].read_message()
        raw_resp_03 = yield conns[1].read_message()

        ws_resp_01 = WebsocketMessageResponse.from_raw(raw_resp_01)
        ws_resp_02 = WebsocketMessageResponse.from_raw(raw_resp_02)
        ws_resp_03 = WebsocketMessageResponse.from_raw(raw_resp_03)

        assert ws_resp_01.result == prop_value_01
        assert ws_resp_02.result == prop_value_02
        assert ws_resp_03.result == prop_value_03

        yield conns[0].close()
        yield conns[1].close()
Beispiel #5
0
    def _handle_on_event(self, req):
        """Handler for the 'on_event' subscription method."""

        params = req.params

        try:
            validate(params, SCHEMA_PARAMS_ON_EVENT)
        except ValidationError as ex:
            self._write_error(str(ex),
                              WebsocketErrors.INVALID_METHOD_PARAMS,
                              msg_id=req.id)
            return

        subscription_id = str(uuid.uuid4())

        res = WebsocketMessageResponse(result=subscription_id, msg_id=req.id)
        self.write_message(res.to_json())

        observable = self.exposed_thing.on_event(name=params["name"])

        self._subscribe(subscription_id, observable)
Beispiel #6
0
    def _handle_dispose(self, req):
        """Handler for the 'dispose' method."""

        params = req.params

        try:
            validate(params, SCHEMA_PARAMS_DISPOSE)
        except ValidationError as ex:
            self._write_error(str(ex),
                              WebsocketErrors.INVALID_METHOD_PARAMS,
                              msg_id=req.id)
            return

        result = None
        subscription_id = params["subscription"]

        if subscription_id in self._subscriptions:
            self._dispose_subscription(subscription_id)
            result = subscription_id

        res = WebsocketMessageResponse(result=result, msg_id=req.id)
        self.write_message(res.to_json())
Beispiel #7
0
    def _handle_set_property(self, req):
        """Handler for the 'set_property' method."""

        params = req.params

        try:
            validate(params, SCHEMA_PARAMS_WRITE_PROPERTY)
        except ValidationError as ex:
            self._write_error(str(ex),
                              WebsocketErrors.INVALID_METHOD_PARAMS,
                              msg_id=req.id)
            return

        try:
            yield self.exposed_thing.write_property(name=params["name"],
                                                    value=params["value"])
        except Exception as ex:
            self._write_error(str(ex),
                              WebsocketErrors.INTERNAL_ERROR,
                              msg_id=req.id)
            return

        res = WebsocketMessageResponse(result=None, msg_id=req.id)
        self.write_message(res.to_json())
Beispiel #8
0
    def _parse_msg_response(cls, raw_msg):
        """Returns a parsed WS Response message instance if
        the raw message format is valid and has the given ID.
        Raises Exception if the WS message is an error."""

        try:
            return WebsocketMessageResponse.from_raw(raw_msg)
        except WebsocketMessageException:
            pass

        try:
            return WebsocketMessageError.from_raw(raw_msg)
        except WebsocketMessageException:
            pass

        return None
Beispiel #9
0
    def test_coroutine():
        observe_msg_id = Faker().pyint()

        updated_val_01 = Faker().pystr()
        updated_val_02 = Faker().pystr()
        updated_val_03 = Faker().pystr()

        conn = yield tornado.websocket.websocket_connect(url_thing_01)

        msg_observe_req = WebsocketMessageRequest(
            method=WebsocketMethods.ON_PROPERTY_CHANGE,
            params={"name": prop_name},
            msg_id=observe_msg_id)

        conn.write_message(msg_observe_req.to_json())

        msg_observe_resp_raw = yield conn.read_message()
        msg_observe_resp = WebsocketMessageResponse.from_raw(
            msg_observe_resp_raw)

        assert msg_observe_resp.id == observe_msg_id

        subscription_id = msg_observe_resp.result

        def assert_emitted(the_msg_raw, the_expected_val):
            msg_emitted = WebsocketMessageEmittedItem.from_raw(the_msg_raw)

            assert msg_emitted.subscription_id == subscription_id
            assert msg_emitted.data["name"] == prop_name
            assert msg_emitted.data["value"] == the_expected_val

        yield exposed_thing_01.write_property(prop_name, updated_val_01)

        msg_emitted_raw = yield conn.read_message()
        assert_emitted(msg_emitted_raw, updated_val_01)

        yield exposed_thing_01.write_property(prop_name, updated_val_02)
        yield exposed_thing_01.write_property(prop_name, updated_val_03)

        msg_emitted_raw = yield conn.read_message()
        assert_emitted(msg_emitted_raw, updated_val_02)

        msg_emitted_raw = yield conn.read_message()
        assert_emitted(msg_emitted_raw, updated_val_03)

        yield conn.close()
Beispiel #10
0
    def test_coroutine():
        observe_msg_id = Faker().pyint()
        payload_01 = Faker().pydict(10, True, str, float)
        payload_02 = Faker().pydict(10, True, str, float)
        payload_03 = Faker().pydict(10, True, int)

        conn = yield tornado.websocket.websocket_connect(url_thing_01)

        msg_observe_req = WebsocketMessageRequest(
            method=WebsocketMethods.ON_EVENT,
            params={"name": event_name},
            msg_id=observe_msg_id)

        conn.write_message(msg_observe_req.to_json())

        msg_observe_resp_raw = yield conn.read_message()
        msg_observe_resp = WebsocketMessageResponse.from_raw(
            msg_observe_resp_raw)

        assert msg_observe_resp.id == observe_msg_id

        subscription_id = msg_observe_resp.result

        def assert_emitted(the_msg_raw, the_expected_payload):
            msg_emitted = WebsocketMessageEmittedItem.from_raw(the_msg_raw)

            assert msg_emitted.subscription_id == subscription_id
            assert msg_emitted.data == the_expected_payload

        exposed_thing_01.emit_event(event_name, payload_01)

        msg_emitted_raw = yield conn.read_message()
        assert_emitted(msg_emitted_raw, payload_01)

        exposed_thing_01.emit_event(event_name, payload_02)
        exposed_thing_01.emit_event(event_name, payload_03)

        msg_emitted_raw = yield conn.read_message()
        assert_emitted(msg_emitted_raw, payload_02)

        msg_emitted_raw = yield conn.read_message()
        assert_emitted(msg_emitted_raw, payload_03)

        yield conn.close()
Beispiel #11
0
    def test_coroutine():
        conn = yield tornado.websocket.websocket_connect(url_thing_01)

        updated_value = Faker().pystr()
        msg_id = uuid.uuid4().hex

        ws_request = WebsocketMessageRequest(
            method=WebsocketMethods.WRITE_PROPERTY,
            params={
                "name": prop_name,
                "value": updated_value
            },
            msg_id=msg_id)

        value = yield exposed_thing_01.read_property(prop_name)

        assert value != updated_value

        conn.write_message(ws_request.to_json())
        raw_response = yield conn.read_message()
        ws_response = WebsocketMessageResponse.from_raw(raw_response)

        assert ws_response.id == msg_id

        value = yield exposed_thing_01.read_property(prop_name)

        assert value == updated_value

        ws_request_err = WebsocketMessageRequest(
            method=WebsocketMethods.WRITE_PROPERTY,
            params={
                "name": prop_name + Faker().pystr(),
                "value": updated_value
            },
            msg_id=msg_id)

        conn.write_message(ws_request_err.to_json())
        raw_error = yield conn.read_message()
        ws_error = WebsocketMessageError.from_raw(raw_error)

        assert ws_error.code

        yield conn.close()
Beispiel #12
0
    def test_coroutine():
        observe_msg_id = Faker().pyint()
        event_name_err = Faker().pystr()

        conn = yield tornado.websocket.websocket_connect(url_thing_01)

        msg_observe_req = WebsocketMessageRequest(
            method=WebsocketMethods.ON_EVENT,
            params={"name": event_name_err},
            msg_id=observe_msg_id)

        conn.write_message(msg_observe_req.to_json())

        msg_observe_resp_raw = yield conn.read_message()
        msg_observe_resp = WebsocketMessageResponse.from_raw(
            msg_observe_resp_raw)

        msg_observe_err_raw = yield conn.read_message()
        msg_observe_err = WebsocketMessageError.from_raw(msg_observe_err_raw)

        assert msg_observe_err.code == WebsocketErrors.SUBSCRIPTION_ERROR
        assert msg_observe_err.data["subscription"] == msg_observe_resp.result
Beispiel #13
0
    def test_coroutine():
        yield server.start()

        ws_url = build_websocket_url(exposed_thing, server, port)

        assert WebsocketSchemes.WSS in ws_url

        with pytest.raises(ssl.SSLError):
            http_req = tornado.httpclient.HTTPRequest(ws_url, method="GET")
            yield tornado.websocket.websocket_connect(http_req)

        http_req = tornado.httpclient.HTTPRequest(ws_url,
                                                  method="GET",
                                                  validate_cert=False)
        conn = yield tornado.websocket.websocket_connect(http_req)

        request_id = Faker().pyint()

        msg_req = WebsocketMessageRequest(
            method=WebsocketMethods.READ_PROPERTY,
            params={"name": prop_name},
            msg_id=request_id)

        conn.write_message(msg_req.to_json())

        msg_resp_raw = yield conn.read_message()
        msg_resp = WebsocketMessageResponse.from_raw(msg_resp_raw)

        assert msg_resp.id == request_id

        value = yield exposed_thing.read_property(prop_name)

        assert value == msg_resp.result

        yield conn.close()
        yield server.stop()