示例#1
0
def test_wsgi_openapi():
    rpc = RPC(openapi={
        "title": "Title",
        "description": "Description",
        "version": "v1"
    })

    @rpc.register
    def none() -> None:
        return None

    @rpc.register
    def sayhi(name: str) -> str:
        """
        say hi with name
        """
        return f"hi {name}"

    class DNSRecord(TypedDict):
        record: str
        ttl: int

    class DNS(TypedDict):
        dns_type: str
        host: str
        result: DNSRecord

    @rpc.register
    def query_dns(dns_type: str, host: str) -> DNS:
        return {
            "dns_type": dns_type,
            "host": host,
            "result": {
                "record": "",
                "ttl": 0
            }
        }

    @rpc.register
    def timestamp() -> Generator[int, None, None]:
        while True:
            yield int(time.time())
            time.sleep(1)

    assert rpc.get_openapi_docs() == OPENAPI_DOCS

    with httpx.Client(app=rpc, base_url="http://testServer/") as client:
        assert client.get("/openapi-docs").status_code == 200
        assert client.get("/get-openapi-docs").status_code == 200

        assert (client.post(
            "/sayhi",
            content=json.dumps({
                "name0": "Aber"
            }).encode("utf8"),
            headers={
                "content-type": "",
                "serializer": "json"
            },
        )).status_code == 422
示例#2
0
async def test_asgi_openapi():
    rpc = RPC(
        mode="ASGI",
        openapi={
            "title": "Title",
            "description": "Description",
            "version": "v1"
        },
    )

    @rpc.register
    async def none() -> None:
        return None

    @rpc.register
    async def sayhi(name: str) -> str:
        """
        say hi with name
        """
        return f"hi {name}"

    DNSRecord = TypedDict("DNSRecord", {"record": str, "ttl": int})
    DNS = TypedDict("DNS", {"dns_type": str, "host": str, "result": DNSRecord})

    @rpc.register
    async def query_dns(dns_type: str, host: str) -> DNS:
        return {
            "dns_type": dns_type,
            "host": host,
            "result": {
                "record": "",
                "ttl": 0
            }
        }

    @rpc.register
    async def timestamp() -> AsyncGenerator[int, None]:
        while True:
            yield int(time.time())
            await asyncio.sleep(1)

    assert rpc.get_openapi_docs() == OPENAPI_DOCS

    async with httpx.AsyncClient(app=rpc,
                                 base_url="http://testServer/") as client:
        assert (await client.get("/openapi-docs")).status_code == 200
        assert (await client.get("/get-openapi-docs")).status_code == 200

        assert (await client.post(
            "/sayhi",
            content=json.dumps({
                "name0": "Aber"
            }).encode("utf8"),
            headers={
                "content-type": "",
                "serializer": "json"
            },
        )).status_code == 422
示例#3
0
def test_wsgi_openapi_without_pydantic():
    rpc = RPC(openapi={
        "title": "Title",
        "description": "Description",
        "version": "v1"
    })

    @rpc.register
    def sayhi(name: str) -> str:
        """
        say hi with name
        """
        return f"hi {name}"

    with pytest.raises(NotImplementedError):
        rpc.get_openapi_docs()
示例#4
0
async def test_asgi_openapi():
    rpc = RPC(
        mode="ASGI",
        openapi={"title": "Title", "description": "Description", "version": "v1"},
    )

    @rpc.register
    async def sayhi(name: str) -> str:
        return f"hi {name}"

    assert rpc.get_openapi_docs() == {
        "openapi": "3.0.0",
        "info": {"title": "Title", "description": "Description", "version": "v1"},
        "paths": {
            "/sayhi": {
                "post": {
                    "requestBody": {
                        "required": True,
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "name": {"title": "Name", "type": "string"}
                                    },
                                    "required": ["name"],
                                }
                            }
                        },
                    },
                    "responses": {
                        200: {
                            "content": {
                                "application/json": {"schema": {"type": "string"}}
                            }
                        }
                    },
                }
            }
        },
    }

    async with httpx.AsyncClient(app=rpc, base_url="http://testServer/") as client:
        assert (await client.get("/openapi-docs")).status_code == 200
        assert (await client.get("/get-openapi-docs")).status_code == 200
示例#5
0
def test_wsgirpc():
    rpc = RPC()
    assert isinstance(rpc, WsgiRPC)

    @rpc.register
    def sayhi(name: str) -> str:
        return f"hi {name}"

    with pytest.raises(
            TypeError,
            match="WSGI mode can only register synchronization functions."):

        @rpc.register
        async def async_sayhi(name: str) -> str:
            return f"hi {name}"

    @rpc.register
    def sayhi_without_type_hint(name):
        return f"hi {name}"

    with httpx.Client(app=rpc, base_url="http://testServer/") as client:
        assert client.get("/openapi-docs").status_code == 405
        assert client.post("/sayhi", data={"name": "Aber"}).status_code == 415
        assert client.post("/sayhi", json={"name": "Aber"}).status_code == 200
        assert (client.post("/sayhi_without_type_hint",
                            json={"name": "Aber"})).status_code == 200
        assert (client.post("/sayhi",
                            content=json.dumps({"name":
                                                "Aber"})).status_code == 415)
        assert (client.post(
            "/sayhi",
            content=json.dumps({
                "name": "Aber"
            }).encode("utf8"),
            headers={
                "serializer": "application/json"
            },
        ).status_code == 415)
        assert (client.post(
            "/sayhi",
            content=json.dumps({
                "name": "Aber"
            }).encode("utf8"),
            headers={
                "content-type": "",
                "serializer": "json"
            },
        ).status_code == 200)
        assert client.post("/non-exists", json={
            "name": "Aber"
        }).status_code == 404
示例#6
0
def test_wsgirpc():
    rpc = RPC()
    assert isinstance(rpc, WsgiRPC)

    @rpc.register
    def sayhi(name: str) -> str:
        return f"hi {name}"

    with pytest.raises(
        TypeError, match="WSGI mode can only register synchronization functions."
    ):

        @rpc.register
        async def async_sayhi(name: str) -> str:
            return f"hi {name}"

    with httpx.Client(app=rpc, base_url="http://testServer/") as client:
        assert client.get("/openapi-docs").status_code == 404
示例#7
0
async def test_asgirpc():
    rpc = RPC(mode="ASGI")
    assert isinstance(rpc, AsgiRPC)

    @rpc.register
    async def sayhi(name: str) -> str:
        return f"hi {name}"

    with pytest.raises(
        TypeError, match="ASGI mode can only register asynchronous functions."
    ):

        @rpc.register
        def sync_sayhi(name: str) -> str:
            return f"hi {name}"

    async with httpx.AsyncClient(app=rpc, base_url="http://testServer/") as client:
        assert (await client.get("/openapi-docs")).status_code == 404