From 66495ee503088f6e880b5e34c561231b4350e652 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 6 Dec 2019 15:51:40 +0000 Subject: [PATCH] rest : rewrite to support the bestChoice of the user, and to be tested rest : add tests git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5803 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/restTest.php | 60 ++++++++++++++++++++++++++++++++++++++++++ rest.php | 65 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 Tests/restTest.php diff --git a/Tests/restTest.php b/Tests/restTest.php new file mode 100644 index 0000000..f2a0aa9 --- /dev/null +++ b/Tests/restTest.php @@ -0,0 +1,60 @@ + + */ + +/** Test the domframework REST part */ +class restTest extends PHPUnit_Framework_TestCase +{ + /** No param, JSON by default + */ + public function testChooseType1 () + { + $rest = new \rest (); + $res = $rest->chooseType (); + $this->assertSame ($res, "json"); + } + + /** If limited allowedTypes, return the first one as default + */ + public function testChooseType2 () + { + $rest = new \rest (); + $rest->allowedtypes = array ("xml", "csv"); + $res = $rest->chooseType (); + $this->assertSame ($res, "xml"); + } + + /** Choose by the user specification : exact match + */ + public function testChooseType3 () + { + $rest = new \rest (); + $_SERVER["HTTP_ACCEPT"] = "text/html,application/xml;q=0.9,*/*;q=0.8"; + $res = $rest->chooseType (); + $this->assertSame ($res, "xml"); + } + + /** Choose by the user specification : generic match + */ + public function testChooseType4 () + { + $rest = new \rest (); + $_SERVER["HTTP_ACCEPT"] = "text/html;q=0.9,*/*;q=0.8"; + $res = $rest->chooseType (); + $this->assertSame ($res, "json"); + } + + /** Choose by the user specification : generic match with limited allowed + * types + */ + public function testChooseType5 () + { + $rest = new \rest (); + $rest->allowedtypes = array ("xml", "csv"); + $_SERVER["HTTP_ACCEPT"] = "text/html;q=0.9,*/*;q=0.8"; + $res = $rest->chooseType (); + $this->assertSame ($res, "xml"); + } +} diff --git a/rest.php b/rest.php index cb172f3..70f0490 100644 --- a/rest.php +++ b/rest.php @@ -1,36 +1,69 @@ */ + * @package domframework + * @author Dominique Fournier + */ require_once ("domframework/http.php"); -/** Allow to manage the REST protocol by using the users output types */ + +/** Allow to manage the REST protocol by using the users output types + */ class rest { - /** Allowed types by default */ + /** Allowed types by default + */ public $allowedtypes = array ("json", "xml", "txt", "csv"); + /** Converted types + */ + private $convert = array ( + "application/json" => "json", + "application/xml" => "xml", + "text/plain" => "txt", + "text/csv" => "csv", + ); + + /** This method allow to choose the type of plugin to use on output + * Based on the HTTP_ACCEPT parameter provided by the user + * @return string like "json", "xml", "txt", "csv" + */ + public function chooseType () + // {{{ + { + $type = reset ($this->allowedtypes); + if (isset ($_SERVER["HTTP_ACCEPT"])) + { + // Must work with the complete definition to match, and by the name of + // the output plugin at the end + $convert = array_intersect ($this->convert, $this->allowedtypes); + $type = array_search (reset ($this->allowedtypes), $this->convert); + $http = new http; + $type = $http->bestChoice ($_SERVER["HTTP_ACCEPT"], array_keys ($convert), + $type); + $type = $this->convert[$type]; + } + return $type; + } + // }}} + /** Display the message (which can be a string, an array, an integer...) - into the type asked by the client if it is allowed. - By default, the JSON type is used - @param string $message Message to be displayed by the output type - @param integer $code HTTP code to use */ - function display ($message, $code=200) + * into the type asked by the client if it is allowed. + * By default, the JSON type is used + * @param string $message Message to be displayed by the output type + * @param integer|null $code HTTP code to use + */ + function display ($message, $code = 200) + // {{{ { $http = new http; $text = $http->codetext ($code); header ($_SERVER["SERVER_PROTOCOL"]." $code $text"); - $type = reset ($this->allowedtypes); - if (isset ($_SERVER["HTTP_ACCEPT"])) - { - $type = $http->bestChoice ($_SERVER["HTTP_ACCEPT"], $this->allowedtypes, - $type); - } - + $type = $this->chooseType (); require_once ("domframework/output$type.php"); $constr = __NAMESPACE__."\\output$type"; $method = "out"; $obj = new $constr (); $obj->$method ($message); } + // }}} }