File: testsite.py

package info (click to toggle)
python-scrapy 2.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,664 kB
  • sloc: python: 52,028; xml: 199; makefile: 25; sh: 7
file content (47 lines) | stat: -rw-r--r-- 1,437 bytes parent folder | download
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
from urllib.parse import urljoin

from twisted.web import resource, server, static, util


class SiteTest:
    def setUp(self):
        from twisted.internet import reactor

        super().setUp()
        self.site = reactor.listenTCP(0, test_site(), interface="127.0.0.1")
        self.baseurl = f"http://localhost:{self.site.getHost().port}/"

    def tearDown(self):
        super().tearDown()
        self.site.stopListening()

    def url(self, path: str) -> str:
        return urljoin(self.baseurl, path)


class NoMetaRefreshRedirect(util.Redirect):
    def render(self, request: server.Request) -> bytes:
        content = util.Redirect.render(self, request)
        return content.replace(
            b'http-equiv="refresh"', b'http-no-equiv="do-not-refresh-me"'
        )


def test_site():
    r = resource.Resource()
    r.putChild(b"text", static.Data(b"Works", "text/plain"))
    r.putChild(
        b"html",
        static.Data(
            b"<body><p class='one'>Works</p><p class='two'>World</p></body>",
            "text/html",
        ),
    )
    r.putChild(
        b"enc-gb18030",
        static.Data(b"<p>gb18030 encoding</p>", "text/html; charset=gb18030"),
    )
    r.putChild(b"redirect", util.Redirect(b"/redirected"))
    r.putChild(b"redirect-no-meta-refresh", NoMetaRefreshRedirect(b"/redirected"))
    r.putChild(b"redirected", static.Data(b"Redirected here", "text/plain"))
    return server.Site(r)