Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c-w committed Oct 2, 2024
1 parent 0db086d commit fc0db6d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/servestatic/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async def __call__(self, scope, receive, send):
wsgi_headers = {
"HTTP_" + key.decode().upper().replace("-", "_"): value.decode() for key, value in scope["headers"]
}
wsgi_headers["QUERY_STRING"] = scope["query_string"].decode()

# Get the ServeStatic file response
response = await self.static_file.aget_response(scope["method"], wsgi_headers)
Expand Down
2 changes: 1 addition & 1 deletion src/servestatic/responders.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def get_response(self, method, request_headers):
return self.response

async def aget_response(self, method, request_headers):
return self.response
return self.get_response(method, request_headers)


class NotARegularFileError(Exception):
Expand Down
16 changes: 15 additions & 1 deletion tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
def test_files():
return Files(
js=str(Path("static") / "app.js"),
index=str(Path("static") / "with-index" / "index.html"),
)


Expand All @@ -34,7 +35,12 @@ async def asgi_app(scope, receive, send):
})
await send({"type": "http.response.body", "body": b"Not Found"})

return ServeStaticASGI(asgi_app, root=test_files.directory, autorefresh=request.param)
return ServeStaticASGI(
asgi_app,
root=test_files.directory,
autorefresh=request.param,
index_file=True,
)


def test_get_js_static_file(application, test_files):
Expand All @@ -47,6 +53,14 @@ def test_get_js_static_file(application, test_files):
assert send.headers[b"content-length"] == str(len(test_files.js_content)).encode()


def test_redirect_preserves_query_string(application, test_files):
scope = AsgiScopeEmulator({"path": "/static/with-index", "query_string": b"v=1"})
receive = AsgiReceiveEmulator()
send = AsgiSendEmulator()
asyncio.run(application(scope, receive, send))
assert send.headers[b"location"] == b"with-index/?v=1"


def test_user_app(application):
scope = AsgiScopeEmulator({"path": "/"})
receive = AsgiReceiveEmulator()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_servestatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ def test_index_file_path_redirected(server, files):
assert location == directory_url


def test_index_file_path_redirected_with_query_string(server, files):
directory_url = files.index_url.rpartition("/")[0] + "/"
query_string = "v=1"
response = server.get(f"{files.index_url}?{query_string}", allow_redirects=False)
location = urljoin(files.index_url, response.headers["Location"])
assert response.status_code == 302
assert location == f"{directory_url}?{query_string}"


def test_directory_path_without_trailing_slash_redirected(server, files):
directory_url = files.index_url.rpartition("/")[0] + "/"
no_slash_url = directory_url.rstrip("/")
Expand Down

0 comments on commit fc0db6d

Please sign in to comment.