def test_if_media_type_not_supported_then_setting_it_raises_error(api: API): foo_type = "application/foo" with pytest.raises(UnsupportedMediaType) as ctx: api.media_type = foo_type assert foo_type in str(ctx.value) with pytest.raises(UnsupportedMediaType) as ctx: API(media_type="application/foo") assert foo_type in str(ctx.value)
def test_replace_media_handlers(api: API, foo_type, handle_foo): api.media_handlers = {foo_type: handle_foo} api.media_type = foo_type @api.route("/") async def index(req, res): res.media = "bar" response = api.client.get("/") assert response.status_code == 200 assert response.headers["content-type"] == foo_type assert response.text == handle_foo("bar")
def test_use_builtin_media_handlers(api: API, media_type, expected_text): api.media_type = media_type data = {"message": "hello"} @api.route("/") async def index(req, res): res.media = data response = api.client.get("/") assert response.status_code == 200 assert response.headers["content-type"] == media_type assert response.text == expected_text(data)
def test_add_and_use_custom_media_handler(api: API, foo_type, handle_foo): api.media_handlers[foo_type] = handle_foo api.media_type = foo_type @api.route("/") async def index(req, res): res.media = "bar" response = api.client.get("/") assert response.status_code == 200 assert response.headers["content-type"] == foo_type assert response.text == handle_foo("bar")
def test_if_media_type_not_supported_then_setting_it_raises_error( api: API, foo_type): with pytest.raises(UnsupportedMediaType) as ctx: api.media_type = foo_type assert foo_type in str(ctx.value)