async def test_get_accept_star_content_negociation(client, app): extensions.content_negociation(app) @app.route('/test', accepts=['text/css']) async def get(req, resp): resp.body = 'accepted' resp = await client.get('/test', headers={'Accept': 'text/*'}) assert resp.status == HTTPStatus.OK
async def test_get_reject_content_negociation_if_no_accept_header(client, app): extensions.content_negociation(app) @app.route('/test', accepts=['*/*']) async def get(req, resp): resp.body = 'rejected' resp = await client.get('/test') assert resp.status == HTTPStatus.NOT_ACCEPTABLE
async def test_get_reject_content_negociation(client, app): extensions.content_negociation(app) @app.route('/test', accepts=['text/html']) async def get(req, resp): resp.body = 'rejected' resp = await client.get('/test', headers={'Accept': 'text/css'}) assert resp.status == HTTPStatus.NOT_ACCEPTABLE
async def test_post_reject_content_negociation(client, app): extensions.content_negociation(app) @app.route('/test', methods=['POST'], accepts=['text/html']) async def get(req, resp): resp.json = {'status': 'accepted'} client.content_type = 'application/x-www-form-urlencoded' resp = await client.post('/test', body={'key': 'value'}, headers={'Accept': 'application/json'}) assert resp.status == HTTPStatus.NOT_ACCEPTABLE
async def test_get_accept_content_negociation(client, app): extensions.content_negociation(app) @app.route('/test', accepts=['text/html']) async def get(req, resp): resp.headers['Content-Type'] = 'text/html' resp.body = 'accepted' resp = await client.get('/test', headers={'Accept': 'text/html'}) assert resp.status == HTTPStatus.OK assert resp.body == b'accepted' assert resp.headers['Content-Type'] == 'text/html'
async def test_post_accept_content_negociation(client, app): extensions.content_negociation(app) @app.route('/test', methods=['POST'], accepts=['application/json']) async def get(req, resp): resp.json = {'status': 'accepted'} client.content_type = 'application/x-www-form-urlencoded' resp = await client.post('/test', body={'key': 'value'}, headers={'Accept': 'application/json'}) assert resp.status == HTTPStatus.OK assert resp.headers['Content-Type'] == 'application/json; charset=utf-8' assert json.loads(resp.body.decode()) == {'status': 'accepted'}
async def test_get_accept_content_negociation_if_many(client, app): extensions.content_negociation(app) @app.route('/test', accepts=['text/html', 'application/json']) async def get(req, resp): if req.headers['ACCEPT'] == 'text/html': resp.headers['Content-Type'] = 'text/html' resp.body = '<h1>accepted</h1>' elif req.headers['ACCEPT'] == 'application/json': resp.json = {'status': 'accepted'} resp = await client.get('/test', headers={'Accept': 'text/html'}) assert resp.status == HTTPStatus.OK assert resp.body == b'<h1>accepted</h1>' assert resp.headers['Content-Type'] == 'text/html' resp = await client.get('/test', headers={'Accept': 'application/json'}) assert resp.status == HTTPStatus.OK assert json.loads(resp.body.decode()) == {'status': 'accepted'} assert resp.headers['Content-Type'] == 'application/json; charset=utf-8'