Skip to content

Commit

Permalink
better handling for query_string
Browse files Browse the repository at this point in the history
  • Loading branch information
majorgreys committed May 27, 2020
1 parent bc6f0a9 commit 3849da1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def collect_request_attributes(scope):
server_host = server[0] + (":" + str(port) if port != 80 else "")
full_path = scope.get("root_path", "") + scope.get("path", "")
http_url = scope.get("scheme", "http") + "://" + server_host + full_path
if scope.get("query_string") and http_url:
if isinstance(scope["query_string"], bytes):
http_url = http_url + ("?" + scope.get("query_string").decode("utf8"))
else:
http_url = http_url + ("?" + urllib.parse.unquote(scope.get("query_string")))
query_string = scope.get("query_string")
if query_string and http_url:
if isinstance(query_string, bytes):
query_string = query_string.decode("utf8")
http_url = http_url + ("?" + urllib.parse.unquote(query_string))

result = {
"component": scope["type"],
Expand Down
31 changes: 13 additions & 18 deletions ext/opentelemetry-ext-asgi/tests/test_asgi_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def setUp(self):
setup_testing_defaults(self.scope)
self.span = mock.create_autospec(trace_api.Span, spec_set=True)

def test_query_string_utf8(self):
def test_request_attributes(self):
self.scope["query_string"] = b"foo=bar"

attrs = otel_asgi.collect_request_attributes(self.scope)
Expand All @@ -315,25 +315,20 @@ def test_query_string_utf8(self):
},
)

def test_query_string_percent_encoding(self):
self.scope["query_string"] = urllib.parse.quote(b"foo=bar")
def test_query_string(self):
self.scope["query_string"] = b"foo=bar"
attrs = otel_asgi.collect_request_attributes(self.scope)
self.assertEqual(attrs["http.url"], "http://127.0.0.1/?foo=bar")

def test_query_string_percent_bytes(self):
self.scope["query_string"] = b"foo%3Dbar"
attrs = otel_asgi.collect_request_attributes(self.scope)
self.assertDictEqual(
attrs,
{
"component": "http",
"http.method": "GET",
"http.host": "127.0.0.1",
"http.target": "/",
"http.url": "http://127.0.0.1/?foo=bar",
"host.port": 80,
"http.scheme": "http",
"http.flavor": "1.0",
"net.peer.ip": "127.0.0.1",
"net.peer.port": 32767,
},
)
self.assertEqual(attrs["http.url"], "http://127.0.0.1/?foo=bar")

def test_query_string_percent_str(self):
self.scope["query_string"] = "foo%3Dbar"
attrs = otel_asgi.collect_request_attributes(self.scope)
self.assertEqual(attrs["http.url"], "http://127.0.0.1/?foo=bar")

def test_response_attributes(self):
otel_asgi.set_status_code(self.span, 404)
Expand Down

0 comments on commit 3849da1

Please sign in to comment.