diff --git a/http-tests/document-hierarchy/POST-html-jsonld.sh b/http-tests/document-hierarchy/POST-html-jsonld.sh new file mode 100755 index 000000000..ab16fe546 --- /dev/null +++ b/http-tests/document-hierarchy/POST-html-jsonld.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -euo pipefail + +initialize_dataset "$END_USER_BASE_URL" "$TMP_END_USER_DATASET" "$END_USER_ENDPOINT_URL" +initialize_dataset "$ADMIN_BASE_URL" "$TMP_ADMIN_DATASET" "$ADMIN_ENDPOINT_URL" +purge_cache "$END_USER_VARNISH_SERVICE" +purge_cache "$ADMIN_VARNISH_SERVICE" +purge_cache "$FRONTEND_VARNISH_SERVICE" + +# add agent to the writers group + +add-agent-to-group.sh \ + -f "$OWNER_CERT_FILE" \ + -p "$OWNER_CERT_PWD" \ + --agent "$AGENT_URI" \ + "${ADMIN_BASE_URL}acl/groups/writers/" + +# POST an HTML document with an embedded JSON-LD +EOF +) \ +| grep -q "$STATUS_NO_CONTENT" + +# check that the triple from the embedded JSON-LD is queryable + +curl -k -f -s -G \ + -E "$AGENT_CERT_FILE":"$AGENT_CERT_PWD" \ + -H "Accept: application/n-triples" \ +"$END_USER_BASE_URL" \ +| tr -d '\n' \ +| grep '"named object HTML/JSON-LD POST"' > /dev/null diff --git a/http-tests/proxy/GET-proxied-html-jsonld.sh b/http-tests/proxy/GET-proxied-html-jsonld.sh new file mode 100755 index 000000000..28e4fd972 --- /dev/null +++ b/http-tests/proxy/GET-proxied-html-jsonld.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -euo pipefail + +initialize_dataset "$END_USER_BASE_URL" "$TMP_END_USER_DATASET" "$END_USER_ENDPOINT_URL" +initialize_dataset "$ADMIN_BASE_URL" "$TMP_ADMIN_DATASET" "$ADMIN_ENDPOINT_URL" +purge_cache "$END_USER_VARNISH_SERVICE" +purge_cache "$ADMIN_VARNISH_SERVICE" +purge_cache "$FRONTEND_VARNISH_SERVICE" + +# add agent to the readers group to be able to read documents + +add-agent-to-group.sh \ + -f "$OWNER_CERT_FILE" \ + -p "$OWNER_CERT_PWD" \ + --agent "$AGENT_URI" \ + "${ADMIN_BASE_URL}acl/groups/readers/" + +# Regression: when an upstream proxied URI returns text/html (e.g. a schema.org term page) +# but embeds JSON-LD via + + """.formatted(EX); + + Model model = parse(html); + + assertTrue(model.contains( + ResourceFactory.createResource(EX + "alice"), + ResourceFactory.createProperty(EX, "name"), + "Alice")); + assertTrue(model.contains( + ResourceFactory.createResource(EX + "alice"), + org.apache.jena.vocabulary.RDF.type, + ResourceFactory.createResource(EX + "Person"))); + } + + @Test + public void testMultipleScriptsAreMerged() + { + String html = """ + + + + + """.formatted(EX, EX); + + Model model = parse(html); + + assertTrue(model.contains( + ResourceFactory.createResource(EX + "alice"), + ResourceFactory.createProperty(EX, "name"), + "Alice")); + assertTrue(model.contains( + ResourceFactory.createResource(EX + "bob"), + ResourceFactory.createProperty(EX, "name"), + "Bob")); + } + + @Test + public void testMissingScriptThrows() + { + String html = "no jsonld

nothing

"; + + assertThrows(RiotParseException.class, () -> parse(html)); + } + + @Test + public void testOtherScriptTypesIgnored() + { + // a non-ld+json + + + """.formatted(EX); + + Model model = parse(html); + + assertTrue(model.contains( + ResourceFactory.createResource(EX + "alice"), + ResourceFactory.createProperty(EX, "name"), + "Alice")); + assertFalse(model.contains( + ResourceFactory.createResource(EX + "js"), + ResourceFactory.createProperty(EX, "name"), + "JS")); + } + + @Test + public void testSameOutputAsDirectJsonLdParse() + { + // the HTML reader must be a transparent wrapper around Jena's JSON-LD11 reader: + // wrapping the same payload in HTML must yield exactly the same model as parsing the payload directly + String jsonLd = """ + { + "@context": {"ex": "%s", "name": {"@id": "ex:name"}}, + "@id": "ex:alice", + "@type": "ex:Person", + "name": "Alice" + } + """.formatted(EX); + String html = ""; + + Model direct = ModelFactory.createDefaultModel(); + RDFParser.create(). + source(new ByteArrayInputStream(jsonLd.getBytes(StandardCharsets.UTF_8))). + lang(Lang.JSONLD11). + base(BASE_URI). + parse(StreamRDFLib.graph(direct.getGraph())); + + Model viaHtml = parse(html); + + assertEquals(direct.size(), viaHtml.size()); + assertTrue(direct.isIsomorphicWith(viaHtml)); + } + +}