File: test_web.py

package info (click to toggle)
python-pdoc 15.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,192 kB
  • sloc: python: 8,013; javascript: 1,156; makefile: 18; sh: 3
file content (99 lines) | stat: -rw-r--r-- 2,356 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import io
from pathlib import Path
import socket
import threading

import pytest

from pdoc.web import DocHandler
from pdoc.web import DocServer

here = Path(__file__).parent


class ReadResponse(threading.Thread):
    def __init__(self, sock):
        super().__init__()
        self.sock = sock
        self.data = io.BytesIO()

    def run(self) -> None:
        while True:
            data = self.sock.recv(65536)
            if data:
                self.data.write(data)
            else:
                return


def handle_request(data: bytes) -> bytes:
    server = DocServer(
        ("", 8080),
        specs=[
            "dataclasses",
            str(here / "testdata" / "import_err_simple.py"),
            "jinja2",
            "!jinja2.",
        ],
        bind_and_activate=False,
    )
    a, b = socket.socketpair()
    b.send(data)
    t = ReadResponse(b)
    t.start()
    # noinspection PyTypeChecker
    DocHandler(a, ("127.0.0.1", 54321), server)  # type: ignore
    a.close()
    t.join()
    return t.data.getvalue()


def test_head_index():
    assert b"200 OK" in handle_request(b"HEAD / HTTP/1.1\r\n\r\n")


def test_get_index():
    assert b'<a href="dataclasses.html">' in handle_request(b"GET / HTTP/1.1\r\n\r\n")


def test_get_search_json():
    with pytest.warns(UserWarning, match="Error importing 'import_err_simple'"):
        assert b'"dataclasses.is_dataclass"' in handle_request(
            b"GET /search.js HTTP/1.1\r\n\r\n"
        )


def test_get_module():
    assert b"make_dataclass" in handle_request(
        b"GET /dataclasses.html HTTP/1.1\r\n\r\n"
    )


def test_get_dependency():
    assert b"a template engine written in pure Python" in handle_request(
        b"GET /jinja2.html HTTP/1.1\r\n\r\n"
    )


def test_get_module_err():
    assert b"I fail on import" in handle_request(
        b"GET /import_err_simple.html HTTP/1.1\r\n\r\n"
    )


def test_get_module_mtime():
    assert float(
        handle_request(b"GET /dataclasses.html?mtime=1 HTTP/1.1\r\n\r\n")
        .splitlines()[-1]
        .decode()
    )


def test_get_unknown():
    assert b"404 Not Found" in handle_request(b"GET /unknown HTTP/1.1\r\n\r\n")


def test_get_not_normalized():
    assert b"Not Found: Please normalize all module separators" in handle_request(
        b"GET /module.submodule HTTP/1.1\r\n\r\n"
    )