예제 #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'
예제 #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"
예제 #3
0
파일: bin.py 프로젝트: Vioasselin/trefle
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)
예제 #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)
예제 #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
예제 #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
예제 #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"
예제 #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