Example #1
0
    def test_load_empty(self):
        factory = RedisSessionFactory(self.redis_pool, 'secret', 'test')

        sid_store = factory._sid_store
        req = mock.Mock()
        req.cookies.get.return_value = sid_store._encode_cookie('123')

        @asyncio.coroutine
        def run():
            fut = asyncio.Future(loop=self.loop)
            factory(req, fut, loop=self.loop)
            sess = yield from fut
            self.assertEqual(sess, {})
            self.assertTrue(sess.new)

        self.loop.run_until_complete(run())
Example #2
0
    def test_load_existent(self):
        factory = RedisSessionFactory(self.redis_pool, 'secret', 'test')
        sid_store = factory._sid_store
        req = mock.Mock()
        req.cookies.get.return_value = sid_store._encode_cookie('123')

        key = b'session:123'
        data = pickle.dumps({'foo': 'bar'}, protocol=pickle.HIGHEST_PROTOCOL)

        @asyncio.coroutine
        def run():
            with (yield from self.redis_pool) as redis:
                ok = yield from redis.set(key, data)
                self.assertTrue(ok)
            fut = asyncio.Future(loop=self.loop)
            factory(req, fut, loop=self.loop)
            sess = yield from fut
            self.assertFalse(sess.new)
            self.assertEqual(sess, {'foo': 'bar'})

        self.loop.run_until_complete(run())
Example #3
0
def main():
    loop = asyncio.get_event_loop()

    handler = Handler()

    redis = loop.run_until_complete(
        Connection.create(db=0, encoder=BytesEncoder(), loop=loop))

    session_factory = RedisSessionFactory(redis,
                                          secret_key=b'secret',
                                          cookie_name='test_cookie',
                                          loop=loop)

    server = aiorest.RESTServer(hostname='127.0.0.1',
                                keep_alive=75,
                                session_factory=session_factory,
                                loop=loop)

    server.add_url('GET', '/count', handler.counter)

    srv = loop.run_until_complete(
        loop.create_server(server.make_handler, '127.0.0.1', 8080))

    @asyncio.coroutine
    def query():
        connector = aiohttp.TCPConnector(share_cookies=True, loop=loop)

        for _ in range(6):
            resp = yield from aiohttp.request('GET',
                                              'http://127.0.0.1:8080/count',
                                              connector=connector,
                                              loop=loop)
            data = yield from resp.json()
            print('Count is', data)

    loop.run_until_complete(query())

    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.close()