Beispiel #1
0
def test_if_method_not_in_allow_methods_then_400():
    app = App(
        enable_cors=True,
        cors_config={
            "allow_origins": ["foobar.com"],
            "allow_methods": ["POST"],
        },
    )

    @app.route("/")
    async def index(req, res):
        pass

    client = create_client(app)
    response = client.options(
        "/",
        headers={
            "origin": "foobar.com",
            "access-control-request-method": "GET",
        },
    )
    assert response.status_code == 400
Beispiel #2
0
"""Application definition."""
from bocadillo import App, discover_providers, Templates, static

app = App()
discover_providers("chatbot.providerconf")
templates = Templates(app, directory='dist')
app.mount(prefix='js', app=static('dist/js'))
app.mount(prefix='css', app=static('dist/css'))


# Create routes here.
@app.route('/')
async def index(req, res):
    res.html = await templates.render("index.html")


@app.websocket_route("/conversation")
async def converse(ws, diego, save_client):
    async for message in ws:
        response = diego.get_response(message)
        await ws.send(str(response))


@app.route("/client-count")
async def client_count(req, res, clients):
    res.json = {"count": len(clients)}
Beispiel #3
0
from bocadillo import App

app = App(
    enable_cors=True,
    cors_config={"allow_origins": ["*"], "allow_methods": ["*"]},
)

_COURSES = [
    {
        "id": 1,
        "code": "adv-maths",
        "name": "Advanced Mathematics",
        "created": "2018-08-14T12:09:45",
    },
    {
        "id": 2,
        "code": "cs1",
        "name": "Computer Science I",
        "created": "2018-06-12T18:34:16",
    },
]


@app.route("/courses")
async def courses_list(req, res):
    res.media = _COURSES


if __name__ == "__main__":
    app.run()
from bocadillo import App, Templates, static, view
from models import *

# Initialize app
app = App(static_dir=None)
templates = Templates(app, directory='../app/static/dist')
###### Please make sure to use env variables as this information should be private #########
db.bind(provider='postgres',
        user='******',
        password='******',
        host='postgres',
        database='yourdbname')
#^^^^^ Please make sure to use env variables as this information should be private ^^^^^^###
db.generate_mapping(create_tables=True)


# Create index route
@app.route("/")
# Allow post and get methods (only get is allowed by default)
@view(methods=['POST', 'GET'])
async def homepage(req, res):
    # Check if a post request is inbound
    if 'POST' in req.method:
        # Grab the data from the form of the post request
        data = await req.form()
        # Use function defined in models to add obj to database
        add_animal(name=data['name'], age=data['age'])
        print(data)

    with orm.db_session:
        dbquery = orm.select((a.age, a.name) for a in Animal)[:]
Beispiel #5
0
def test_if_debug_then_static_files_auto_refresh(app: App, empty_run):
    app.run(debug=True, _run=empty_run)
    for static_app in app._static_apps.values():
        assert static_app.autorefresh
Beispiel #6
0
def test_if_debug_then_debug_mode_enabled(app: App, empty_run):
    assert not app.debug
    app.run(debug=True, _run=empty_run)
    assert app.debug
Beispiel #7
0
def test_port_is_8000_by_default(app: App):
    def run(_, port, **kwargs):
        assert port == 8000

    app.run(_run=run)
Beispiel #8
0
def test_if_no_route_exists_for_name_then_url_for_raises_404(app: App):
    with pytest.raises(HTTPError):
        app.url_for(name="about")
Beispiel #9
0
def test_basic(app: App, client, view):
    app.route("/hi")(view)
    r = client.get("/hi")
    assert r.status_code == 200
    assert r.text == "Hello, providers!"
Beispiel #10
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)
Beispiel #11
0
def test_can_specify_media_type_when_creating_the_api_object():
    App(media_type=CONTENT_TYPE.JSON)
Beispiel #12
0
def test_sessions_enabled_no_secret_key():
    with pytest.raises(RuntimeError):
        App(enable_sessions=True)
Beispiel #13
0
def test_sessions_enabled_secret_key_empty():
    with override_env("SECRET_KEY", ""):
        with pytest.raises(RuntimeError):
            App(enable_sessions=True)
Beispiel #14
0
from bocadillo import App, configure, discover_providers

from . import settings
from .router import router

discover_providers("server.providerconf")

app = App()
app.include_router(router)
configure(app, settings)
Beispiel #15
0
def fixture_raw_app(request) -> App:
    settings._clear()
    return App()
Beispiel #16
0
def test_with_route_parameter(app: App, client, view):
    app.route("/hi/{who}")(view)
    r = client.get("/hi/peeps")
    assert r.status_code == 200
    assert r.text == "Hello, peeps!"
Beispiel #17
0
from bocadillo import App, view, provider

app = App(enable_sessions=True)


@provider(scope="app", name="todos")
def provide_todos():
    return [
        {
            "id": 0,
            "content": "Go shopping"
        },
        {
            "id": 1,
            "content": "Cook fries"
        },
        {
            "id": 2,
            "content": "Do laundry"
        },
    ]


@app.route("/unseen-todos")
async def get_unseen_todos(req, res, todos):
    last_id = req.session.get("last_id", -1)
    unseen_todos = todos[last_id + 1:]

    if unseen_todos:
        req.session["last_id"] = unseen_todos[-1]["id"]
Beispiel #18
0
import socketio
from bocadillo import App, Templates, static

app = App()
templates = Templates(app)

# Create a socket.io async server.
# NOTE: use the asgi driver, as described in:
# https://python-socketio.readthedocs.io/en/latest/server.html#uvicorn-daphne-and-other-asgi-servers
sio = socketio.AsyncServer(async_mode="asgi")

# Create an ASGI-compliant app out of the socket.io server,
# and mount it under the root app.
# NOTE: "socket.io" is the default for `socketio_path`. We only add it
# here for the sake of being explicit.
# As a result, the client can connect at `/sio/socket.io`.
app.mount("/sio", socketio.ASGIApp(sio, socketio_path="socket.io"))

# Server static files for the socket.io client.
# See: https://github.com/socketio/socket.io-client
# NOTE: alternatively, the socket.io client could be served from
# a CDN if you don't have npm/Node.js available in your runtime.
# If so, static files would be linked to in the HTML page, and we wouldn't
# need this line.
# See: https://socket.io/docs/#Javascript-Client
app.mount("/socket.io", static("node_modules/socket.io-client/dist"))


@app.route("/")
async def index(req, res):
    res.html = await templates.render("index.html")
Beispiel #19
0
def test_host_is_localhost_by_default(app: App):
    def run(_, host, **kwargs):
        assert host == "127.0.0.1"

    app.run(_run=run)
def test_at_least_one_of_name_or_url_must_be_given(app: App):
    with pytest.raises(AssertionError) as ctx:
        app.redirect()
    assert all(item in str(ctx.value) for item in ("url", "expected", "name"))
Beispiel #21
0
def test_debug_mode_disabled_by_default(app: App, empty_run):
    assert not app.debug
    app.run(_run=empty_run)
    assert not app.debug
Beispiel #22
0
def test_setter_updates_exception_and_error_middleware(app: App):
    app.debug = True
    assert app.debug is True
    assert app.exception_middleware.debug is True
    assert app.server_error_middleware.debug is True
Beispiel #23
0
def test_if_debug_then_app_given_as_import_string(app: App):
    def run(app_obj, **kwargs):
        assert isinstance(app_obj, str)
        assert app_obj.endswith(":app")

    app.run(debug=True, _run=run)
Beispiel #24
0
def test_sessions_enabled_secret_key_empty():
    with override_env("SECRET_KEY", ""):
        with pytest.raises(MissingSecretKey):
            App(enable_sessions=True)
Beispiel #25
0
def test_declared_as(app: App):
    def run(app_obj, **kwargs):
        assert app_obj.endswith(":application")

    app.run(debug=True, declared_as="application", _run=run)
Beispiel #26
0
def test_sessions_enabled_no_secret_key():
    with pytest.raises(MissingSecretKey):
        App(enable_sessions=True)
Beispiel #27
0
from bocadillo import App

app = App()


@app.route("/")
async def index(req, res):
    res.text = "Hello, from Heroku!"


if __name__ == "__main__":
    app.run()
Beispiel #28
0
        # HTTP error responses!
        raise HTTPError(400, detail="Invalid JSON")

    # NOTE: you're free to integrate a third-party
    # validation library such as Marshmallow or jsonschema.
    for field in "name", "teacher":
        if field not in data:
            raise HTTPError(400, detail=f"{field} is a required field")


# Now, let's assemble the actual application, shall we?

app = App(
    # Built-in CORS, HSTS and GZip!
    enable_cors=True,
    enable_hsts=False,  # the default
    enable_gzip=True,
    gzip_min_size=1024,  # the default
)

# Register the token middleware.
app.add_middleware(TokenMiddleware)

# Instanciate helper backends.
storage = Storage()
analytics = Analytics()

# Jinja templates!
templates = Templates(app)

Beispiel #29
0
from bocadillo import App

app = App()


@app.websocket_route("/conversation")
async def converse(ws, diego, save_client):
    with save_client(ws):
        async for message in ws:
            response = diego.get_response(message)
            await ws.send(str(response))


@app.route("/client-count")
async def client_count(req, res, clients):
    res.media = {"count": len(clients)}
Beispiel #30
0
from bocadillo import App, view, provider

app = App(enable_sessions=True)


@provider(scope="app", name="todos")
def provide_todos():
    return [
        {"id": 0, "content": "Go shopping"},
        {"id": 1, "content": "Cook fries"},
        {"id": 2, "content": "Do laundry"},
    ]


@app.route("/unseen-todos")
async def get_unseen_todos(req, res, todos):
    last_id = req.session.get("last_id", -1)
    unseen_todos = todos[last_id + 1 :]

    if unseen_todos:
        req.session["last_id"] = unseen_todos[-1]["id"]

    res.media = unseen_todos


@app.route("/todos")
@view(methods=["post"])
async def create_todo(req, res, todos):
    json = await req.json()
    todo = {"id": len(todos) + 1, "content": json["content"]}
    todos.append(todo)