Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set correct Accept header for CONSTRUCT queries #48

Merged
merged 12 commits into from
May 27, 2024
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()
);
}

/**
* Setup a Graph instance using a real Client to load a CONSTRUCT result.
k00ni marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 and check result
k00ni marked this conversation as resolved.
Show resolved Hide resolved
*/
$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 DBpedia's SPARQL endpoint and check result
*/
$endpointUrl = 'https://query.wikidata.org/sparql';
k00ni marked this conversation as resolved.
Show resolved Hide resolved
$endPoint = new Client($endpointUrl);
$result = $endPoint->query($query);
$this->assertTrue($result instanceof Graph, '$result is an instance of '.get_class($result));
}
}
2 changes: 1 addition & 1 deletion tests/ExampleTest/OpenGraphProtocolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testRottenTomatoes()
{
$output = executeExample('open_graph_protocol.php');
$this->assertStringContainsString('<dd><a href="https://www.rottentomatoes.com/m/oceans_eleven"', $output);
$this->assertStringContainsString('<dt>Title:</dt> <dd>Ocean\'s Eleven</dd>', $output);
$this->assertStringContainsString('<dt>Title:</dt> <dd>Ocean\'s Eleven', $output);
$this->assertStringContainsString('<dt>Description:</dt>', $output);
}
}