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]'
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
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
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()
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
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'
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
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()
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': '' }
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': ''}
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'
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]'