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("/"))
def test_exception_is_propogated_if_no_exc_handler_is_defined(app, client): @app.route("/") def index(req, resp): raise HTTPError(404) with raises(HTTPError): client.get(url("/"))
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("/"))
def test_middleware_methods_are_called(app, client): process_request_called = False process_response_called = False class CallMiddlewareMethods(Middleware): def __init__(self, app): super().__init__(app) def process_request(self, req): nonlocal process_request_called process_request_called = True def process_response(self, req, resp): nonlocal process_response_called process_response_called = True app.add_middleware(CallMiddlewareMethods) @app.route('/') def index(req, res): res.text = "YOLO" client.get(url('/')) assert process_request_called is True assert process_response_called is True
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
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
def test_class_based_handler_not_allowed_method(app, client): @app.route("/book") class BookResource: def post(self, req, resp): resp.text = "yolo" with raises(HTTPError): client.get(url("/book"))
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, **kwargs): resp.text = response_text assert client.get(url("/book/1")).text == response_text
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
def test_class_based_handler_post(app, client): response_text = "this is a post request" @app.route("/book") class BookResource: def put(self, req, resp, pk): resp.text = response_text assert client.put(url("/book/1")).text == response_text
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"
def test_json_response_helper(app, client): @app.route("/json") def json_handler(req, resp): resp.json = {"name": "ridaakh"} response = client.get(url("/json")) json_body = response.json() assert response.headers["Content-Type"] == "application/json" assert json_body["name"] == "ridaakh"
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
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"
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
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"