Skip to content

Commit

Permalink
Pass client query string through to upstream unedited
Browse files Browse the repository at this point in the history
Refs #3
  • Loading branch information
akx committed May 28, 2021
1 parent 28c3796 commit ef4048d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 14 additions & 2 deletions asgiproxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ def get_upstream_url(self, *, scope: Scope) -> str:
"""
raise NotImplementedError("...")

def get_upstream_url_with_query(self, *, scope: Scope) -> str:
"""
Get the upstream URL for a client request, including any query parameters to include.
"""
# The default implementation simply appends the original URL's query string to the
# upstream URL generated by `get_upstream_url`.
url = self.get_upstream_url(scope=scope)
query_string = scope.get("query_string")
if query_string:
sep = "&" if "?" in url else "?"
url += "{}{}".format(sep, query_string.decode("utf-8"))
return url

def process_client_headers(self, *, scope: Scope, headers: Headers) -> Headerlike:
"""
Process client HTTP headers before they're passed upstream.
Expand All @@ -39,8 +52,7 @@ def get_upstream_http_options(
"""
return dict(
method=client_request.method,
url=self.get_upstream_url(scope=scope),
params=client_request.query_params.multi_items(),
url=self.get_upstream_url_with_query(scope=scope),
data=data,
headers=self.process_client_headers(
scope=scope,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_asgiproxy.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import pytest
from asgiref.testing import ApplicationCommunicator
from starlette.requests import Request

from asgiproxy.context import ProxyContext
from asgiproxy.proxies.http import proxy_http
from tests.configs import ExampleComProxyConfig
from tests.utils import http_response_from_asgi_messages, make_http_scope


def test_query_string_passthrough():
proxy_config = ExampleComProxyConfig()
scope = make_http_scope(
full_url="http://127.0.0.1/pathlet/?encode&flep&murp",
headers={
"Accept": "text/html",
"User-Agent": "Foo",
},
)
client_request = Request(scope)
opts = proxy_config.get_upstream_http_options(
scope=scope, client_request=client_request, data=None
)
assert opts["url"] == "http://example.com/pathlet/?encode&flep&murp"


@pytest.mark.asyncio
async def test_asgiproxy():
scope = make_http_scope(
Expand Down

0 comments on commit ef4048d

Please sign in to comment.