Skip to content

Commit b6d3281

Browse files
VishnuSanalpre-commit-ci[bot]sansyrox
authored
fix: trailing slash issue (sparckles#819)
* 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 <29942790+sansyrox@users.noreply.github.com>
1 parent 00ca522 commit b6d3281

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

integration_tests/base_routes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ async def hello_world(request):
219219
return "Hello, world!"
220220

221221

222+
@app.get("/trailing")
223+
def trailing_slash(request):
224+
return "Trailing slash test successful!"
225+
226+
222227
@app.get("/sync/str")
223228
def sync_str_get():
224229
return "sync str get"

integration_tests/test_get_requests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import requests
23
from requests import Response
34

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

5253
r = get(f"/{function_type}/queries")
5354
assert r.json() == {}
55+
56+
57+
@pytest.mark.benchmark
58+
def test_trailing_slash(session):
59+
r = requests.get("http://localhost:8080/trailing") # `integration_tests#get` strips the trailing slash, tests always pass!`
60+
assert r.text == "Trailing slash test successful!"
61+
62+
r = requests.get("http://localhost:8080/trailing/")
63+
assert r.text == "Trailing slash test successful!"

src/server.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,24 @@ impl Server {
279279
route: &str,
280280
function: FunctionInfo,
281281
is_const: bool,
282+
) {
283+
let second_route: String = if route.ends_with('/') {
284+
route[0..route.len() - 1].to_string()
285+
} else {
286+
format!("{}/", route)
287+
};
288+
289+
self._add_route(py, route_type, route, function.clone(), is_const);
290+
self._add_route(py, route_type, &second_route, function, is_const);
291+
}
292+
293+
fn _add_route(
294+
&self,
295+
py: Python,
296+
route_type: &HttpMethod,
297+
route: &str,
298+
function: FunctionInfo,
299+
is_const: bool,
282300
) {
283301
debug!("Route added for {:?} {} ", route_type, route);
284302
let asyncio = py.import("asyncio").unwrap();

0 commit comments

Comments
 (0)