File: test_base.py

package info (click to toggle)
quart 0.20.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,892 kB
  • sloc: python: 8,644; makefile: 42; sh: 17; sql: 6
file content (145 lines) | stat: -rw-r--r-- 4,070 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from __future__ import annotations

from base64 import b64encode

import pytest
from hypercorn.typing import HTTPScope
from werkzeug.datastructures import Headers

from quart.wrappers.base import BaseRequestWebsocket


def test_basic_authorization(http_scope: HTTPScope) -> None:
    headers = Headers()
    headers["Authorization"] = "Basic {}".format(
        b64encode(b"identity:secret").decode("ascii")
    )
    request = BaseRequestWebsocket(
        "GET", "http", "/", b"", headers, "", "1.1", http_scope
    )
    auth = request.authorization
    assert auth.username == "identity"
    assert auth.password == "secret"


def test_digest_authorization(http_scope: HTTPScope) -> None:
    headers = Headers()
    headers["Authorization"] = (
        "Digest "
        'username="identity", '
        'realm="realm@rea.lm", '
        'nonce="abcd1234", '
        'uri="/path", '
        'response="abcd1235", '
        'opaque="abcd1236"'
    )
    request = BaseRequestWebsocket(
        "GET", "http", "/", b"", headers, "", "1.1", http_scope
    )
    auth = request.authorization
    assert auth.username == "identity"
    assert auth.realm == "realm@rea.lm"
    assert auth.nonce == "abcd1234"
    assert auth.uri == "/path"
    assert auth.response == "abcd1235"
    assert auth.opaque == "abcd1236"


@pytest.mark.parametrize(
    "method, scheme, host, path, query_string,"
    "expected_path, expected_full_path, expected_url, expected_base_url,"
    "expected_url_root, expected_host_url",
    [
        (
            "GET",
            "http",
            "quart.com",
            "/",
            b"",
            "/",
            "/?",
            "http://quart.com/",
            "http://quart.com/",
            "http://quart.com/",
            "http://quart.com/",
        ),
        (
            "GET",
            "http",
            "quart.com",
            "/",
            b"a=b",
            "/",
            "/?a=b",
            "http://quart.com/?a=b",
            "http://quart.com/",
            "http://quart.com/",
            "http://quart.com/",
        ),
        (
            "GET",
            "https",
            "quart.com",
            "/branch/leaf",
            b"a=b",
            "/branch/leaf",
            "/branch/leaf?a=b",
            "https://quart.com/branch/leaf?a=b",
            "https://quart.com/branch/leaf",
            "https://quart.com/",
            "https://quart.com/",
        ),
    ],
)
def test_url_structure(
    method: str,
    scheme: str,
    host: str,
    path: str,
    query_string: bytes,
    expected_path: str,
    expected_full_path: str,
    expected_url: str,
    expected_base_url: str,
    expected_url_root: str,
    expected_host_url: str,
    http_scope: HTTPScope,
) -> None:
    base_request_websocket = BaseRequestWebsocket(
        method,
        scheme,
        path,
        query_string,
        Headers({"host": host}),
        "",
        "1.1",
        http_scope,
    )

    assert base_request_websocket.path == expected_path
    assert base_request_websocket.query_string == query_string
    assert base_request_websocket.full_path == expected_full_path
    assert base_request_websocket.url == expected_url
    assert base_request_websocket.base_url == expected_base_url
    assert base_request_websocket.url_root == expected_url_root
    assert base_request_websocket.host_url == expected_host_url
    assert base_request_websocket.host == host
    assert base_request_websocket.method == method
    assert base_request_websocket.scheme == scheme
    assert base_request_websocket.is_secure == scheme.endswith("s")


def test_query_string(http_scope: HTTPScope) -> None:
    base_request_websocket = BaseRequestWebsocket(
        "GET",
        "http",
        "/",
        b"a=b&a=c&f",
        Headers({"host": "localhost"}),
        "",
        "1.1",
        http_scope,
    )
    assert base_request_websocket.query_string == b"a=b&a=c&f"
    assert base_request_websocket.args.getlist("a") == ["b", "c"]
    assert base_request_websocket.args["f"] == ""