File: test_http_proxy.py

package info (click to toggle)
python-werkzeug 2.2.2-3%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,248 kB
  • sloc: python: 22,177; javascript: 304; makefile: 32; xml: 16; sh: 10
file content (52 lines) | stat: -rw-r--r-- 1,587 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
import pytest

from werkzeug.middleware.http_proxy import ProxyMiddleware
from werkzeug.test import Client
from werkzeug.wrappers import Response


@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning")
@pytest.mark.dev_server
def test_http_proxy(standard_app):
    app = ProxyMiddleware(
        Response("ROOT"),
        {
            "/foo": {
                "target": standard_app.url,
                "host": "faked.invalid",
                "headers": {"X-Special": "foo"},
            },
            "/bar": {
                "target": standard_app.url,
                "host": None,
                "remove_prefix": True,
                "headers": {"X-Special": "bar"},
            },
            "/autohost": {"target": standard_app.url},
        },
    )

    client = Client(app)

    r = client.get("/")
    assert r.data == b"ROOT"

    r = client.get("/foo/bar")
    assert r.json["HTTP_X_SPECIAL"] == "foo"
    assert r.json["HTTP_HOST"] == "faked.invalid"
    assert r.json["PATH_INFO"] == "/foo/bar"

    r = client.get("/bar/baz?a=a&b=b")
    assert r.json["HTTP_X_SPECIAL"] == "bar"
    assert r.json["HTTP_HOST"] == "localhost"
    assert r.json["PATH_INFO"] == "/baz"
    assert r.json["QUERY_STRING"] == "a=a&b=b"

    r = client.get("/autohost/aha")
    assert "HTTP_X_SPECIAL" not in r.json
    assert r.json["HTTP_HOST"] == "127.0.0.1"
    assert r.json["PATH_INFO"] == "/autohost/aha"

    # test if characters allowed in URL are not encoded by proxy
    r = client.get("/autohost/$")
    assert r.json["REQUEST_URI"] == "/autohost/$"