public function testFindByAccept() { $html = MediaType::add('html', ['conditions' => ['accept' => ['application/one', 'application/two']]]); $json = MediaType::add('json', ['conditions' => ['accept' => ['application/three', '*/*']]]); $this->assertEquals('html', MediaType::findByAccept('*/*')->options['name']); $this->assertEquals('html', MediaType::findByAccept('application/one;q=0.2,application/three;q=0.5,application/two;q=0.9')->options['name']); $this->assertEquals(false, MediaType::findByAccept('application/five;q=0.2')); }
public function setUp() { $this->controller = new MockController(); $this->controller->method = 'index'; $this->controller->params = []; MediaType::add('html', ['conditions' => ['accept' => ['text/html', '*/*']], 'encode' => function ($request, $response) { return $response; }]); }
public static function render($request, $response) { $mediaByType = MediaType::findByType($request['type']); $mediaByAccept = MediaType::findByAccept($request['accept']); $mediaType = $mediaByType ?: $mediaByAccept; if (!$mediaType) { return App::abort(406, 'Unrecognized accept type.'); } return $mediaType->encode($request, $response); }