Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Remove trailing slash from forwarded requests #159

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions app/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
from . import utility as util


def build_node_request_urls(node_urls: list, path: str) -> list:
"""
Return a list of URLs for the current request for the specified set of Neurobagel nodes.
"""
node_request_urls = []
for node_url in node_urls:
node_request_urls.append(node_url + path)
return node_request_urls


def build_combined_response(
total_nodes: int, cross_node_results: list | dict, node_errors: list
) -> dict:
Expand Down Expand Up @@ -116,8 +126,8 @@ async def get(
params["pipeline_version"] = pipeline_version

tasks = [
util.send_get_request(node_url + "query", params, token)
for node_url in node_urls
util.send_get_request(node_request_url, params, token)
for node_request_url in build_node_request_urls(node_urls, "query")
]
responses = await asyncio.gather(*tasks, return_exceptions=True)

Expand Down Expand Up @@ -164,10 +174,10 @@ async def get_instances(attribute_base_path: str):
attribute_uri = util.RESOURCE_URI_MAP[attribute_base_path]

tasks = [
util.send_get_request(
url=node_url + attribute_base_path + "/",
util.send_get_request(url=node_request_url)
for node_request_url in build_node_request_urls(
util.FEDERATION_NODES, attribute_base_path
)
for node_url in util.FEDERATION_NODES
]
responses = await asyncio.gather(*tasks, return_exceptions=True)

Expand Down Expand Up @@ -216,8 +226,10 @@ async def get_pipeline_versions(pipeline_term: str):
all_pipe_versions = []

tasks = [
util.send_get_request(f"{node_url}pipelines/{pipeline_term}/versions")
for node_url in util.FEDERATION_NODES
util.send_get_request(node_request_url)
for node_request_url in build_node_request_urls(
util.FEDERATION_NODES, f"pipelines/{pipeline_term}/versions"
)
]
responses = await asyncio.gather(*tasks, return_exceptions=True)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_attribute_factory_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import httpx
import pytest
from fastapi import status

from app.api import crud


def test_get_instances_with_duplicate_terms_handled(
test_app, monkeypatch, set_valid_test_federation_nodes
Expand Down Expand Up @@ -138,3 +141,24 @@ def test_fully_failed_get_instances_handled_gracefully(
assert response["nodes_response_status"] == "fail"
assert len(response["errors"]) == 2
assert response["responses"] == {"nb:Assessment": []}


@pytest.mark.parametrize(
"path",
[
"assessments",
"diagnoses",
"pipelines",
"query",
],
)
def test_node_request_urls_do_not_have_trailing_slash(path):
"""
Ensure that URLs used to forward requests to node APIs do not include a trailing slash.
"""
assert crud.build_node_request_urls(
["https://node1.institute.org/", "https://node2.institute.org/"], path
) == [
f"https://node1.institute.org/{path}",
f"https://node2.institute.org/{path}",
]
Loading