1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
from rdflib import Graph, Namespace
from http.server import BaseHTTPRequestHandler
from urllib.error import HTTPError
from .testutils import SimpleHTTPMock, MockHTTPResponse, ctx_http_server, GraphHelper
import unittest
"""
Test that correct content negotiation headers are passed
by graph.parse
"""
xmltestdoc = """<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns="http://example.org/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
<rdf:Description rdf:about="http://example.org/a">
<b rdf:resource="http://example.org/c"/>
</rdf:Description>
</rdf:RDF>
"""
n3testdoc = """@prefix : <http://example.org/> .
:a :b :c .
"""
nttestdoc = "<http://example.org/a> <http://example.org/b> <http://example.org/c> .\n"
class ContentNegotiationHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200, "OK")
# fun fun fun parsing accept header.
acs = self.headers["Accept"].split(",")
acq = [x.split(";") for x in acs if ";" in x]
acn = [(x, "q=1") for x in acs if ";" not in x]
acs = [(x[0], float(x[1].strip()[2:])) for x in acq + acn]
ac = sorted(acs, key=lambda x: x[1])
ct = ac[-1]
if "application/rdf+xml" in ct:
rct = "application/rdf+xml"
content = xmltestdoc
elif "text/n3" in ct:
rct = "text/n3"
content = n3testdoc
elif "text/plain" in ct:
rct = "text/plain"
content = nttestdoc
self.send_header("Content-type", rct)
self.end_headers()
self.wfile.write(content.encode("utf-8"))
def log_message(self, *args):
pass
class TestGraphHTTP(unittest.TestCase):
def test_content_negotiation(self) -> None:
EG = Namespace("http://example.org/")
expected = Graph()
expected.add((EG["a"], EG["b"], EG["c"]))
expected_triples = GraphHelper.triple_set(expected)
with ctx_http_server(ContentNegotiationHandler) as server:
(host, port) = server.server_address
url = f"http://{host}:{port}/foo"
for format in ("xml", "n3", "nt"):
graph = Graph()
graph.parse(url, format=format)
self.assertEqual(expected_triples, GraphHelper.triple_set(graph))
def test_source(self) -> None:
EG = Namespace("http://example.org/")
expected = Graph()
expected.add((EG["a"], EG["b"], EG["c"]))
expected_triples = GraphHelper.triple_set(expected)
httpmock = SimpleHTTPMock()
with ctx_http_server(httpmock.Handler) as server:
(host, port) = server.server_address
url = f"http://{host}:{port}/"
httpmock.do_get_responses.append(
MockHTTPResponse(
200,
"OK",
f"<{EG['a']}> <{EG['b']}> <{EG['c']}>.".encode(),
{"Content-Type": ["text/turtle"]},
)
)
graph = Graph()
graph.parse(source=url)
self.assertEqual(expected_triples, GraphHelper.triple_set(graph))
def test_3xx(self) -> None:
EG = Namespace("http://example.com/")
expected = Graph()
expected.add((EG["a"], EG["b"], EG["c"]))
expected_triples = GraphHelper.triple_set(expected)
httpmock = SimpleHTTPMock()
with ctx_http_server(httpmock.Handler) as server:
(host, port) = server.server_address
url = f"http://{host}:{port}/"
for idx in range(3):
httpmock.do_get_responses.append(
MockHTTPResponse(
302, "FOUND", "".encode(), {"Location": [f"{url}loc/302/{idx}"]}
)
)
for idx in range(3):
httpmock.do_get_responses.append(
MockHTTPResponse(
303,
"See Other",
"".encode(),
{"Location": [f"{url}loc/303/{idx}"]},
)
)
for idx in range(3):
httpmock.do_get_responses.append(
MockHTTPResponse(
308,
"Permanent Redirect",
"".encode(),
{"Location": [f"{url}loc/308/{idx}"]},
)
)
httpmock.do_get_responses.append(
MockHTTPResponse(
200,
"OK",
f"<{EG['a']}> <{EG['b']}> <{EG['c']}>.".encode(),
{"Content-Type": ["text/turtle"]},
)
)
graph = Graph()
graph.parse(location=url, format="turtle")
self.assertEqual(expected_triples, GraphHelper.triple_set(graph))
httpmock.do_get_mock.assert_called()
assert len(httpmock.do_get_requests) == 10
for request in httpmock.do_get_requests:
self.assertRegex(request.headers.get("Accept"), "text/turtle")
request_paths = [request.path for request in httpmock.do_get_requests]
self.assertEqual(
request_paths,
[
"/",
"/loc/302/0",
"/loc/302/1",
"/loc/302/2",
"/loc/303/0",
"/loc/303/1",
"/loc/303/2",
"/loc/308/0",
"/loc/308/1",
"/loc/308/2",
],
)
def test_5xx(self):
httpmock = SimpleHTTPMock()
with ctx_http_server(httpmock.Handler) as server:
(host, port) = server.server_address
url = f"http://{host}:{port}/"
response = MockHTTPResponse(500, "Internal Server Error", "".encode(), {})
httpmock.do_get_responses.append(response)
graph = Graph()
with self.assertRaises(HTTPError) as raised:
graph.parse(location=url, format="turtle")
self.assertEqual(raised.exception.code, 500)
if __name__ == "__main__":
unittest.main()
|