コード例 #1
0
def test_only_specified_methods_are_allowed(app, client):
    @app.route("/", methods=["get"])
    def home(req, resp):
        resp.text = "Welcome Home."

    get_response = client.get(url("/"))
    assert get_response.status_code == 200

    with pytest.raises(HTTPError):
        client.post(url("/"))
コード例 #2
0
def test_exception_is_propogated_if_no_exc_handler_is_defined(app, client):
    @app.route("/")
    def index(req, resp):
        raise AttributeError()

    with pytest.raises(AttributeError):
        client.get(url("/"))
コード例 #3
0
def test_status_code(app, client):
    @app.route("/cool")
    def cool(req, resp):
        resp.text = "cool thing"
        resp.status_code = 215

    assert client.get(url("/cool")).status_code == 215
コード例 #4
0
def test_empty_methods(app, client):
    @app.route("/")
    def home(req, resp):
        resp.text = "Welcome Home."

    response = client.get(url("/"))

    assert response.status_code == 200
コード例 #5
0
def test_class_based_handler_not_allowed_method(app, client):
    @app.route("/book")
    class BookResource:
        def post(self, req, resp):
            resp.text = "yolo"

    with pytest.raises(HTTPError):
        client.get(url("/book"))
コード例 #6
0
def test_class_based_handler_get(app, client):
    response_text = "this is a get request"

    @app.route("/book")
    class BookResource:
        def get(self, req, resp):
            resp.text = response_text

    assert client.get(url("/book")).text == response_text
コード例 #7
0
def test_cb_handlers_ignore_route_methods(app, client):
    response_text = "this is a get request"

    @app.route("/book", methods=["post"])
    class BookResource:
        def get(self, req, resp):
            resp.text = response_text

    assert client.get(url("/book")).text == response_text
コード例 #8
0
def test_manually_setting_body(app, client):
    @app.route("/body")
    def text_handler(req, resp):
        resp.body = b"Byte Body"
        resp.content_type = "text/plain"

    response = client.get(url("/body"))

    assert "text/plain" in response.headers["Content-Type"]
    assert response.text == "Byte Body"
コード例 #9
0
def test_json_response_helper(app, client):
    @app.route("/json")
    def json_handler(req, resp):
        resp.json = {"name": "alcazar"}

    response = client.get(url("/json"))
    json_body = response.json()

    assert response.headers["Content-Type"] == "application/json"
    assert json_body["name"] == "alcazar"
コード例 #10
0
def test_html_response_helper(app, client):
    @app.route("/html")
    def html_handler(req, resp):
        resp.html = app.template("example.html", context={"title": "Best Title", "body": "Best Body"})

    response = client.get(url("/html"))

    assert "text/html" in response.headers["Content-Type"]
    assert "Best Title" in response.text
    assert "Best Body" in response.text
コード例 #11
0
def test_text_response_helper(app, client):
    response_text = "Just Plain Text"

    @app.route("/text")
    def text_handler(req, resp):
        resp.text = response_text

    response = client.get(url("/text"))

    assert "text/plain" in response.headers["Content-Type"]
    assert response.text == response_text
コード例 #12
0
def test_custom_error_handler(app, client):
    def on_exception(req, resp, exc):
        resp.text = "AttributeErrorHappened"

    app.add_exception_handler(on_exception)

    @app.route("/")
    def index(req, resp):
        raise AttributeError()

    response = client.get(url("/"))

    assert response.text == "AttributeErrorHappened"
コード例 #13
0
def test_parameterized_route(app, client):
    @app.route("/{name}")
    def hello(req, resp, name):
        resp.text = f"hey {name}"

    assert client.get(url("/matthew")).text == "hey matthew"