Skip to content

Commit

Permalink
fix: add charset encoding to SPARQLConnector.update() request. (#2112)
Browse files Browse the repository at this point in the history
Add encoding "charset=UTF-8" to Content-Type header in `SPARQLConnector.update()` request.

Fixes #2095
  • Loading branch information
robcast committed Sep 15, 2022
1 parent bcd05e9 commit 91e9842
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
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")

0 comments on commit 91e9842

Please sign in to comment.