Ejemplo n.º 1
0
 async def process_message(self, path, body, message_id, message_key,
                           progress_cb):
     try:
         executor = self.commander.executor(progress_cb,
                                            self,
                                            message_key=message_key,
                                            message_id=message_id)
         return await executor.execute(path, body, allow_ws_only=True)
     except NoSuchPath as error:
         raise Finished(
             status=404,
             wanted=error.wanted,
             available=error.available,
             error="Specified path is invalid",
         )
Ejemplo n.º 2
0
    async def do_put(self):
        j = self.body_as_json()

        def progress_cb(message, stack_extra=0, **kwargs):
            maker = self.progress_maker(1 + stack_extra)
            info = maker(j, message, **kwargs)
            self.process_reply(info)

        path = self.request.path
        while path and path.endswith("/"):
            path = path[:-1]

        try:
            return await self.commander.executor(progress_cb,
                                                 self).execute(path, j)
        except NoSuchPath as error:
            raise Finished(
                status=404,
                wanted=error.wanted,
                available=error.available,
                error="Specified path is invalid",
            )
Ejemplo n.º 3
0
            await server.runner.ws_write(
                connection, {"path": "/thing", "body": {}, "message_id": msg_id}
            )
            assert await server.runner.ws_read(connection) == {
                "message_id": msg_id,
                "reply": {"result": "", "value": "blah and stuff"},
            }

            connection.close()
            assert await server.runner.ws_read(connection) is None

    async it "can process replies", make_wrapper:
        replies = []

        error1 = ValueError("Bad things happen")
        error2 = Finished(status=400, error="Stuff")

        class Handler(SimpleWebSocketBase):
            def process_reply(s, msg, exc_info=None):
                replies.append((msg, exc_info))

            async def process_message(s, path, body, message_id, message_key, progress_cb):
                if path == "/no_error":
                    return {"success": True}
                elif path == "/internal_error":
                    raise error1
                elif path == "/custom_return":
                    s.reply({"progress": {"error": "progress"}}, message_id=message_id)

                    class Ret:
                        def as_dict(s):
Ejemplo n.º 4
0
        expected = {"status": 301, "other": "<<<OTHER>>>"}
        assert json.loads(got) == expected

    it "hexlifies bytes objects":
        val = str(uuid.uuid1()).replace("-", "")
        unhexlified = binascii.unhexlify(val)

        thing = {"status": 302, "thing": unhexlified}
        got = json.dumps(thing, default=reprer)

        expected = {"status": 302, "thing": val}
        assert json.loads(got) == expected

describe "MessageFromExc":
    it "returns the kwargs from a Finished":
        error = Finished(status=418, one=1)
        info = MessageFromExc()(Finished, error, None)
        assert info == {"status": 418, "one": 1}

    it "uses process if not a finished":
        message_from_exc = MessageFromExc()
        exc_type = mock.Mock(name="exc_type")
        exc = mock.Mock(nme="exc")
        tb = mock.Mock(name="tb")
        info = mock.Mock(name="info")
        process = mock.Mock(name="process", return_value=info)

        exc = mock.Mock(name="exc")

        with mock.patch.object(message_from_exc, "process", process):
            assert message_from_exc(exc_type, exc, tb) is info
Ejemplo n.º 5
0
 async def do_delete(s):
     assert s.request.path == path
     raise Finished(status=501, reason=reason)
Ejemplo n.º 6
0
 async def do_delete(s):
     assert s.request.path == "/info/blah"
     raise Finished(status=501, reason=self.reason)
Ejemplo n.º 7
0
 async def do_patch(s):
     assert s.request.path == "/info/blah"
     assert s.request.body == self.body.encode()
     raise Finished(status=501, reason=self.reason)