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 missing query string params in sparqlconnector when using POST method #2180

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion rdflib/plugins/stores/sparqlconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def query(
)
elif self.method == "POST":
args["headers"].update({"Content-Type": "application/sparql-query"})
qsa = "?" + urlencode(params)
args["params"].update(params)
qsa = "?" + urlencode(args["params"])
try:
res = urlopen(
Request(
Expand Down
76 changes: 76 additions & 0 deletions test/test_store/test_store_sparqlstore_sparqlconnector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import annotations

import json
import logging
from test.utils.httpservermock import (
MethodName,
MockHTTPResponse,
ServedBaseHTTPServerMock,
)
from typing import Optional

import pytest

from rdflib.graph import Graph
from rdflib.plugins.stores.sparqlstore import SPARQLStore


@pytest.mark.parametrize(
["graph_identifier"],
[
(None,),
("http://example.com",),
],
)
def test_query_url_construct_format(
function_httpmock: ServedBaseHTTPServerMock, graph_identifier: Optional[str]
) -> None:
"""
This tests that query string params (foo & bar) are appended to the endpoint
"""
endpoint = f"{function_httpmock.url}/query"

store: SPARQLStore = SPARQLStore(
sparql11=True,
returnFormat="json",
method="POST",
params={"foo": "1", "bar": "2"},
)
graph: Graph = Graph(store=store, identifier=graph_identifier)
graph.open(endpoint, create=True)

query = """
SELECT {
?s ?p ?o
}
WHERE {
?s ?p ?o
}
"""

function_httpmock.responses[MethodName.POST].append(
MockHTTPResponse(
200,
"OK",
json.dumps({"head": {"vars": []}, "results": {"bindings": []}}).encode(
encoding="utf-8"
),
{"Content-Type": ["application/sparql-results+json"]},
)
)

result = graph.query(query)

request = function_httpmock.requests[MethodName.POST].pop()
logging.debug("request = %s", request)
logging.debug("request.query_string = %s", request.path_query)
assert "foo" in request.path_query
assert request.path_query["foo"] == ["1"]
assert "bar" in request.path_query
assert request.path_query["bar"] == ["2"]
if graph_identifier is None:
assert "default-graph-uri" not in request.path_query
else:
assert "default-graph-uri" in request.path_query
assert request.path_query["default-graph-uri"] == [graph_identifier]
assert result.type == "SELECT"