Beispiel #1
0
async def test_static_with_default_index(client, app):

    app.hooks['startup'] = []
    extensions.static(app,
                      root=Path(__file__).parent / 'static',
                      default_index='index.html')
    await app.startup()

    resp = await client.get('/static/index.html')
    assert resp.status == HTTPStatus.OK
    assert b'Test' in resp.body
    assert resp.headers['Content-Type'] == 'text/html'

    resp = await client.get('/static/')
    assert resp.status == HTTPStatus.OK
    assert b'Test' in resp.body
    assert resp.headers['Content-Type'] == 'text/html'

    resp = await client.get('/static/sub/index.html')
    assert resp.status == HTTPStatus.OK
    assert b'Subtest' in resp.body
    assert resp.headers['Content-Type'] == 'text/html'

    resp = await client.get('/static/sub/')
    assert resp.status == HTTPStatus.OK
    assert b'Subtest' in resp.body
    assert resp.headers['Content-Type'] == 'text/html'
Beispiel #2
0
async def test_static(client, app):

    # startup has yet been called, but static extensions was not registered
    # yet, so let's simulate a new startup.
    app.hooks['startup'] = []
    extensions.static(app, root=Path(__file__).parent / 'static')
    url_for = extensions.named_url(app)
    await app.startup()

    resp = await client.get('/static/index.html')
    assert resp.status == HTTPStatus.OK
    assert b'Test' in resp.body
    assert resp.headers['Content-Type'] == 'text/html'

    resp = await client.get('/static/sub/index.html')
    assert resp.status == HTTPStatus.OK
    assert b'Subtest' in resp.body
    assert resp.headers['Content-Type'] == 'text/html'

    resp = await client.get('/static/style.css')
    assert resp.status == HTTPStatus.OK
    assert b'chocolate' in resp.body
    assert resp.headers['Content-Type'] == 'text/css'

    assert url_for("static",
                   path="path/myfile.png") == "/static/path/myfile.png"
Beispiel #3
0
def serve(reload=False):
    """Run a web server (for development only)."""
    # Debug only.
    static(app, "/explorer/", Path(__file__).parent / "explorer")
    if reload:
        hupper.start_reloader("trefle.bin.serve")
    traceback(app)
    simple_server(app)
Beispiel #4
0
def serve(reload=False):
    """Run a web server (for development only)."""
    if reload:
        import hupper

        hupper.start_reloader("copanier.serve")
    static(app, root=Path(__file__).parent / "static")
    simple_server(app, port=2244)
Beispiel #5
0
async def test_static_raises_if_path_is_outside_root(client, app):

    app.hooks['startup'] = []
    extensions.static(app, root=Path(__file__).parent / 'static')
    await app.startup()

    resp = await client.get('/static/../../README.md')
    assert resp.status == HTTPStatus.BAD_REQUEST
Beispiel #6
0
async def test_can_change_static_prefix(client, app):

    app.hooks['startup'] = []
    extensions.static(app,
                      root=Path(__file__).parent / 'static',
                      prefix='/foo')
    await app.startup()

    resp = await client.get('/foo/index.html')
    assert resp.status == HTTPStatus.OK
    assert b'Test' in resp.body
Beispiel #7
0
async def test_can_call_static_twice(client, app):

    # startup has yet been called, but static extensions was not registered
    # yet, so let's simulate a new startup.
    app.hooks["startup"] = []
    extensions.static(app,
                      root=Path(__file__).parent / "static",
                      prefix="/static/",
                      name="statics")
    extensions.static(app,
                      root=Path(__file__).parent / "medias",
                      prefix="/medias/",
                      name="medias")
    url_for = extensions.named_url(app)
    await app.startup()
    assert url_for("statics", path="myfile.png") == "/static/myfile.png"
    assert url_for("medias", path="myfile.mp3") == "/medias/myfile.mp3"
Beispiel #8
0
from .utils import escape_content, get_md_content_from_disk, save_md_content_to_disk

HERE = Path(__file__)
ALLOWED_TAGS = ["p", "strong", "em"]

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())


class Roll(BaseRoll):
    Response = CustomResponse


app = Roll()
logger(app)
traceback(app)
static(app, root=HERE.parent / "static")
i18n(app)


@app.route("/")
async def home(request: Request, response: CustomResponse) -> None:
    template_name = "home.html"
    template = language_aware_template(template_name, request["language"])
    response.html(template)


@app.route("/essay/", methods=["POST"])
async def on_post(request: Request, response: CustomResponse) -> None:
    essay_content = request.form.get("essay")
    name = save_md_content_to_disk(essay_content)
    response.status = HTTPStatus.FOUND