Beispiel #1
0
def test_static_handler_alternate_index(index_directory, other_content):
    handler = StaticHandler(index_directory, index_file="other.gmi")
    # "/" returns other.gmi content
    response = handler.get_response("", "/")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(other_content, encoding="utf-8")

    # Subdir -> no other.gmi -> directory listing
    response = handler.get_response("", "/subdir/")
    assert isinstance(response, DirectoryListingResponse)
    assert response.content.startswith(
        b"# Directory listing for `/subdir`\r\n")
Beispiel #2
0
def test_static_handler_not_found(index_directory):
    handler = StaticHandler(index_directory)
    with pytest.raises(FileNotFoundError):
        handler.get_response("", "/not-found")

    with pytest.raises(FileNotFoundError):
        handler.get_response("", "/not-found.gmi")

    with pytest.raises(FileNotFoundError):
        handler.get_response("", "/subdir/not-found/")

    with pytest.raises(FileNotFoundError):
        handler.get_response("", "/subdir/not-found.gmi")
Beispiel #3
0
def test_static_handler_alternate_index_no_dirlist(index_directory,
                                                   other_content):
    handler = StaticHandler(index_directory,
                            directory_listing=False,
                            index_file="other.gmi")
    # "/" returns other.gmi content
    response = handler.get_response("", "/")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(other_content, encoding="utf-8")

    # Subdir -> no other.gmi -> no directory listing
    with pytest.raises(FileNotFoundError):
        response = handler.get_response("", "/subdir/")
Beispiel #4
0
def test_static_handler_subdir(index_directory, sub_content):
    # Reaching direct /subdir/sub.gmi
    handler = StaticHandler(index_directory)
    response = handler.get_response("", "/subdir/sub.gmi")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(sub_content, encoding="utf-8")

    # No Index -> Directory Listing
    handler = StaticHandler(index_directory)
    response = handler.get_response("", "/subdir/")
    assert isinstance(response, DirectoryListingResponse)
    assert response.content.startswith(
        b"# Directory listing for `/subdir`\r\n")

    # No Index + No slash -> Directory Listing
    handler = StaticHandler(index_directory)
    response = handler.get_response("", "/subdir")
    assert isinstance(response, RedirectResponse)
    assert response.target == "subdir/"
Beispiel #5
0
def test_static_handler_no_directory_listing(index_directory, index_content,
                                             sub_content):
    handler = StaticHandler(index_directory, directory_listing=False)
    # No change in response for "/"
    response = handler.get_response("", "/")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(index_content, encoding="utf-8")

    # Subdir + no slash -> Redirect to "/"
    response = handler.get_response("", "/subdir")
    assert isinstance(response, RedirectResponse)
    assert response.target == "subdir/"

    # Subdir -> no index -> no directory listing
    with pytest.raises(FileNotFoundError):
        handler.get_response("", "/subdir/")

    # subdir/sub.gmi
    response = handler.get_response("", "/subdir/sub.gmi")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(sub_content, encoding="utf-8")
Beispiel #6
0
        response = self.get_response()
        return response


class DatetimeTemplateHandler(TemplateHandler):
    template_file = "examples/templates/template.txt"

    def get_context(self, *args, **kwargs):
        return {"datetime": datetime.datetime.now()}


if __name__ == "__main__":
    urls = {
        "": StaticHandler(
            # Static pages, with directory listing
            static_dir="examples/static/",
            directory_listing=True,
        ),
        "/test": StaticHandler(
            # Static pages, no directory listing
            static_dir="examples/static/",
            directory_listing=False,
        ),
        "/with-sub": StaticHandler(
            # Static pages, pointing at a "deep" directory with an index.gmi file
            static_dir="examples/static/sub-dir",
        ),
        "/index-file": StaticHandler(
            # Static pages, pointing at a directory with an alternate index file
            static_dir="examples/static/empty-dir",
            index_file="one.gmi",
Beispiel #7
0
def test_static_handler_sub_url(index_directory, index_content):
    handler = StaticHandler(index_directory)
    response = handler.get_response("/test", "/test/index.gmi")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(index_content, encoding="utf-8")
Beispiel #8
0
def test_static_handler_document(index_directory, index_content,
                                 other_content):
    handler = StaticHandler(index_directory)
    response = handler.get_response("", "/")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(index_content, encoding="utf-8")

    # Reaching directly index.gmi
    handler = StaticHandler(index_directory)
    response = handler.get_response("", "/index.gmi")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(index_content, encoding="utf-8")

    # Reaching directly index.gmi / no starting slash
    handler = StaticHandler(index_directory)
    response = handler.get_response("/", "index.gmi")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(index_content, encoding="utf-8")

    # Reaching directly other.gmi
    handler = StaticHandler(index_directory)
    response = handler.get_response("", "/other.gmi")
    assert isinstance(response, DocumentResponse)
    assert response.content == bytes(other_content, encoding="utf-8")
Beispiel #9
0
def test_static_handler_not_a_directory():
    with pytest.raises(ImproperlyConfigured):
        StaticHandler("/tmp/not-a-directory")