Skip to content

Commit

Permalink
fix: trailing slash issue (sparckles#819)
Browse files Browse the repository at this point in the history
* fixes sparckles#818

* fix: trailing slash issue

* use `str#ends_with`

* something like this

* update

* fix: error[E0716]: temporary value dropped while borrowed

* use only one `#clone`, pass the function to the other

* change back to inline assignment itself

* test: add tests

* docs: add comment

* fix(ci): change string to char

* fix(ci): formatting -- run `cargo fmt`

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sanskar Jethi <[email protected]>
  • Loading branch information
3 people authored May 19, 2024
1 parent 00ca522 commit b6d3281
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ async def hello_world(request):
return "Hello, world!"


@app.get("/trailing")
def trailing_slash(request):
return "Trailing slash test successful!"


@app.get("/sync/str")
def sync_str_get():
return "sync str get"
Expand Down
10 changes: 10 additions & 0 deletions integration_tests/test_get_requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import requests
from requests import Response

from integration_tests.helpers.http_methods_helpers import get
Expand Down Expand Up @@ -51,3 +52,12 @@ def test_queries(function_type: str, session):

r = get(f"/{function_type}/queries")
assert r.json() == {}


@pytest.mark.benchmark
def test_trailing_slash(session):
r = requests.get("http://localhost:8080/trailing") # `integration_tests#get` strips the trailing slash, tests always pass!`
assert r.text == "Trailing slash test successful!"

r = requests.get("http://localhost:8080/trailing/")
assert r.text == "Trailing slash test successful!"
18 changes: 18 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ impl Server {
route: &str,
function: FunctionInfo,
is_const: bool,
) {
let second_route: String = if route.ends_with('/') {
route[0..route.len() - 1].to_string()
} else {
format!("{}/", route)
};

self._add_route(py, route_type, route, function.clone(), is_const);
self._add_route(py, route_type, &second_route, function, is_const);
}

fn _add_route(
&self,
py: Python,
route_type: &HttpMethod,
route: &str,
function: FunctionInfo,
is_const: bool,
) {
debug!("Route added for {:?} {} ", route_type, route);
let asyncio = py.import("asyncio").unwrap();
Expand Down

0 comments on commit b6d3281

Please sign in to comment.