Skip to content
Snippets Groups Projects
Commit c6f46547 authored by Stein Magne Bjorklund's avatar Stein Magne Bjorklund :speech_balloon:
Browse files

Merge branch 'getBestContentType' into 'master'

Get best content type

See merge request !12
parents f45fe04d 9cb1729c
No related branches found
No related tags found
1 merge request!12Get best content type
...@@ -124,39 +124,50 @@ class Utils ...@@ -124,39 +124,50 @@ class Utils
return $obj; return $obj;
} }
public static function getExtension($accept_string) public static function getExtension($accept_string, array $configuration): string
{ {
global $conf; foreach ($configuration as $extension => $extensions) {
$extension = "html"; if (in_array($accept_string, $extensions, true)) {
foreach ($conf['http_accept'] as $ext => $accept_arr) { return $extension;
if (in_array($accept_string, $accept_arr)) {
$extension = $ext;
} }
} }
return $extension;
return 'html';
} }
public static function getBestContentType($accept_string) /**
* header from the current request, if there is one.
*
* @param $accept_string
* Contents of the Accept: header from the current request.
* @param array $configuration
* @return int|string
*/
public static function getBestContentType($accept_string, array $configuration)
{ {
global $conf;
$a = explode(",", $accept_string); $a = explode(",", $accept_string);
$b = array(); if ($a[0] === 'text/html' || $a[0] === 'text/css' || $a[0] === '*/*') {
return 'text/html';
}
$b = [];
foreach ($a as $v) { foreach ($a as $v) {
foreach ($conf['http_accept'] as $formatTypeArray) { foreach ($configuration as $formatTypeArray) {
if (strstr($v, ";")) { if (strpos($v, ";") !== false) {
$aux = explode(";", $v); $aux = explode(';', $v);
$aux[0] = trim($aux[0]); $aux[0] = trim($aux[0]);
if (in_array($aux[0], $formatTypeArray)) { if (in_array($aux[0], $formatTypeArray, true)) {
$b[$aux[0]] = floatval(trim(str_replace("q=", "", $aux[1]))); $b[$aux[0]] = (float)trim(str_replace("q=", "", $aux[1]));
} }
} else { } else {
$value = trim($v); $value = trim($v);
if (in_array($value, $formatTypeArray)) { if (in_array($value, $formatTypeArray, true)) {
$b[$value] = 1.0; $b[$value] = 1.0;
} }
} }
} }
} }
$a = $b; $a = $b;
arsort($a); arsort($a);
$ct = 'text/html'; $ct = 'text/html';
...@@ -164,9 +175,11 @@ class Utils ...@@ -164,9 +175,11 @@ class Utils
$ct = $k; $ct = $k;
break; break;
} }
if ($ct == null || $ct == "" || $ct == "*/*") {
$ct = 'text/html'; if ($ct === null || $ct === "" || $ct === "*/*") {
return 'text/html';
} }
return $ct; return $ct;
} }
...@@ -250,7 +263,7 @@ class Utils ...@@ -250,7 +263,7 @@ class Utils
global $conf; global $conf;
global $lodspk; global $lodspk;
$contentType = $lodspk['contentType']; $contentType = $lodspk['contentType'];
$extension = Utils::getExtension($contentType); $extension = Utils::getExtension($contentType, $conf['http_accept']);
header('Content-Type: ' . $contentType); header('Content-Type: ' . $contentType);
if (isset($lodspk['resultRdf']) && $lodspk['resultRdf'] == true) { if (isset($lodspk['resultRdf']) && $lodspk['resultRdf'] == true) {
......
...@@ -25,7 +25,7 @@ class ServiceModule extends abstractModule ...@@ -25,7 +25,7 @@ class ServiceModule extends abstractModule
} }
$extension = Utils::getExtension($acceptContentType); $extension = Utils::getExtension($acceptContentType, $conf['http_accept']);
$viewFile = null; $viewFile = null;
$tokens = $qArr; $tokens = $qArr;
$arguments = array(); $arguments = array();
...@@ -140,7 +140,7 @@ class ServiceModule extends abstractModule ...@@ -140,7 +140,7 @@ class ServiceModule extends abstractModule
$params = $this->getParams($localUri); $params = $this->getParams($localUri);
//$params[] = $context; //$params[] = $context;
//$acceptContentType = Utils::getBestContentType($_SERVER['HTTP_ACCEPT']); //$acceptContentType = Utils::getBestContentType($_SERVER['HTTP_ACCEPT']);
$extension = Utils::getExtension($acceptContentType); $extension = Utils::getExtension($acceptContentType, $conf['http_accept']);
$args = array(); $args = array();
list($modelFile, $viewFile) = $service; list($modelFile, $viewFile) = $service;
try { try {
......
...@@ -86,7 +86,7 @@ class sparqlFilterModule extends abstractModule ...@@ -86,7 +86,7 @@ class sparqlFilterModule extends abstractModule
$localUri = preg_replace("|^" . $conf['ns']['local'] . "|", $conf['basedir'], $res); $localUri = preg_replace("|^" . $conf['ns']['local'] . "|", $conf['basedir'], $res);
} }
$extension = Utils::getExtension($format); $extension = Utils::getExtension($format, $conf['http_accept']);
/*Redefine Content type based on the /*Redefine Content type based on the
* dcterms:format for this page * dcterms:format for this page
......
...@@ -64,7 +64,7 @@ class TypeModule extends abstractModule ...@@ -64,7 +64,7 @@ class TypeModule extends abstractModule
$localUri = preg_replace("|^" . $conf['ns']['local'] . "|", $conf['basedir'], $res); $localUri = preg_replace("|^" . $conf['ns']['local'] . "|", $conf['basedir'], $res);
} }
$extension = Utils::getExtension($format); $extension = Utils::getExtension($format, $conf['http_accept']);
/*Redefine Content type based on the /*Redefine Content type based on the
* dcterms:format for this page * dcterms:format for this page
......
...@@ -43,7 +43,7 @@ class UriModule extends abstractModule ...@@ -43,7 +43,7 @@ class UriModule extends abstractModule
return false; return false;
} }
} }
$extension = Utils::getExtension($pair[2]); $extension = Utils::getExtension($pair[2], $conf['http_accept']);
$curie = Utils::uri2curie($pair[0]); $curie = Utils::uri2curie($pair[0]);
list($modelFile, $viewFile) = $this->getModelandView($curie, $extension); list($modelFile, $viewFile) = $this->getModelandView($curie, $extension);
if ($modelFile == null) { if ($modelFile == null) {
...@@ -88,7 +88,7 @@ class UriModule extends abstractModule ...@@ -88,7 +88,7 @@ class UriModule extends abstractModule
$localUri = preg_replace("|^" . $conf['ns']['local'] . "|", $conf['basedir'], $res); $localUri = preg_replace("|^" . $conf['ns']['local'] . "|", $conf['basedir'], $res);
} }
$extension = Utils::getExtension($format); $extension = Utils::getExtension($format, $conf['http_accept']);
/*Redefine Content type based on the /*Redefine Content type based on the
* dcterms:format for this page * dcterms:format for this page
......
...@@ -50,8 +50,8 @@ $firstResults = array(); ...@@ -50,8 +50,8 @@ $firstResults = array();
$endpoints = array(); $endpoints = array();
$endpoints['local'] = new Endpoint($conf['endpoint']['local'], $conf['endpointParams']['config']); $endpoints['local'] = new Endpoint($conf['endpoint']['local'], $conf['endpointParams']['config']);
$acceptContentType = Utils::getBestContentType($_SERVER['HTTP_ACCEPT']); $acceptContentType = Utils::getBestContentType($_SERVER['HTTP_ACCEPT'], $conf['http_accept']);
$extension = Utils::getExtension($acceptContentType); $extension = Utils::getExtension($acceptContentType, $conf['http_accept']);
//Check content type is supported by LODSPeaKr //Check content type is supported by LODSPeaKr
......
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">classes</directory>
</include>
</coverage>
</phpunit>
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use uib\ub\loadspeakr\Utils;
final class UtilsTest extends TestCase
{
private array $http_accept;
public function setUp(): void
{
parent::setUp();
$this->http_accept = [
'html' => ['text/html'],
'rdf' => ['application/rdf+xml'],
'ttl' => [
'text/n3',
'application/x-turtle',
'application/turtle',
'text/turtle',
'application/rdf+turtle',
],
'json' => [
'application/json',
'application/x-javascript',
'text/javascript',
'text/x-javascript',
'text/x-json',
],
'nt' => ['text/plain'],
];
}
/**
* Test Loadspeakr content negotiation.
*
* @dataProvider requestProvider
* @covers \uib\ub\loadspeakr\Utils::getBestContentType
*/
public function testGetBestContentType($expected, $requestHeader): void
{
self::assertSame($expected, Utils::getBestContentType($requestHeader, $this->http_accept));
}
/**
* Test Loadspeakr extension negotiation.
*
* @dataProvider requestGetExtensionProvider
* @covers \uib\ub\loadspeakr\Utils::getExtension
*/
public function testGetExtension($expected, $mediaType): void
{
self::assertEquals($expected, Utils::getExtension($mediaType, $this->http_accept));
}
/**
* @see testGetBestContentType
*/
public function requestProvider(): array
{
return [
'null header' => [
'expected' => 'text/html',
'media-type' => 'text/html',
],
'Empty header' => [
'expected' => 'text/html',
'media-type' => 'text/html',
],
'Empty string' => [
'expected' => 'text/html',
'media-type' => 'text/html',
],
'Illegal string' => [
'expected' => 'text/html',
'media-type' => 'foo, bar',
],
'html request' => [
'expected' => 'text/html',
'media-type' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
],
'CSS request' => [
'expected' => 'text/html',
'media-type' => 'text/css,*/*;q=0.1',
],
'JSON request' => [
'expected' => 'application/json',
'media-type' => 'json/txt,application/json,*/*;q=0.1',
],
'Illegal JSON' => [
'expected' => 'text/html',
'media-type' => 'json/born, application/jsons, */* ;q=0.1',
],
];
}
/**
* @see testGetExtension
*/
public function requestGetExtensionProvider(): array
{
return [
'Text html' => [
'expected' => 'html',
'media-type' => 'text/html',
],
'Unknown media type' => [
'expected' => 'html',
'media-type' => 'foo/bar',
],
'Empty string' => [
'expected' => 'html',
'media-type' => '',
],
'Plain text' => [
'expected' => 'html',
'media-type' => 'text/plain',
],
'CSS request' => [
'expected' => 'json',
'media-type' => 'application/json',
],
'JSON request' => [
'expected' => 'json',
'media-type' => 'application/json',
],
'Illegal JSON' => [
'expected' => 'json',
'media-type' => 'application/jsons',
],
'Image jpeg' => [
'expected' => 'json',
'media-type' => 'image/jpeg',
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment