diff --git a/lib/Graph.php b/lib/Graph.php index 9970f6f7..a173bf17 100644 --- a/lib/Graph.php +++ b/lib/Graph.php @@ -323,11 +323,19 @@ public function load($uri = null, $format = null) } } } else { + $acceptHeader = Format::formatAcceptHeader([ + 'application/ld+json' => 1.0, + 'application/rdf+xml' => 0.9, + 'text/turtle' => 0.8, + 'application/n-quads' => 0.7, + 'application/n-triples' => 0.7, + ]); + // Send a list of all the formats we can parse if ($client instanceof Client) { - $client->setHeaders('Accept', Format::getHttpAcceptHeader()); + $client->setHeaders('Accept', $acceptHeader); } else { - $client->setHeaders(['Accept' => Format::getHttpAcceptHeader()]); + $client->setHeaders(['Accept' => $acceptHeader]); } } diff --git a/lib/Sparql/Client.php b/lib/Sparql/Client.php index 5afe621a..6ccc7ff0 100644 --- a/lib/Sparql/Client.php +++ b/lib/Sparql/Client.php @@ -338,6 +338,13 @@ protected function executeQuery($processed_query, $type) 'application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8, ]; + $sparql_graph_types = [ + 'application/ld+json' => 1.0, + 'application/rdf+xml' => 0.9, + 'text/turtle' => 0.8, + 'application/n-quads' => 0.7, + 'application/n-triples' => 0.7, + ]; if ('update' == $type) { // accept anything, as "response body of a […] update request is implementation defined" @@ -368,7 +375,7 @@ protected function executeQuery($processed_query, $type) $accept = Format::formatAcceptHeader($sparql_results_types); } elseif ('CONSTRUCT' === $query_verb || 'DESCRIBE' === $query_verb) { // only "graph" - $accept = Format::getHttpAcceptHeader(); + $accept = Format::formatAcceptHeader($sparql_graph_types); } else { // both $accept = Format::getHttpAcceptHeader($sparql_results_types); diff --git a/tests/EasyRdf/GraphTest.php b/tests/EasyRdf/GraphTest.php index 8a8dbc35..319e73b6 100644 --- a/tests/EasyRdf/GraphTest.php +++ b/tests/EasyRdf/GraphTest.php @@ -42,6 +42,7 @@ use EasyRdf\Format; use EasyRdf\Graph; use EasyRdf\Http; +use EasyRdf\Http\Client; use EasyRdf\Literal; use EasyRdf\ParsedUri; use EasyRdf\RdfNamespace; @@ -273,7 +274,7 @@ public function testLoadGraphUri() public function testLoadWithContentType() { $checkRequest = function ($client) { - $this->assertStringContainsString(',application/json,', $client->getHeader('Accept')); + $this->assertStringContainsString('application/ld+json,', $client->getHeader('Accept')); return true; }; @@ -392,6 +393,20 @@ public function testLoadRedirect() ); } + /** + * Set up a Graph instance using a real Client to load a CONSTRUCT result. + * + * @see https://github.com/sweetrdf/easyrdf/pull/48 + */ + public function testIssue47GraphLoadRdfFile(): void + { + Http::setDefaultHttpClient(new Client()); + $graph = new Graph(); + $url = 'https://query.wikidata.org/sparql?query=construct+%7B+%3Fs+%3Fq+%3Fr+%7D+where+%7B+%3Fs+%3Fp+%3Fo+.+%3Fo+%3Fq+%3Fr+%7D+limit+1'; + $tripleCount = $graph->load($url); + $this->assertTrue(0 < $tripleCount); + } + public function testNewAndLoad() { $this->client->addMockOnce('GET', 'http://www.example.com/', readFixture('foaf.json')); diff --git a/tests/EasyRdf/Sparql/ClientTest.php b/tests/EasyRdf/Sparql/ClientTest.php index 006e4ff9..51149a91 100644 --- a/tests/EasyRdf/Sparql/ClientTest.php +++ b/tests/EasyRdf/Sparql/ClientTest.php @@ -606,4 +606,29 @@ private static function parseAcceptHeader($accept_str) return $types; } + + /** + * @see https://github.com/sweetrdf/easyrdf/pull/48 + */ + public function testIssue47CorrectHeaderForConstructQueries(): void + { + Http::setDefaultHttpClient(new HttpClient()); + $query = 'construct { ?s ?q ?r } where { ?s ?p ?o . ?o ?q ?r } limit 1'; + + /* + * use DBpedia's SPARQL endpoint to check result + */ + $endpointUrl = 'https://dbpedia.org/sparql'; + $endPoint = new Client($endpointUrl); + $result = $endPoint->query($query); + $this->assertTrue($result instanceof Graph, '$result is an instance of '.get_class($result)); + + /* + * use Wikidata's SPARQL endpoint to check result + */ + $endpointUrl = 'https://query.wikidata.org/sparql'; + $endPoint = new Client($endpointUrl); + $result = $endPoint->query($query); + $this->assertTrue($result instanceof Graph, '$result is an instance of '.get_class($result)); + } } diff --git a/tests/ExampleTest/OpenGraphProtocolTest.php b/tests/ExampleTest/OpenGraphProtocolTest.php index 6ff4bd1f..f06c58de 100644 --- a/tests/ExampleTest/OpenGraphProtocolTest.php +++ b/tests/ExampleTest/OpenGraphProtocolTest.php @@ -43,7 +43,7 @@ public function testRottenTomatoes() { $output = executeExample('open_graph_protocol.php'); $this->assertStringContainsString('
assertStringContainsString('
Title:
Ocean\'s Eleven
', $output); + $this->assertStringContainsString('
Title:
Ocean\'s Eleven', $output); $this->assertStringContainsString('
Description:
', $output); } }