/** * @link https://github.com/ptlis/conneg/issues/2 GitHub Issue #2. */ public function testOne() { $httpField = 'text/rdf+n3; q=0.8, application/rdf+json; q=0.8, text/turtle; q=1.0, text/n3; q=0.8, application/ld+json; q=0.5, application/rdf+xml; q=0.8'; $serverPrefs = ''; $expectList = array(new MatchedPreference(Preference::MIME, new Preference('text/turtle', 1, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT)), new MatchedPreference(Preference::MIME, new Preference('application/rdf+json', 0.8, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT)), new MatchedPreference(Preference::MIME, new Preference('application/rdf+xml', 0.8, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT)), new MatchedPreference(Preference::MIME, new Preference('text/n3', 0.8, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT)), new MatchedPreference(Preference::MIME, new Preference('text/rdf+n3', 0.8, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT)), new MatchedPreference(Preference::MIME, new Preference('application/ld+json', 0.5, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT))); $negotiate = new Negotiation(); $resultList = $negotiate->mimeAll($httpField, $serverPrefs); $this->assertEquals($expectList, $resultList); }
/** * @link https://github.com/ptlis/conneg/issues/3 GitHub Issue #3. */ public function testThree() { $httpField = 'application/rdf+xml;q=0.5,text/html;q=.5'; $serverPrefs = ''; $expectList = array(new MatchedPreference(Preference::MIME, new Preference('application/rdf+xml', 0.5, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT)), new MatchedPreference(Preference::MIME, new Preference('text/html', 0.5, Preference::COMPLETE), new Preference('', 0, Preference::ABSENT))); $negotiate = new Negotiation(); $resultList = $negotiate->mimeAll($httpField, $serverPrefs); $this->assertEquals($expectList, $resultList); }
/** * Middleware callback used to check MIME types are OK. * It is not intended that you call this function yourself. * @throws \InvalidArgumentException Exception thrown if callback invoked incorrectly. */ public function callbackCheckFormats() { // get the acceptable MIME type from route info $args = func_get_args(); if (count($args) === 0 || !$args[0] instanceof \Slim\Route) { throw new \InvalidArgumentException('This method should not be invoked outside of the Slim Framework'); } $route = $args[0]; $id = $this->app->request()->getMethod() . $route->getPattern(); $acceptableMime = $this->routeInfo[$id]['mimeTypes']; // perform MIME-type matching on the requested and available formats // note that if there are no q-values, the *LAST* type "wins" $conneg = new \ptlis\ConNeg\Negotiation(); $this->mimeBest = $conneg->mimeBest($_SERVER['HTTP_ACCEPT'], $acceptableMime); if (!$this->mimeBest) { // TODO: need to verify that this is the expected return if there are no matches $this->outputError(406, 'Not Acceptable', '</p><p>This service cannot return information in the format(s) you requested.', 'Sorry, we cannot serve you data in your requested format'); } // store alternative MIME types foreach ($conneg->mimeAll('*/*', $acceptableMime) as $type) { $type = $type->getClientPreference(); // TODO: this stores objects of type ptlis\ConNeg\Preference\Preference // but we want to have a list of content types if ($type != $this->mimeBest) { $this->mimeAlternatives[] = $type; } } // return best match return $this->mimeBest; }
/** * @dataProvider charsetProvider * * @param string $clientField * @param string $serverField * @param string $best * @param MatchedPreferenceInterface[] $all */ public function testAll($clientField, $serverField, $best, array $all) { $negotiate = new Negotiation(); $collection = $negotiate->charsetAll($clientField, $serverField); $this->assertEquals($all, $collection); }