Skip to content

Commit 91e9842

Browse files
authored
fix: add charset encoding to SPARQLConnector.update() request. (#2112)
Add encoding "charset=UTF-8" to Content-Type header in `SPARQLConnector.update()` request. Fixes #2095
1 parent bcd05e9 commit 91e9842

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

rdflib/plugins/stores/sparqlconnector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def update(
167167

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

173173
args = dict(self.kwargs) # other QSAs

test/test_store/test_store_sparqlstore.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import re
23
import socket
34
from http.server import BaseHTTPRequestHandler, HTTPServer
@@ -457,23 +458,22 @@ def do_POST(self):
457458
print(body)
458459
```
459460
"""
460-
contenttype = self.headers.get("Content-Type")
461+
contenttype = [
462+
part.strip() for part in f"{self.headers.get('Content-Type')}".split(";")
463+
]
464+
logging.debug("contenttype = %s", contenttype)
461465
if self.path == "/query" or self.path == "/query?":
462-
if self.headers.get("Content-Type") == "application/sparql-query":
466+
if "application/sparql-query" in contenttype:
463467
pass
464-
elif (
465-
self.headers.get("Content-Type") == "application/x-www-form-urlencoded"
466-
):
468+
elif "application/x-www-form-urlencoded" in contenttype:
467469
pass
468470
else:
469471
self.send_response(406, "Not Acceptable")
470472
self.end_headers()
471473
elif self.path == "/update" or self.path == "/update?":
472-
if self.headers.get("Content-Type") == "application/sparql-update":
474+
if "application/sparql-update" in contenttype:
473475
pass
474-
elif (
475-
self.headers.get("Content-Type") == "application/x-www-form-urlencoded"
476-
):
476+
elif "application/x-www-form-urlencoded" in contenttype:
477477
pass
478478
else:
479479
self.send_response(406, "Not Acceptable")

test/test_store/test_store_sparqlupdatestore_mock.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,26 @@ def test_graph_update(self):
5858
req = self.httpmock.requests[MethodName.POST].pop(0)
5959
assert req.parsed_path.path == self.update_path
6060
assert "application/sparql-update" in req.headers.get("content-type")
61+
62+
def test_update_encoding(self):
63+
graph = ConjunctiveGraph("SPARQLUpdateStore")
64+
graph.open((self.query_endpoint, self.update_endpoint))
65+
update_statement = f"INSERT DATA {{ {EG['subj']} {EG['pred']} {EG['obj']}. }}"
66+
67+
self.httpmock.responses[MethodName.POST].append(
68+
MockHTTPResponse(
69+
200,
70+
"OK",
71+
b"Update succeeded",
72+
{"Content-Type": ["text/plain; charset=UTF-8"]},
73+
)
74+
)
75+
76+
# This test assumes that updates are performed using POST
77+
# at the moment this is the only supported way for SPARQLUpdateStore
78+
# to do updates.
79+
graph.update(update_statement)
80+
assert self.httpmock.call_count == 1
81+
req = self.httpmock.requests[MethodName.POST].pop(0)
82+
assert req.parsed_path.path == self.update_path
83+
assert "charset=UTF-8" in req.headers.get("content-type")

0 commit comments

Comments
 (0)