File: test_http_proxy.py

package info (click to toggle)
python-werkzeug 1.0.1%2Bdfsg1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,888 kB
  • sloc: python: 21,897; javascript: 173; makefile: 36; xml: 16
file content (57 lines) | stat: -rw-r--r-- 1,655 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
from werkzeug.middleware.http_proxy import ProxyMiddleware
from werkzeug.test import Client
from werkzeug.urls import url_parse
from werkzeug.wrappers import BaseResponse


def test_http_proxy(dev_server):
    server = dev_server(
        r"""
        from werkzeug.wrappers import Request, Response

        @Request.application
        def app(request):
            return Response(u'%s|%s|%s' % (
                request.headers.get('X-Special'),
                request.environ['HTTP_HOST'],
                request.full_path,
            ))
        """
    )

    app = ProxyMiddleware(
        BaseResponse("ROOT"),
        {
            "/foo": {
                "target": server.url,
                "host": "faked.invalid",
                "headers": {"X-Special": "foo"},
            },
            "/bar": {
                "target": server.url,
                "host": None,
                "remove_prefix": True,
                "headers": {"X-Special": "bar"},
            },
            "/autohost": {"target": server.url},
        },
    )

    client = Client(app, response_wrapper=BaseResponse)

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

    rv = client.get("/foo/bar")
    assert rv.data.decode("ascii") == "foo|faked.invalid|/foo/bar?"

    rv = client.get("/bar/baz")
    assert rv.data.decode("ascii") == "bar|localhost|/baz?"

    rv = client.get("/autohost/aha")
    expected = "None|%s|/autohost/aha?" % url_parse(server.url).ascii_host
    assert rv.data.decode("ascii") == expected

    # test query string
    rv = client.get("/bar/baz?a=a&b=b")
    assert rv.data.decode("ascii") == "bar|localhost|/baz?a=a&b=b"