/** * @dataProvider provideParseWeights */ public function testParseWeights($header, $expected) { $parser = new HttpAcceptParser(); $actual = $parser->parseWeights($header); $this->assertEquals($expected, $actual); // shouldn't be sensitive to order }
/** * Applies HTTP content negotiation. * If the negotiation is successfull, this method will set the appropriate redirect * in the OutputPage object and return. Otherwise, an HttpError is thrown. * * @param WebRequest $request * @param OutputPage $output * @param EntityId $id The ID of the entity to show * @param int $revision The desired revision * * @throws HttpError */ public function httpContentNegotiation(WebRequest $request, OutputPage $output, EntityId $id, $revision = 0) { $headers = $request->getAllHeaders(); if (isset($headers['ACCEPT'])) { $parser = new HttpAcceptParser(); $accept = $parser->parseWeights($headers['ACCEPT']); } else { // anything goes $accept = array('*' => 0.1); $defaultFormat = $this->entityDataFormatProvider->getFormatName($this->defaultFormat); $defaultMime = $this->entityDataFormatProvider->getMimeType($defaultFormat); // prefer the default if ($defaultMime != null) { $accept[$defaultMime] = 1; } } $mimeTypes = $this->entityDataFormatProvider->getSupportedMimeTypes(); $mimeTypes[] = 'text/html'; // HTML is handled by the normal page URL $negotiator = new HttpAcceptNegotiator($mimeTypes); $format = $negotiator->getBestSupportedKey($accept, null); if ($format === null) { $mimeTypes = implode(', ', $this->entityDataFormatProvider->getSupportedMimeTypes()); throw new HttpError(406, wfMessage('wikibase-entitydata-not-acceptable')->params($mimeTypes)); } $format = $this->getCanonicalFormat($format); $url = $this->uriManager->getDocUrl($id, $format, $revision); $output->redirect($url, 303); }