예제 #1
0
async def test_controller_return_file():
    file_path = get_file_path("example.config", "files2")

    app = FakeApplication()
    app.controllers_router = RoutesRegistry()
    get = app.controllers_router.get

    class Example(Controller):
        @get("/")
        async def home(self):
            return self.file(file_path, "text/plain; charset=utf-8")

    app.setup_controllers()

    await app(
        get_example_scope("GET", "/", []),
        MockReceive(),
        MockSend(),
    )

    response = app.response
    assert response.status == 200
    assert response.headers.get_single(
        b"content-type") == b"text/plain; charset=utf-8"
    assert response.headers.get_single(b"content-disposition") == b"attachment"

    text = await response.text()
    with open(file_path, mode="rt", encoding="utf8") as f:
        contents = f.read()
        assert contents == text
예제 #2
0
async def test_file_response_from_fs_with_filename():
    app = FakeApplication()
    file_path = get_file_path("example.config", "files2")

    @app.router.get("/")
    async def home():
        return file(file_path, "text/plain; charset=utf-8", file_name="foo.xml")

    app.normalize_handlers()

    await app(
        get_example_scope("GET", "/", []),
        MockReceive(),
        MockSend(),
    )

    response = app.response
    assert response.status == 200
    assert response.headers.get_single(b"content-type") == b"text/plain; charset=utf-8"
    assert (
        response.headers.get_single(b"content-disposition")
        == b'attachment; filename="foo.xml"'
    )

    text = await response.text()
    with open(file_path, mode="rt", encoding="utf8") as f:
        contents = f.read()
        assert contents == text
예제 #3
0
async def test_file_response_from_fs(app, mock_receive, mock_send):
    file_path = get_file_path("example.config", "files2")

    @app.router.get("/")
    async def home():
        return file(file_path, "text/plain; charset=utf-8")

    app.normalize_handlers()

    await app(
        get_example_scope("GET", "/", []),
        mock_receive(),
        mock_send,
    )

    response = app.response
    assert response.status == 200
    assert response.headers.get_single(
        b"content-type") == b"text/plain; charset=utf-8"
    assert response.headers.get_single(b"content-disposition") == b"attachment"

    text = await response.text()
    with open(file_path, mode="rt", encoding="utf8") as f:
        contents = f.read()
        assert contents == text