예제 #1
0
    def test_error(self):
        @api
        class API:
            class good:
                def GET(x: int) -> int:
                    ...

            class bad:
                def GET(x: int) -> int:
                    ...

        class Impl(TestControllerBase):
            async def good_GET(self, x: int) -> int:
                return x + 1

            async def bad_GET(self, x: int) -> int:
                raise Exception("baz")

        async def run():
            async with makeE2EClient(API, Impl()) as client:
                r = await client.good.GET(2)
                self.assertEqual(r, 3)
                with self.assertRaises(aiohttp.ClientResponseError):
                    await client.bad.GET(2)

        run_coro(run())
예제 #2
0
    def test_typed(self):
        @attr.s(auto_attribs=True)
        class In:
            val: int

        @attr.s(auto_attribs=True)
        class Out:
            doubled: int

        @api
        class API:
            class doubler:
                def POST(data: In) -> Out:
                    ...

        class Impl(TestControllerBase):
            async def doubler_POST(self, data: In) -> Out:
                return Out(doubled=data.val * 2)

        async def run():
            async with makeE2EClient(API, Impl()) as client:
                out = await client.doubler.POST(In(3))
                self.assertEqual(out.doubled, 6)

        run_coro(run())
예제 #3
0
    def test_incomplete(self):
        async def fn():
            self.assertFalse(await self.geoip.lookup())

        run_coro(fn())
        self.assertIsNone(self.geoip.countrycode)
        self.assertIsNone(self.geoip.timezone)
예제 #4
0
    def setUp(self):
        self.geoip = GeoIP(make_app())

        async def fn():
            self.assertTrue(await self.geoip.lookup())

        run_coro(fn())
예제 #5
0
    def test_error_middleware(self):
        @api
        class API:
            class good:
                def GET(x: int) -> int:
                    ...

            class bad:
                def GET(x: int) -> int:
                    ...

        class Impl(TestControllerBase):
            async def good_GET(self, x: int) -> int:
                return x + 1

            async def bad_GET(self, x: int) -> int:
                1 / 0

        @web.middleware
        async def middleware(request, handler):
            resp = await handler(request)
            if resp.get('exception'):
                resp.headers['x-status'] = 'ERROR'
            return resp

        class Abort(Exception):
            pass

        @contextlib38.asynccontextmanager
        async def custom_make_request(client, method, path, *, params, json):
            async with make_request(client,
                                    method,
                                    path,
                                    params=params,
                                    json=json) as resp:
                if resp.headers.get('x-status') == 'ERROR':
                    raise Abort
                yield resp

        async def run():
            async with makeE2EClient(
                    API,
                    Impl(),
                    middlewares=[middleware],
                    make_request=custom_make_request) as client:
                r = await client.good.GET(2)
                self.assertEqual(r, 3)
                with self.assertRaises(Abort):
                    await client.bad.GET(2)

        run_coro(run())
예제 #6
0
    def test_args(self):
        @api
        class API:
            def GET(arg1: str, arg2: str) -> str:
                ...

        class Impl(TestControllerBase):
            async def GET(self, arg1: str, arg2: str) -> str:
                return '{}+{}'.format(arg1, arg2)

        async def run():
            async with makeE2EClient(API, Impl()) as client:
                self.assertEqual(await client.GET(arg1="A", arg2="B"), 'A+B')

        run_coro(run())
예제 #7
0
    def test_simple(self):
        @api
        class API:
            def GET() -> str:
                ...

        class Impl(TestControllerBase):
            async def GET(self) -> str:
                return 'value'

        async def run():
            async with makeE2EClient(API, Impl()) as client:
                self.assertEqual(await client.GET(), 'value')

        run_coro(run())
예제 #8
0
    def test_post(self):
        @api
        class API:
            def POST(data: Payload[dict]) -> str:
                ...

        class Impl(TestControllerBase):
            async def POST(self, data: dict) -> str:
                return data['key']

        async def run():
            async with makeE2EClient(API, Impl()) as client:
                self.assertEqual(await client.POST({'key': 'value'}), 'value')

        run_coro(run())
예제 #9
0
    def test_nested(self):
        @api
        class API:
            class endpoint:
                class nested:
                    def GET() -> str:
                        ...

        class Impl(TestControllerBase):
            async def endpoint_nested_GET(self) -> str:
                return 'value'

        async def run():
            async with makeE2EClient(API, Impl()) as client:
                self.assertEqual(await client.endpoint.nested.GET(), 'value')

        run_coro(run())
예제 #10
0
    def test_basic(self):
        def job():
            self.thething.broadcast(42)

        def cb(val, mydata):
            self.assertEqual(42, val)
            self.assertEqual('bacon', mydata)
            self.called += 1

        async def fn():
            self.called = 0
            self.thething = EventCallback()
            calls_expected = 3
            for _ in range(calls_expected):
                self.thething.subscribe(cb, 'bacon')
            job()
            await wait_other_tasks()
            self.assertEqual(calls_expected, self.called)

        run_coro(fn())
예제 #11
0
    def test_middleware(self):
        @api
        class API:
            def GET() -> int:
                ...

        class Impl(TestControllerBase):
            async def GET(self) -> int:
                return 1 / 0

        @web.middleware
        async def middleware(request, handler):
            return web.Response(status=200, headers={'x-status': 'skip'})

        class Skip(Exception):
            pass

        @contextlib38.asynccontextmanager
        async def custom_make_request(client, method, path, *, params, json):
            async with make_request(client,
                                    method,
                                    path,
                                    params=params,
                                    json=json) as resp:
                if resp.headers.get('x-status') == 'skip':
                    raise Skip
                yield resp

        async def run():
            async with makeE2EClient(
                    API,
                    Impl(),
                    middlewares=[middleware],
                    make_request=custom_make_request) as client:
                with self.assertRaises(Skip):
                    await client.GET()

        run_coro(run())
예제 #12
0
    def test_partial_reponse(self):
        async def fn():
            self.assertFalse(await self.geoip.lookup())

        run_coro(fn())
예제 #13
0
    def test_empty_tz(self):
        async def fn():
            self.assertFalse(await self.geoip.lookup())

        run_coro(fn())
        self.assertIsNone(self.geoip.timezone)
예제 #14
0
    def test_empty_cc(self):
        async def fn():
            self.assertFalse(await self.geoip.lookup())

        run_coro(fn())
        self.assertIsNone(self.geoip.countrycode)