def test_no_path(self) -> None: """Tests the case when PATH_INFO is empty (should give the main page).""" environ = { "PATH_INFO": "" } ret = webframe.get_request_uri(environ, get_relations()) self.assertEqual(ret, "")
def our_application( environ: Dict[str, Any], start_response: 'StartResponse' ) -> Iterable[bytes]: """Dispatches the request based on its URI.""" util.set_locale() language = util.setup_localization(environ) relations = areas.Relations(config.Config.get_workdir()) request_uri = webframe.get_request_uri(environ, relations) _, _, ext = request_uri.partition('.') if ext in ("txt", "chkl"): return our_application_txt(environ, start_response, relations, request_uri) prefix = config.Config.get_uri_prefix() found = request_uri == "/" or request_uri.startswith(prefix) if not found: doc = webframe.handle_404() return webframe.send_response(environ, start_response, webframe.ResponseProperties("text/html", "404 Not Found"), doc.getvalue().encode("utf-8"), []) if request_uri.startswith(prefix + "/static/") or request_uri.endswith("favicon.ico"): output, content_type, extra_headers = webframe.handle_static(request_uri) return webframe.send_response(environ, start_response, webframe.ResponseProperties(content_type, "200 OK"), output, extra_headers) if ext == "json": return wsgi_json.our_application_json(environ, start_response, relations, request_uri) doc = yattag.doc.Doc() util.write_html_header(doc) with doc.tag("html", lang=language): write_html_head(doc, get_html_title(request_uri)) with doc.tag("body"): no_such_relation = webframe.check_existing_relation(relations, request_uri) handler = get_handler(request_uri) if no_such_relation.getvalue(): doc.asis(no_such_relation.getvalue()) elif handler: doc.asis(handler(relations, request_uri).getvalue()) elif request_uri.startswith(prefix + "/webhooks/github"): doc.asis(handle_github_webhook(environ).getvalue()) else: doc.asis(handle_main(request_uri, relations).getvalue()) return webframe.send_response(environ, start_response, webframe.ResponseProperties("text/html", "200 OK"), doc.getvalue().encode("utf-8"), [])
def test_no_path(self) -> None: """Tests the case when PATH_INFO is empty (should give the main page).""" environ = { "PATH_INFO": "" } ctx = test_context.make_test_context() relations = areas.Relations(test_context.make_test_context()) ret = webframe.get_request_uri(environ, ctx, relations) self.assertEqual(ret, "")
def test_no_path(self) -> None: """Tests the case when PATH_INFO is empty (should give the main page).""" with unittest.mock.patch('config.get_abspath', get_abspath): environ = {"PATH_INFO": ""} ret = webframe.get_request_uri(environ, get_relations()) self.assertEqual(ret, "")
def our_application(environ: Dict[str, Any], start_response: 'StartResponse', ctx: context.Context) -> Tuple[Iterable[bytes], str]: """Dispatches the request based on its URI.""" try: language = util.setup_localization(environ, ctx) relations = areas.Relations(ctx) request_uri = webframe.get_request_uri(environ, ctx, relations) _, _, ext = request_uri.partition('.') if ext in ("txt", "chkl"): return our_application_txt(environ, start_response, ctx, relations, request_uri), str() if not (request_uri == "/" or request_uri.startswith(ctx.get_ini().get_uri_prefix())): doc = webframe.handle_404() response = webframe.Response("text/html", "404 Not Found", doc.getvalue().encode("utf-8"), []) return webframe.send_response(environ, start_response, response), str() if request_uri.startswith(ctx.get_ini().get_uri_prefix() + "/static/") or \ request_uri.endswith("favicon.ico") or request_uri.endswith("favicon.svg"): output, content_type, headers = webframe.handle_static( ctx, request_uri) return webframe.send_response( environ, start_response, webframe.Response(content_type, "200 OK", output, headers)), str() if ext == "json": return wsgi_json.our_application_json(environ, start_response, ctx, relations, request_uri), str() doc = yattag.doc.Doc() util.write_html_header(doc) with doc.tag("html", lang=language): write_html_head(ctx, doc, get_html_title(request_uri)) with doc.tag("body"): no_such_relation = webframe.check_existing_relation( ctx, relations, request_uri) handler = get_handler(ctx, request_uri) if no_such_relation.getvalue(): doc.asis(no_such_relation.getvalue()) elif handler: doc.asis(handler(ctx, relations, request_uri).getvalue()) elif request_uri.startswith(ctx.get_ini().get_uri_prefix() + "/webhooks/github"): doc.asis( webframe.handle_github_webhook(environ, ctx).getvalue()) else: doc.asis( handle_main(request_uri, ctx, relations).getvalue()) err = ctx.get_unit().make_error() if err: return [], err return webframe.send_response( environ, start_response, webframe.Response("text/html", "200 OK", doc.getvalue().encode("utf-8"), [])), err # pylint: disable=broad-except except Exception: # pragma: no cover return [], traceback.format_exc()