|
17 | 17 | import unittest
|
18 | 18 | import unittest.mock as mock
|
19 | 19 | import wsgiref.util as wsgiref_util
|
| 20 | +from urllib.parse import urlparse |
20 | 21 |
|
21 | 22 | from opentelemetry import trace as trace_api
|
22 | 23 | from opentelemetry.ext.wsgi import OpenTelemetryMiddleware
|
@@ -191,35 +192,72 @@ def test_request_attributes(self):
|
191 | 192 | self.assertEqual(self.span.set_attribute.call_count, len(expected))
|
192 | 193 | self.span.set_attribute.assert_has_calls(expected, any_order=True)
|
193 | 194 |
|
194 |
| - def test_request_attributes_with_partial_raw_uri(self): |
195 |
| - self.environ["RAW_URI"] = "/#top" |
| 195 | + def validate_url(self, expected_url): |
196 | 196 | OpenTelemetryMiddleware._add_request_attributes( # noqa pylint: disable=protected-access
|
197 | 197 | self.span, self.environ
|
198 | 198 | )
|
199 |
| - self.span.set_attribute.assert_any_call( |
200 |
| - "http.url", "http://127.0.0.1/#top" |
201 |
| - ) |
| 199 | + attrs = {args[0][0]: args[0][1] for args in self.span.set_attribute.call_args_list} |
| 200 | + self.assertIn("http.url", attrs) |
| 201 | + self.assertEqual(attrs["http.url"], expected_url) |
| 202 | + self.assertIn("http.host", attrs) |
| 203 | + self.assertEqual(attrs["http.host"], urlparse(attrs["http.url"]).netloc) |
| 204 | + |
| 205 | + |
| 206 | + def test_request_attributes_with_partial_raw_uri(self): |
| 207 | + self.environ["RAW_URI"] = "/#top" |
| 208 | + self.validate_url("http://127.0.0.1/#top") |
202 | 209 |
|
203 | 210 | def test_request_attributes_with_partial_raw_uri_and_nonstandard_port(
|
204 | 211 | self
|
205 | 212 | ):
|
206 |
| - self.environ["RAW_URI"] = "/#top" |
| 213 | + self.environ["RAW_URI"] = "/?" |
| 214 | + del self.environ["HTTP_HOST"] |
207 | 215 | self.environ["SERVER_PORT"] = "8080"
|
208 |
| - OpenTelemetryMiddleware._add_request_attributes( # noqa pylint: disable=protected-access |
209 |
| - self.span, self.environ |
210 |
| - ) |
211 |
| - self.span.set_attribute.assert_any_call( |
212 |
| - "http.url", "http://127.0.0.1:8080/#top" |
213 |
| - ) |
| 216 | + self.validate_url("http://127.0.0.1:8080/?") |
| 217 | + |
| 218 | + def test_request_attributes_with_nonstandard_port_and_no_host( |
| 219 | + self |
| 220 | + ): |
| 221 | + del self.environ["HTTP_HOST"] |
| 222 | + self.environ["SERVER_PORT"] = "8080" |
| 223 | + self.validate_url("http://127.0.0.1:8080/") |
| 224 | + |
| 225 | + def test_request_attributes_with_nonstandard_port( |
| 226 | + self |
| 227 | + ): |
| 228 | + self.environ["HTTP_HOST"] += ":8080" |
| 229 | + self.validate_url("http://127.0.0.1:8080/") |
| 230 | + |
| 231 | + def test_request_attributes_with_scheme_relative_raw_uri( |
| 232 | + self |
| 233 | + ): |
| 234 | + self.environ["RAW_URI"] = "//127.0.0.1/?" |
| 235 | + self.validate_url("http://127.0.0.1/?") |
| 236 | + |
| 237 | + def test_request_attributes_with_netlocless_raw_uri( |
| 238 | + self |
| 239 | + ): |
| 240 | + self.environ["RAW_URI"] = "http:///?" |
| 241 | + self.validate_url("http://127.0.0.1/?") |
| 242 | + |
| 243 | + def test_request_attributes_with_pathless_raw_uri( |
| 244 | + self |
| 245 | + ): |
| 246 | + self.environ["RAW_URI"] = "http://hello" |
| 247 | + self.environ["HTTP_HOST"] = "hello" |
| 248 | + self.validate_url("http://hello") |
| 249 | + |
| 250 | + def test_request_attributes_with_strange_raw_uri( |
| 251 | + self |
| 252 | + ): |
| 253 | + self.environ["RAW_URI"] = "http://?" |
| 254 | + self.validate_url("http://127.0.0.1?") |
| 255 | + |
214 | 256 |
|
215 | 257 | def test_request_attributes_with_full_request_uri(self):
|
216 |
| - self.environ["REQUEST_URI"] = "http://foobar.com:8080/?foo=bar#top" |
217 |
| - OpenTelemetryMiddleware._add_request_attributes( # noqa pylint: disable=protected-access |
218 |
| - self.span, self.environ |
219 |
| - ) |
220 |
| - self.span.set_attribute.assert_any_call( |
221 |
| - "http.url", "http://foobar.com:8080/?foo=bar#top" |
222 |
| - ) |
| 258 | + self.environ["HTTP_HOST"] = "127.0.0.1:8080" |
| 259 | + self.environ["REQUEST_URI"] = "http://127.0.0.1:8080/?foo=bar#top" |
| 260 | + self.validate_url("http://127.0.0.1:8080/?foo=bar#top") |
223 | 261 |
|
224 | 262 | def test_response_attributes(self):
|
225 | 263 | OpenTelemetryMiddleware._add_response_attributes( # noqa pylint: disable=protected-access
|
|
0 commit comments