Beispiel #1
0
 def test_happy(self) -> None:
     """Tests the happy path: css case."""
     prefix = util.Config.get_uri_prefix()
     content, content_type = webframe.handle_static(prefix +
                                                    "/static/osm.css")
     self.assertTrue(len(content))
     self.assertEqual(content_type, "text/css")
Beispiel #2
0
 def test_javascript(self) -> None:
     """Tests the javascript case."""
     prefix = util.Config.get_uri_prefix()
     content, content_type = webframe.handle_static(prefix +
                                                    "/static/sorttable.js")
     self.assertTrue(len(content))
     self.assertEqual(content_type, "application/x-javascript")
Beispiel #3
0
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"),
                                  [])
Beispiel #4
0
 def test_else(self) -> None:
     """Tests the case when the content type is not recognized."""
     prefix = util.Config.get_uri_prefix()
     content, content_type = webframe.handle_static(prefix +
                                                    "/static/test.xyz")
     self.assertFalse(len(content))
     self.assertFalse(len(content_type))
Beispiel #5
0
 def test_svg(self) -> None:
     """Tests the svg case."""
     content, content_type, extra_headers = webframe.handle_static("/favicon.svg")
     self.assertTrue(len(content))
     self.assertEqual(content_type, "image/svg+xml")
     self.assertEqual(len(extra_headers), 1)
     self.assertEqual(extra_headers[0][0], "Last-Modified")
Beispiel #6
0
 def test_generated_javascript(self) -> None:
     """Tests the generated javascript case."""
     prefix = config.Config.get_uri_prefix()
     content, content_type, extra_headers = webframe.handle_static(prefix + "/static/bundle.js")
     self.assertEqual("// bundle.js\n", content.decode("utf-8"))
     self.assertEqual(content_type, "application/x-javascript")
     self.assertEqual(len(extra_headers), 1)
     self.assertEqual(extra_headers[0][0], "Last-Modified")
Beispiel #7
0
 def test_else(self) -> None:
     """Tests the case when the content type is not recognized."""
     prefix = config.Config.get_uri_prefix()
     content, content_type, extra_headers = webframe.handle_static(prefix + "/static/test.xyz")
     self.assertFalse(len(content))
     self.assertFalse(len(content_type))
     # No last modified non-existing file.
     self.assertEqual(len(extra_headers), 0)
Beispiel #8
0
 def test_json(self) -> None:
     """Tests the json case."""
     prefix = config.Config.get_uri_prefix()
     content, content_type, extra_headers = webframe.handle_static(prefix + "/static/stats-empty.json")
     self.assertTrue(content.decode("utf-8").startswith("{"))
     self.assertEqual(content_type, "application/json")
     self.assertEqual(len(extra_headers), 1)
     self.assertEqual(extra_headers[0][0], "Last-Modified")
Beispiel #9
0
 def test_json(self) -> None:
     """Tests the json case."""
     prefix = config.Config.get_uri_prefix()
     with unittest.mock.patch('config.get_abspath', get_abspath):
         content, content_type = webframe.handle_static(
             prefix + "/static/stats.json")
         self.assertEqual(content, "{}\n")
         self.assertEqual(content_type, "application/json")
Beispiel #10
0
 def test_happy(self) -> None:
     """Tests the happy path: css case."""
     prefix = config.Config.get_uri_prefix()
     content, content_type, extra_headers = webframe.handle_static(prefix + "/static/osm.min.css")
     self.assertTrue(len(content))
     self.assertEqual(content_type, "text/css")
     self.assertEqual(len(extra_headers), 1)
     self.assertEqual(extra_headers[0][0], "Last-Modified")
Beispiel #11
0
 def test_ico(self) -> None:
     """Tests the ico case."""
     ctx = test_context.make_test_context()
     content, content_type, extra_headers = webframe.handle_static(
         ctx, "/favicon.ico")
     self.assertTrue(len(content))
     self.assertEqual(content_type, "image/x-icon")
     self.assertEqual(len(extra_headers), 1)
     self.assertEqual(extra_headers[0][0], "Last-Modified")
Beispiel #12
0
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(util.Config.get_workdir())

    request_uri = get_request_uri(environ, relations)
    _, _, ext = request_uri.partition('.')

    if ext in ("txt", "chkl"):
        return our_application_txt(start_response, relations, request_uri)

    prefix = util.Config.get_uri_prefix()
    if request_uri.startswith(prefix + "/static/"):
        output, content_type = webframe.handle_static(request_uri)
        return webframe.send_response(start_response, content_type, "200 OK",
                                      output, [])

    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 = 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(start_response, "text/html", "200 OK",
                                  doc.getvalue(), [])
 def test_else(self) -> None:
     """Tests the case when the content type is not recognized."""
     content, content_type = webframe.handle_static("/osm/static/test.xyz")
     self.assertFalse(len(content))
     self.assertFalse(len(content_type))
 def test_happy(self) -> None:
     """Tests the happy path: css case."""
     content, content_type = webframe.handle_static("/osm/static/osm.css")
     self.assertTrue(len(content))
     self.assertEqual(content_type, "text/css")
 def test_javascript(self) -> None:
     """Tests the javascript case."""
     content, content_type = webframe.handle_static(
         "/osm/static/sorttable.js")
     self.assertTrue(len(content))
     self.assertEqual(content_type, "application/x-javascript")
Beispiel #16
0
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()
Beispiel #17
0
 def test_json(self) -> None:
     """Tests the json case."""
     prefix = config.Config.get_uri_prefix()
     content, content_type = webframe.handle_static(prefix + "/static/stats-empty.json")
     self.assertTrue(content.startswith("{"))
     self.assertEqual(content_type, "application/json")