File size: 2,551 Bytes
fec4e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from starlette.applications import Starlette  # noqa: E402
from starlette.responses import HTMLResponse, JSONResponse  # noqa: E402
from starlette.routing import Route  # noqa: E402
from starlette.testclient import TestClient  # noqa: E402

import gradio_runtime  # noqa: E402

GRADIO_INDEX = """<!doctype html><html><head>
<meta property="og:title" content="Gradio" />
<meta name="twitter:creator" content="@Gradio" />
<title>Gradio</title>
</head><body>app</body></html>"""
TITLE = "Pocket 3D AI | SkyeBrowse"
HEAD = '<link rel="canonical" href="https://3dai.skyebrowse.com/" />'
GOOGLEBOT = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
CHROME = "Mozilla/5.0 (Macintosh) AppleWebKit/537.36 Chrome/141.0 Safari/537.36"


def served_app(title=TITLE):
    kwargs = gradio_runtime.build_launch_kwargs(None, "", "", HEAD, title=title)
    routes = [
        Route("/", lambda request: HTMLResponse(GRADIO_INDEX)),
        Route("/config", lambda request: JSONResponse({"head": kwargs["head"]})),
    ]
    return TestClient(Starlette(routes=routes, **kwargs["app_kwargs"]), follow_redirects=False)


class DocumentHeadTest(unittest.TestCase):
    def test_page_html_carries_title_and_head_without_gradio_defaults(self):
        html = served_app().get("/?__theme=light", headers={"Accept": "text/html"}).text
        self.assertIn(f"<title>{TITLE}</title>", html)
        self.assertIn(HEAD, html)
        self.assertNotIn('content="Gradio"', html)
        self.assertNotIn("@Gradio", html)
        self.assertEqual(html.count("<title>"), 1)

    def test_config_head_is_empty_so_embeds_do_not_inherit_it(self):
        self.assertEqual(served_app().get("/config").json()["head"], "")

    def test_without_title_head_stays_in_config(self):
        self.assertEqual(served_app(title="").get("/config").json()["head"], HEAD)

    def test_browsers_are_redirected_to_light_theme(self):
        response = served_app().get("/", headers={"Accept": "text/html", "User-Agent": CHROME})
        self.assertEqual(response.status_code, 307)
        self.assertIn("__theme=light", response.headers["location"])

    def test_crawlers_get_the_page_without_a_redirect(self):
        response = served_app().get("/", headers={"Accept": "text/html", "User-Agent": GOOGLEBOT})
        self.assertEqual(response.status_code, 200)
        self.assertIn(HEAD, response.text)


if __name__ == "__main__":
    unittest.main()