Exemplo n.º 1
0
def test_normal_app():
    async def handler(req):
        return {'body': 'asdf'}

    port = util.net.free_port()
    web.app([('/', {'get': handler})]).listen(port)
    proc = pool.proc.new(tornado.ioloop.IOLoop.current().start)
    url = f'http://0.0.0.0:{port}'
    assert web.get_sync(url)['body'] == 'asdf'
    proc.terminate()
Exemplo n.º 2
0
def test_normal_app():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        return {'body': 'asdf'}
    port = util.net.free_port()
    web.app([('/', {'get': handler})]).listen(port)
    proc = pool.proc.new(tornado.ioloop.IOLoop.current().start)
    url = 'http://0.0.0.0:{port}'.format(**locals())
    assert web.get_sync(url)['body'] == 'asdf'
    proc.terminate()
Exemplo n.º 3
0
def test_middleware():
    def middleware(old_handler):
        async def new_handler(req):
            req = util.dicts.merge(req, {'headers': {'asdf': ' [mod req]'}})
            resp = await old_handler(req)
            resp = util.dicts.merge(resp,
                                    {'body': resp['body'] + ' [mod resp]'})
            return resp

        return new_handler

    @middleware
    async def handler(req):
        return {
            'headers': {
                'foo': 'bar'
            },
            'code': 200,
            'body': 'ok' + req['headers']['asdf']
        }

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        resp = web.get_sync(url)
        assert resp['body'] == 'ok [mod req] [mod resp]'
Exemplo n.º 4
0
 def setUp(self):
     cfg = ConfigParser.SafeConfigParser()
     cfg.read((os.path.join(TEST_DIR,"tests.cfg")))
     cfg.set("FileMan","testdir",TEST_DIR)
 
     # start the server
     # FIXME not sure, if this is a good start
     self.laymanapp = app(("/layman/(.*)","LayMan"), globals())
Exemplo n.º 5
0
def test_url_args():
    async def handler(req):
        return {'code': 200, 'body': json.dumps(req['args'])}

    app = web.app([('/(.*)/(.*)', {'get': handler})])
    with web.test(app) as url:
        resp = web.get_sync(url + '/something/stuff')
        assert json.loads(resp['body']) == ['something', 'stuff'], resp
Exemplo n.º 6
0
def test_non_2XX_codes():
    async def handler(req):
        1 / 0

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        resp = web.get_sync(url)
        assert '1 / 0' not in resp['body']
        assert resp['code'] == 500
Exemplo n.º 7
0
def test_basic():
    async def handler(req):
        assert req['verb'] == 'get'
        return {'headers': {'foo': 'bar'}, 'code': 200, 'body': 'ok'}

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        resp = web.get_sync(url)
        assert resp['body'] == 'ok'
        assert resp['headers']['foo'] == 'bar'
Exemplo n.º 8
0
def test_url_kwargs():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        return {'code': 200,
                'body': json.dumps(req['kwargs']['foo'])}
    app = web.app([('/:foo/stuff', {'get': handler})])
    with web.test(app) as url:
        rep = web.get_sync(url + '/something/stuff')
        assert json.loads(rep['body']) == 'something', rep
Exemplo n.º 9
0
def test_get_params():
    async def handler(req):
        return {'body': json.dumps(req['query'])}

    async def main(url):
        resp = await web.get(url, query={'foo': 'bar'})
        assert json.loads(resp['body']) == {'foo': 'bar'}

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 10
0
def test_non_2XX_codes():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        1 / 0

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        rep = web.get_sync(url)
        assert '1 / 0' not in rep['body']
        assert rep['code'] == 500
Exemplo n.º 11
0
def test_validate():
    async def handler(req):
        return {'code': 200, 'body': json.dumps(req['query'])}

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        resp = web.get_sync(url + '/?asdf=123&foo=bar&foo=notbar&stuff')
        assert json.loads(resp['body']) == {
            'asdf': '123',
            'foo': ['bar', 'notbar'],
            'stuff': ''
        }
Exemplo n.º 12
0
def test_post():
    async def handler(req):
        body = json.loads(req['body'])
        return {'code': body['num'] + 1}

    async def main(url):
        resp = await web.post(url, json.dumps({'num': 200}))
        assert resp['code'] == 201

    app = web.app([('/', {'post': handler})])
    with web.test(app) as url:
        tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 13
0
def test_url_params():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        return {'code': 200,
                'body': json.dumps(req['query'])}
    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        rep = web.get_sync(url + '/?asdf=123&foo=bar&foo=notbar&stuff')
        assert json.loads(rep['body']) == {'asdf': '123',
                                           'foo': ['bar', 'notbar'],
                                           'stuff': ''}
Exemplo n.º 14
0
def test_basic():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        assert req['verb'] == 'get'
        return {'headers': {'foo': 'bar'},
                'code': 200,
                'body': 'ok'}
    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        rep = web.get_sync(url)
        assert rep['body'] == 'ok'
        assert rep['headers']['foo'] == 'bar'
Exemplo n.º 15
0
def test_post_timeout():
    async def handler(req):
        await tornado.gen.sleep(1)
        return {'code': 200}

    async def main(url):
        resp = await web.post(url, '', timeout=.001)
        assert resp['code'] == 201

    app = web.app([('/', {'post': handler})])
    with web.test(app) as url:
        with pytest.raises(web.Timeout):
            tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 16
0
def test_get():
    async def handler(req):
        return {'body': 'ok', 'code': 200, 'headers': {'foo': 'bar'}}

    async def main(url):
        resp = await web.get(url)
        assert resp['body'] == 'ok'
        assert resp['code'] == 200
        assert resp['headers']['foo'] == 'bar'

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 17
0
def test_get_params():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        return {'body': json.dumps(req['query'])}

    @tornado.gen.coroutine
    def main(url):
        rep = yield web.get(url, query={'foo': 'bar'})
        assert json.loads(rep['body']) == {'foo': 'bar'}

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 18
0
def test_get_timeout():
    async def handler(req):
        if 'sleep' in req['query']:
            await tornado.gen.sleep(1)
        handler._sleep = True
        return {}

    async def main(url):
        await web.get(url + '?sleep', timeout=.001)

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        with pytest.raises(web.Timeout):
            tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 19
0
def test_post():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        body = json.loads(req['body'])
        return {'code': body['num'] + 1}

    @tornado.gen.coroutine
    def main(url):
        rep = yield web.post(url, json.dumps({'num': 200}))
        assert rep['code'] == 201

    app = web.app([('/', {'post': handler})])
    with web.test(app) as url:
        tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 20
0
def test_get():
    @tornado.gen.coroutine
    def handler(req):
        yield None
        return {'body': 'ok',
                'code': 200,
                'headers': {'foo': 'bar'}}

    @tornado.gen.coroutine
    def main(url):
        rep = yield web.get(url)
        assert rep['body'] == 'ok'
        assert rep['code'] == 200
        assert rep['headers']['foo'] == 'bar'

    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        tornado.ioloop.IOLoop.instance().run_sync(lambda: main(url))
Exemplo n.º 21
0
def test_middleware():
    def middleware(old_handler):
        @tornado.gen.coroutine
        def new_handler(req):
            req = util.dicts.merge(req, {'headers': {'asdf': ' [mod req]'}})
            rep = yield old_handler(req)
            rep = util.dicts.merge(rep, {'body': rep['body'] + ' [mod rep]'})
            return rep
        return new_handler
    @middleware
    @tornado.gen.coroutine
    def handler(req):
        yield None
        return {'headers': {'foo': 'bar'},
                'code': 200,
                'body': 'ok' + req['headers']['asdf']}
    app = web.app([('/', {'get': handler})])
    with web.test(app) as url:
        rep = web.get_sync(url)
        assert rep['body'] == 'ok [mod req] [mod rep]'
Exemplo n.º 22
0
def main(port=8080):
    routes = [('/(.*)', {'post': handle})]
    app = web.app(routes)
    print('start server on:', port)
    tornado.httpserver.HTTPServer(app).listen(8080)
    tornado.ioloop.IOLoop.current().start()