|  | 
| 7 | 7 | import sentry_sdk | 
| 8 | 8 | from sentry_sdk import capture_message | 
| 9 | 9 | from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware | 
| 10 |  | -from sentry_sdk._werkzeug import get_host | 
|  | 10 | +from sentry_sdk._werkzeug import get_host, _get_headers | 
| 11 | 11 | 
 | 
| 12 | 12 | 
 | 
| 13 | 13 | @pytest.fixture | 
| @@ -76,9 +76,24 @@ def next(self): | 
| 76 | 76 |             "ham", | 
| 77 | 77 |             id="ignore x-forwarded-host", | 
| 78 | 78 |         ), | 
|  | 79 | +        pytest.param( | 
|  | 80 | +            { | 
|  | 81 | +                "SERVER_NAME": "2001:0db8:85a3:0042:1000:8a2e:0370:7334", | 
|  | 82 | +                "SERVER_PORT": "8080", | 
|  | 83 | +            }, | 
|  | 84 | +            "[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:8080", | 
|  | 85 | +            id="IPv6, custom port", | 
|  | 86 | +        ), | 
|  | 87 | +        pytest.param( | 
|  | 88 | +            {"SERVER_NAME": "eggs"}, | 
|  | 89 | +            "eggs", | 
|  | 90 | +            id="name, no port", | 
|  | 91 | +        ), | 
| 79 | 92 |     ), | 
| 80 | 93 | ) | 
|  | 94 | +# | 
| 81 | 95 | # https://github.com/pallets/werkzeug/blob/main/tests/test_wsgi.py#L60 | 
|  | 96 | +# | 
| 82 | 97 | def test_get_host(environ, expect): | 
| 83 | 98 |     environ.setdefault("wsgi.url_scheme", "http") | 
| 84 | 99 |     assert get_host(environ) == expect | 
| @@ -106,26 +121,19 @@ def test_basic(sentry_init, crashing_app, capture_events): | 
| 106 | 121 |     } | 
| 107 | 122 | 
 | 
| 108 | 123 | 
 | 
| 109 |  | -def test_basic_forwarded_host(sentry_init, crashing_app, capture_events): | 
| 110 |  | -    sentry_init(send_default_pii=True) | 
| 111 |  | -    app = SentryWsgiMiddleware(crashing_app, use_x_forwarded_for=True) | 
| 112 |  | -    client = Client(app) | 
| 113 |  | -    events = capture_events() | 
| 114 |  | - | 
| 115 |  | -    with pytest.raises(ZeroDivisionError): | 
| 116 |  | -        client.get("/", environ_overrides={"HTTP_X_FORWARDED_HOST": "foobarbaz:80"}) | 
| 117 |  | - | 
| 118 |  | -    (event,) = events | 
| 119 |  | - | 
| 120 |  | -    assert event["transaction"] == "generic WSGI request" | 
| 121 |  | - | 
| 122 |  | -    assert event["request"] == { | 
| 123 |  | -        "env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"}, | 
| 124 |  | -        "headers": {"Host": "localhost", "X-Forwarded-Host": "foobarbaz:80"}, | 
| 125 |  | -        "method": "GET", | 
| 126 |  | -        "query_string": "", | 
| 127 |  | -        "url": "http://foobarbaz/", | 
| 128 |  | -    } | 
|  | 124 | +@pytest.mark.parametrize( | 
|  | 125 | +    ("environ", "expect"), | 
|  | 126 | +    ( | 
|  | 127 | +        pytest.param( | 
|  | 128 | +            {"CONTENT_TYPE": "text/html", "CONTENT_LENGTH": "0"}, | 
|  | 129 | +            [("Content-Length", "0"), ("Content-Type", "text/html")], | 
|  | 130 | +            id="headers", | 
|  | 131 | +        ), | 
|  | 132 | +    ), | 
|  | 133 | +) | 
|  | 134 | +def test_headers(environ, expect): | 
|  | 135 | +    environ.setdefault("wsgi.url_scheme", "http") | 
|  | 136 | +    assert sorted(_get_headers(environ)) == expect | 
| 129 | 137 | 
 | 
| 130 | 138 | 
 | 
| 131 | 139 | @pytest.mark.parametrize("path_info", ("bark/", "/bark/")) | 
|  | 
0 commit comments