Skip to content

Commit

Permalink
fix: set correct Accept header for CONSTRUCT queries (#48)
Browse files Browse the repository at this point in the history
* fix: set correct Accept header for CONSTRUCT queries
Refs #47

* ClientTest.php: added test case to reproduce the error

* fixed failing test in OpenGraphProtocolTest.php

* add test with wikidata endpoint

* GraphTest.php: added test to show same faulty behavior in Graph::load method

* Graph.php: Graph::load uses correct Accept-headers from now on

* ClientTest.php: refined new test for better readability

* Update tests/EasyRdf/Sparql/ClientTest.php
Co-authored-by: Ted Thibodeau Jr <[email protected]>

* Update tests/EasyRdf/GraphTest.php
Co-authored-by: Ted Thibodeau Jr <[email protected]>

---------

Co-authored-by: Konrad Abicht <[email protected]>
Co-authored-by: Ted Thibodeau Jr <[email protected]>
  • Loading branch information
3 people authored May 27, 2024
1 parent 6756872 commit b5eeeb3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
9 changes: 8 additions & 1 deletion lib/Sparql/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 16 additions & 1 deletion tests/EasyRdf/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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'));
Expand Down
25 changes: 25 additions & 0 deletions tests/EasyRdf/Sparql/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit b5eeeb3

Please sign in to comment.