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

add charset encoding to SPARQLConnector.update() request. #2112

Merged
merged 3 commits into from
Sep 15, 2022
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
2 changes: 1 addition & 1 deletion rdflib/plugins/stores/sparqlconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def update(

headers = {
"Accept": _response_mime_types[self.returnFormat],
"Content-Type": "application/sparql-update",
"Content-Type": "application/sparql-update; charset=UTF-8",
}

args = dict(self.kwargs) # other QSAs
Expand Down
18 changes: 9 additions & 9 deletions test/test_store/test_store_sparqlstore.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
Expand Down Expand Up @@ -457,23 +458,22 @@ def do_POST(self):
print(body)
```
"""
contenttype = self.headers.get("Content-Type")
contenttype = [
part.strip() for part in f"{self.headers.get('Content-Type')}".split(";")
]
logging.debug("contenttype = %s", contenttype)
if self.path == "/query" or self.path == "/query?":
if self.headers.get("Content-Type") == "application/sparql-query":
if "application/sparql-query" in contenttype:
pass
elif (
self.headers.get("Content-Type") == "application/x-www-form-urlencoded"
):
elif "application/x-www-form-urlencoded" in contenttype:
pass
else:
self.send_response(406, "Not Acceptable")
self.end_headers()
elif self.path == "/update" or self.path == "/update?":
if self.headers.get("Content-Type") == "application/sparql-update":
if "application/sparql-update" in contenttype:
pass
elif (
self.headers.get("Content-Type") == "application/x-www-form-urlencoded"
):
elif "application/x-www-form-urlencoded" in contenttype:
pass
else:
self.send_response(406, "Not Acceptable")
Expand Down
23 changes: 23 additions & 0 deletions test/test_store/test_store_sparqlupdatestore_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,26 @@ def test_graph_update(self):
req = self.httpmock.requests[MethodName.POST].pop(0)
assert req.parsed_path.path == self.update_path
assert "application/sparql-update" in req.headers.get("content-type")

def test_update_encoding(self):
graph = ConjunctiveGraph("SPARQLUpdateStore")
graph.open((self.query_endpoint, self.update_endpoint))
update_statement = f"INSERT DATA {{ {EG['subj']} {EG['pred']} {EG['obj']}. }}"

self.httpmock.responses[MethodName.POST].append(
MockHTTPResponse(
200,
"OK",
b"Update succeeded",
{"Content-Type": ["text/plain; charset=UTF-8"]},
)
)

# This test assumes that updates are performed using POST
# at the moment this is the only supported way for SPARQLUpdateStore
# to do updates.
graph.update(update_statement)
assert self.httpmock.call_count == 1
req = self.httpmock.requests[MethodName.POST].pop(0)
assert req.parsed_path.path == self.update_path
assert "charset=UTF-8" in req.headers.get("content-type")