def test_parameterized_route(sego, client): @sego.route(Route("main", Verb.HTTP_GET, "", "", "/{name}")) def hello(req, resp, name): resp.text = f"hey {name}" assert client.get("http://segotestserver/kabelo").text == "hey kabelo" assert client.get("http://segotestserver/kgotso").text == "hey kgotso"
def test_custom_exception_handler(sego, client): def on_exception(req, resp, exc): resp.text = "AttributeErrorHappened" sego.add_exception_handler(AttributeError, on_exception) @sego.route( Route("custom_exception", Verb.HTTP_GET, "", "", "/custom_exception")) def index(req, resp): raise AttributeError() response = client.get("http://segotestserver/custom_exception") assert response.text == "AttributeErrorHappened"
def test_client_can_send_requests(sego, client): RESPONSE_TEXT = "SEGO IS ALIVE" @sego.route( Route(name="client_test", verb=Verb.HTTP_GET, controller="", action="", url="/client_test/")) def test_handler(req, resp): resp.text = RESPONSE_TEXT assert client.get( "http://segotestserver/client_test/").text == RESPONSE_TEXT
def test_template(views, sego, client): @sego.route(Route("html", Verb.HTTP_GET, "", "", "/html")) def html_handler(req, resp): # views = sego.get_view_environment() resp.body = views.render_view("tests/index.html", context={ "title": "Some Title", "name": "Some Name" }).encode() response = client.get("http://segotestserver/html") print(response) assert "text/html" in response.headers["Content-Type"] assert "Some Title" in response.text assert "Some Name" in response.text
def test_route_overlap_throws_exception(router): router.add_route( Route("home", Verb.HTTP_GET, "HomeController", "index", "/home")) with pytest.raises(RouteAlreadyExistsException): router.add_route( Route("home", Verb.HTTP_GET, "HomeController", "index", "/home"))
def test_basic_route_adding(router): router.add_route( Route("test", Verb.HTTP_GET, "TestController", "index", "/test/"))
def route(): return Route()
router = Router() # route object # EXAMPLE route route_name = "homepage" # The string name for this route instance # http_method = Verb.HTTP_POST # The HTTP verb we are expecting http_method = Verb.HTTP_GET # The HTTP verb we are expecting controller = "HomeController" # The controller, to handle custom routing logic action = "index" # The specific action the controller will execute url = "/" # The path we are expecting # A route is defined homepage_route = Route(name=route_name, \ verb=http_method, \ controller=controller, \ action=action, \ url=url).set_middleware(Middleware.PREPROCESS,['example3']) # add the route to the router router.add_route(homepage_route) # Flask style routing EXAMPLE @sego.route(Route("framework_name", Verb.HTTP_GET, "", "", "/framework/{name}")) def html_handler(req, resp, name): views = sego.get_view_environment() resp.text = views.render_view("tests/index.html", context={ "name": name,