コード例 #1
0
def test_replace_media_handlers(app: App, client, foo_type, handle_foo):
    app.media_handlers = {foo_type: handle_foo}
    app.media_type = foo_type

    @app.route("/")
    async def index(req, res):
        res.media = "bar"

    response = client.get("/")
    assert response.status_code == 200
    assert response.headers["content-type"] == foo_type
    assert response.text == handle_foo("bar")
コード例 #2
0
def test_add_and_use_custom_media_handler(app: App, client, foo_type,
                                          handle_foo):
    app.media_handlers[foo_type] = handle_foo
    app.media_type = foo_type

    @app.route("/")
    async def index(req, res):
        res.media = "bar"

    response = client.get("/")
    assert response.status_code == 200
    assert response.headers["content-type"] == foo_type
    assert response.text == handle_foo("bar")
コード例 #3
0
def test_use_builtin_media_handlers(app: App, client, media_type: str,
                                    expected_text):
    app.media_type = media_type
    data = {"message": "hello"}

    @app.route("/")
    async def index(req, res):
        res.media = data

    response = client.get("/")
    assert response.status_code == 200
    assert response.headers["content-type"] == media_type
    assert response.text == expected_text(data)
コード例 #4
0
def test_if_media_type_not_supported_then_setting_it_raises_error(
        app: App, foo_type):
    with pytest.raises(UnsupportedMediaType) as ctx:
        app.media_type = foo_type

    assert foo_type in str(ctx.value)