async def test_status_method_response_in_controller(method, expected_status): app = FakeApplication() app.controllers_router = RoutesRegistry() get = app.controllers_router.get method_name = method.__name__ class Home(Controller): @get("/") def greet(self): controller_method = getattr(Home, method_name, None) return controller_method(self, "Everything's good") controller_method = getattr(Home, method_name, None) assert callable(controller_method) await app.start() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) assert app.response.status == expected_status content = await app.response.text() assert content == "Everything's good"
async def test_pretty_json_response_in_controller(obj: Any, values: Dict[str, Any]): app = FakeApplication() app.controllers_router = RoutesRegistry() get = app.controllers_router.get class Home(Controller): @get("/") def greet(self): return self.pretty_json(obj) await app.start() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response raw = await response.text() assert response.status == 200 content_type = response.headers.get_single(b"content-type") assert content_type == b"application/json" raw = await response.text() data = await response.json() for name, value in values.items(): assert data.get(name) == value if isinstance(value, str): assert f' "{name}": "{value}"' in raw else: assert f' "{name}": ' in raw
async def test_redirect_method_in_controller(method, expected_status): app = FakeApplication() app.controllers_router = RoutesRegistry() get = app.controllers_router.get method_name = method.__name__ class Home(Controller): @get("/") def greet(self): controller_method = getattr(Home, method_name, None) return controller_method(self, "https://foo.org/somewhere") controller_method = getattr(Home, method_name, None) assert callable(controller_method) await app.start() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == expected_status location = response.headers.get_single(b"location") assert location == b"https://foo.org/somewhere"
async def test_status_method_without_body_response_in_controller( method, expected_status ): app = FakeApplication() app.controllers_router = RoutesRegistry() get = app.controllers_router.get method_name = method.__name__ class Home(Controller): @get("/") def greet(self): controller_method = getattr(Home, method_name, None) return controller_method(self) controller_method = getattr(Home, method_name, None) assert callable(controller_method) await app.start() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == expected_status assert response.has_body() is False
async def test_view_methods_in_controller_throw_if_templating_not_configured(): app = FakeApplication() app.controllers_router = RoutesRegistry() get = app.controllers_router.get class Home(Controller): @get("/") def greet(self): self.view("foo") await app.start() home = Home() with pytest.raises(TemplatingNotConfiguredException): home.greet() with pytest.raises(TemplatingNotConfiguredException): await home.view_async("foo")
async def test_status_code_response_with_text_in_controller(status: int): app = FakeApplication() app.controllers_router = RoutesRegistry() get = app.controllers_router.get class Home(Controller): @get("/") def greet(self): return self.status_code(status, "Everything's good") await app.start() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) assert app.response.status == status content = await app.response.text() assert content == "Everything's good"