| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910 |
- <?php
- /**
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Controller\Component;
- use Cake\Controller\ComponentRegistry;
- use Cake\Controller\Component\RequestHandlerComponent;
- use Cake\Controller\Controller;
- use Cake\Core\App;
- use Cake\Core\Configure;
- use Cake\Event\Event;
- use Cake\Network\Request;
- use Cake\Network\Response;
- use Cake\Routing\DispatcherFactory;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- use TestApp\Controller\RequestHandlerTestController;
- /**
- * RequestHandlerComponentTest class
- */
- class RequestHandlerComponentTest extends TestCase {
- /**
- * Controller property
- *
- * @var RequestHandlerTestController
- */
- public $Controller;
- /**
- * RequestHandler property
- *
- * @var RequestHandlerComponent
- */
- public $RequestHandler;
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('App.namespace', 'TestApp');
- DispatcherFactory::add('Routing');
- DispatcherFactory::add('ControllerFactory');
- $this->_init();
- }
- /**
- * init method
- *
- * @return void
- */
- protected function _init() {
- $request = new Request('controller_posts/index');
- $response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
- $this->Controller = new RequestHandlerTestController($request, $response);
- $this->Controller->constructClasses();
- $this->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- Router::scope('/', function($routes) {
- $routes->extensions('json');
- $routes->fallbacks();
- });
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- DispatcherFactory::clear();
- $this->_init();
- unset($this->RequestHandler, $this->Controller);
- }
- /**
- * Test that the constructor sets the config.
- *
- * @return void
- */
- public function testConstructorConfig() {
- $config = array(
- 'viewClassMap' => array('json' => 'MyPlugin.MyJson')
- );
- $controller = $this->getMock('Cake\Controller\Controller');
- $collection = new ComponentRegistry($controller);
- $requestHandler = new RequestHandlerComponent($collection, $config);
- $this->assertEquals(array('json' => 'MyPlugin.MyJson'), $requestHandler->config('viewClassMap'));
- }
- /**
- * testInitializeCallback method
- *
- * @return void
- */
- public function testInitializeCallback() {
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- $this->Controller->request->params['_ext'] = 'rss';
- $this->RequestHandler->initialize($event);
- $this->assertEquals('rss', $this->RequestHandler->ext);
- }
- /**
- * test that a mapped Accept-type header will set $this->ext correctly.
- *
- * @return void
- */
- public function testInitializeContentTypeSettingExt() {
- $event = new Event('Controller.initialize', $this->Controller);
- $_SERVER['HTTP_ACCEPT'] = 'application/json';
- Router::parseExtensions('json', false);
- $this->assertNull($this->RequestHandler->ext);
- $this->RequestHandler->initialize($event);
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
- *
- * @return void
- */
- public function testInitializeContentTypeWithjQueryAccept() {
- $_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- Router::parseExtensions('json', false);
- $this->RequestHandler->initialize($event);
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that RequestHandler does not set extension to csv for text/plain mimetype
- *
- * @return void
- */
- public function testInitializeContentTypeWithjQueryTextPlainAccept() {
- $_SERVER['HTTP_ACCEPT'] = 'text/plain, */*; q=0.01';
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- Router::parseExtensions('csv', false);
- $this->RequestHandler->initialize($event);
- $this->assertNull($this->RequestHandler->ext);
- }
- /**
- * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers
- * and the application is configured to handle multiple extensions
- *
- * @return void
- */
- public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions() {
- $_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- Router::parseExtensions(['rss', 'json'], false);
- $this->RequestHandler->initialize($event);
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that RequestHandler does not set $this->ext when multiple accepts are sent.
- *
- * @return void
- */
- public function testInitializeNoContentTypeWithSingleAccept() {
- $_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- Router::parseExtensions('json', false);
- $this->RequestHandler->initialize($event);
- $this->assertNull($this->RequestHandler->ext);
- }
- /**
- * Test that ext is set to the first listed extension with multiple accepted
- * content types.
- * Having multiple types accepted with same weight, means the client lets the
- * server choose the returned content type.
- *
- * @return void
- */
- public function testInitializeNoContentTypeWithMultipleAcceptedTypes() {
- $_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, application/xml, */*; q=0.01';
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- Router::parseExtensions(['xml', 'json'], false);
- $this->RequestHandler->initialize($event);
- $this->assertEquals('xml', $this->RequestHandler->ext);
- $this->RequestHandler->ext = null;
- Router::parseExtensions(array('json', 'xml'), false);
- $this->RequestHandler->initialize($event);
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that ext is set to type with highest weight
- *
- * @return void
- */
- public function testInitializeContentTypeWithMultipleAcceptedTypes() {
- $_SERVER['HTTP_ACCEPT'] = 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7';
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- Router::parseExtensions(['xml', 'json'], false);
- $this->RequestHandler->initialize($event);
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that ext is not set with confusing android accepts headers.
- *
- * @return void
- */
- public function testInitializeAmbiguousAndroidAccepts() {
- $_SERVER['HTTP_ACCEPT'] = 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- Router::parseExtensions(['html', 'xml'], false);
- $this->RequestHandler->initialize($event);
- $this->assertNull($this->RequestHandler->ext);
- }
- /**
- * Test that the headers sent by firefox are not treated as XML requests.
- *
- * @return void
- */
- public function testInititalizeFirefoxHeaderNotXml() {
- $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
- Router::parseExtensions(['xml', 'json'], false);
- $event = new Event('Controller.initialize', $this->Controller);
- $this->RequestHandler->initialize($event);
- $this->assertNull($this->RequestHandler->ext);
- }
- /**
- * Test that a type mismatch doesn't incorrectly set the ext
- *
- * @return void
- */
- public function testInitializeContentTypeAndExtensionMismatch() {
- $event = new Event('Controller.initialize', $this->Controller);
- $this->assertNull($this->RequestHandler->ext);
- $extensions = Router::extensions();
- Router::parseExtensions('xml', false);
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['accepts']);
- $this->Controller->request->expects($this->any())
- ->method('accepts')
- ->will($this->returnValue(array('application/json')));
- $this->RequestHandler->initialize($event);
- $this->assertNull($this->RequestHandler->ext);
- call_user_func_array(array('Cake\Routing\Router', 'parseExtensions'), [$extensions, false]);
- }
- /**
- * testViewClassMap method
- *
- * @return void
- */
- public function testViewClassMap() {
- $event = new Event('Controller.initialize', $this->Controller);
- $this->RequestHandler->config(array('viewClassMap' => array('json' => 'CustomJson')));
- $this->RequestHandler->initialize($event);
- $result = $this->RequestHandler->viewClassMap();
- $expected = array(
- 'json' => 'CustomJson',
- 'xml' => 'Xml',
- 'ajax' => 'Ajax'
- );
- $this->assertEquals($expected, $result);
- $result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel');
- $expected = array(
- 'json' => 'CustomJson',
- 'xml' => 'Xml',
- 'ajax' => 'Ajax',
- 'xls' => 'Excel.Excel'
- );
- $this->assertEquals($expected, $result);
- $this->RequestHandler->renderAs($this->Controller, 'json');
- $this->assertEquals('TestApp\View\CustomJsonView', $this->Controller->viewClass);
- }
- /**
- * testDisabling method
- *
- * @return void
- */
- public function testDisabling() {
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $this->_init();
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize($event);
- $this->Controller->beforeFilter($event);
- $this->RequestHandler->startup($event);
- $this->assertEquals(true, $this->Controller->request->params['isAjax']);
- }
- /**
- * testAutoAjaxLayout method
- *
- * @return void
- */
- public function testAutoAjaxLayout() {
- $event = new Event('Controller.startup', $this->Controller);
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
- $view = $this->Controller->createView();
- $this->assertEquals('ajax', $view->layout);
- $this->_init();
- $this->Controller->request->params['_ext'] = 'js';
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertNotEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
- unset($_SERVER['HTTP_X_REQUESTED_WITH']);
- }
- /**
- * test custom JsonView class is loaded and correct.
- *
- * @return void
- */
- public function testJsonViewLoaded() {
- Router::parseExtensions(['json', 'xml', 'ajax'], false);
- $this->Controller->request->params['_ext'] = 'json';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertEquals('Cake\View\JsonView', $this->Controller->viewClass);
- $view = $this->Controller->createView();
- $this->assertEquals('json', $view->layoutPath);
- $this->assertEquals('json', $view->subDir);
- }
- /**
- * test custom XmlView class is loaded and correct.
- *
- * @return void
- */
- public function testXmlViewLoaded() {
- Router::parseExtensions(['json', 'xml', 'ajax'], false);
- $this->Controller->request->params['_ext'] = 'xml';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
- $view = $this->Controller->createView();
- $this->assertEquals('xml', $view->layoutPath);
- $this->assertEquals('xml', $view->subDir);
- }
- /**
- * test custom AjaxView class is loaded and correct.
- *
- * @return void
- */
- public function testAjaxViewLoaded() {
- Router::parseExtensions(['json', 'xml', 'ajax'], false);
- $this->Controller->request->params['_ext'] = 'ajax';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertEquals('Cake\View\AjaxView', $this->Controller->viewClass);
- $view = $this->Controller->createView();
- $this->assertEquals('ajax', $view->layout);
- }
- /**
- * test configured extension but no view class set.
- *
- * @return void
- */
- public function testNoViewClassExtension() {
- Router::parseExtensions(['json', 'xml', 'ajax', 'csv'], false);
- $this->Controller->request->params['_ext'] = 'csv';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertEquals('RequestHandlerTest' . DS . 'csv', $this->Controller->viewPath);
- $this->assertEquals('csv', $this->Controller->layoutPath);
- }
- /**
- * testStartupCallback method
- *
- * @return void
- */
- public function testStartupCallback() {
- $event = new Event('Controller.startup', $this->Controller);
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $_SERVER['CONTENT_TYPE'] = 'application/xml';
- $this->Controller->request = $this->getMock('Cake\Network\Request', array('_readInput'));
- $this->RequestHandler->startup($event);
- $this->assertTrue(is_array($this->Controller->request->data));
- $this->assertFalse(is_object($this->Controller->request->data));
- }
- /**
- * testStartupCallback with charset.
- *
- * @return void
- */
- public function testStartupCallbackCharset() {
- $event = new Event('Controller.startup', $this->Controller);
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
- $this->Controller->request = $this->getMock('Cake\Network\Request', array('_readInput'));
- $this->RequestHandler->startup($event);
- $this->assertTrue(is_array($this->Controller->request->data));
- $this->assertFalse(is_object($this->Controller->request->data));
- }
- /**
- * Test mapping a new type and having startup process it.
- *
- * @return void
- */
- public function testStartupCustomTypeProcess() {
- if (!function_exists('str_getcsv')) {
- $this->markTestSkipped('Need "str_getcsv" for this test.');
- }
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->request = $this->getMock('Cake\Network\Request', array('_readInput'));
- $this->Controller->request->expects($this->once())
- ->method('_readInput')
- ->will($this->returnValue('"A","csv","string"'));
- $this->RequestHandler->addInputType('csv', array('str_getcsv'));
- $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
- $this->RequestHandler->request->env('CONTENT_TYPE', 'text/csv');
- $this->RequestHandler->startup($event);
- $expected = array(
- 'A', 'csv', 'string'
- );
- $this->assertEquals($expected, $this->Controller->request->data);
- }
- /**
- * testNonAjaxRedirect method
- *
- * @return void
- */
- public function testNonAjaxRedirect() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertNull($this->RequestHandler->beforeRedirect($event, '/', $this->Controller->response));
- }
- /**
- * test that redirects with ajax and no URL don't do anything.
- *
- * @return void
- */
- public function testAjaxRedirectWithNoUrl() {
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->response = $this->getMock('Cake\Network\Response');
- $this->Controller->response->expects($this->never())
- ->method('body');
- $this->RequestHandler->initialize($event);
- $this->RequestHandler->startup($event);
- $this->assertNull($this->RequestHandler->beforeRedirect($event, null, $this->Controller->response));
- }
- /**
- * testRenderAs method
- *
- * @return void
- */
- public function testRenderAs() {
- $this->assertFalse(in_array('Rss', $this->Controller->helpers));
- $this->RequestHandler->renderAs($this->Controller, 'rss');
- $this->assertTrue(in_array('Rss', $this->Controller->helpers));
- $this->Controller->viewPath = 'request_handler_test\\rss';
- $this->RequestHandler->renderAs($this->Controller, 'js');
- $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath);
- }
- /**
- * test that attachment headers work with renderAs
- *
- * @return void
- */
- public function testRenderAsWithAttachment() {
- $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
- $this->RequestHandler->request->expects($this->any())
- ->method('parseAccept')
- ->will($this->returnValue(array('1.0' => array('application/xml'))));
- $this->RequestHandler->response = $this->getMock('Cake\Network\Response', array('type', 'download', 'charset'));
- $this->RequestHandler->response->expects($this->at(0))
- ->method('type')
- ->with('application/xml');
- $this->RequestHandler->response->expects($this->at(1))
- ->method('charset')
- ->with('UTF-8');
- $this->RequestHandler->response->expects($this->at(2))
- ->method('download')
- ->with('myfile.xml');
- $this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
- $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
- }
- /**
- * test that respondAs works as expected.
- *
- * @return void
- */
- public function testRespondAs() {
- $this->RequestHandler->response = $this->getMock('Cake\Network\Response', array('type'));
- $this->RequestHandler->response->expects($this->at(0))->method('type')
- ->with('application/json');
- $this->RequestHandler->response->expects($this->at(1))->method('type')
- ->with('text/xml');
- $result = $this->RequestHandler->respondAs('json');
- $this->assertTrue($result);
- $result = $this->RequestHandler->respondAs('text/xml');
- $this->assertTrue($result);
- }
- /**
- * test that attachment headers work with respondAs
- *
- * @return void
- */
- public function testRespondAsWithAttachment() {
- $this->RequestHandler = $this->getMock(
- 'Cake\Controller\Component\RequestHandlerComponent',
- array('_header'),
- array($this->Controller->components())
- );
- $this->RequestHandler->response = $this->getMock('Cake\Network\Response', array('type', 'download'));
- $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
- $this->RequestHandler->request->expects($this->once())
- ->method('parseAccept')
- ->will($this->returnValue(array('1.0' => array('application/xml'))));
- $this->RequestHandler->response->expects($this->once())->method('download')
- ->with('myfile.xml');
- $this->RequestHandler->response->expects($this->once())->method('type')
- ->with('application/xml');
- $result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
- $this->assertTrue($result);
- }
- /**
- * test that calling renderAs() more than once continues to work.
- *
- * @link #6466
- * @return void
- */
- public function testRenderAsCalledTwice() {
- $this->RequestHandler->renderAs($this->Controller, 'print');
- $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
- $this->assertEquals('print', $this->Controller->layoutPath);
- $this->RequestHandler->renderAs($this->Controller, 'js');
- $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
- $this->assertEquals('js', $this->Controller->layoutPath);
- }
- /**
- * testRequestContentTypes method
- *
- * @return void
- */
- public function testRequestContentTypes() {
- $this->RequestHandler->request->env('REQUEST_METHOD', 'GET');
- $this->assertNull($this->RequestHandler->requestedWith());
- $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
- $this->RequestHandler->request->env('CONTENT_TYPE', 'application/json');
- $this->assertEquals('json', $this->RequestHandler->requestedWith());
- $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
- $this->assertEquals('json', $result);
- $result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
- $this->assertFalse($result);
- $this->RequestHandler->request->env('REQUEST_METHOD', 'DELETE');
- $this->assertEquals('json', $this->RequestHandler->requestedWith());
- $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
- $this->RequestHandler->request->env('CONTENT_TYPE', '');
- $this->RequestHandler->request->env('HTTP_CONTENT_TYPE', 'application/json');
- $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
- $this->assertEquals('json', $result);
- $result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
- $this->assertFalse($result);
- $this->RequestHandler->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
- $this->assertTrue($this->RequestHandler->isXml());
- $this->assertFalse($this->RequestHandler->isAtom());
- $this->assertFalse($this->RequestHandler->isRSS());
- $this->RequestHandler->request->env('HTTP_ACCEPT', 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
- $this->assertTrue($this->RequestHandler->isAtom());
- $this->assertFalse($this->RequestHandler->isRSS());
- $this->RequestHandler->request->env('HTTP_ACCEPT', 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
- $this->assertFalse($this->RequestHandler->isAtom());
- $this->assertTrue($this->RequestHandler->isRSS());
- $this->assertFalse($this->RequestHandler->isWap());
- $this->RequestHandler->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
- $this->assertTrue($this->RequestHandler->isWap());
- }
- /**
- * testResponseContentType method
- *
- * @return void
- */
- public function testResponseContentType() {
- $this->assertEquals('html', $this->RequestHandler->responseType());
- $this->assertTrue($this->RequestHandler->respondAs('atom'));
- $this->assertEquals('atom', $this->RequestHandler->responseType());
- }
- /**
- * testMobileDeviceDetection method
- *
- * @return void
- */
- public function testMobileDeviceDetection() {
- $request = $this->getMock('Cake\Network\Request', ['is']);
- $request->expects($this->once())->method('is')
- ->with('mobile')
- ->will($this->returnValue(true));
- $this->RequestHandler->request = $request;
- $this->assertTrue($this->RequestHandler->isMobile());
- }
- /**
- * test that map alias converts aliases to content types.
- *
- * @return void
- */
- public function testMapAlias() {
- $result = $this->RequestHandler->mapAlias('xml');
- $this->assertEquals('application/xml', $result);
- $result = $this->RequestHandler->mapAlias('text/html');
- $this->assertNull($result);
- $result = $this->RequestHandler->mapAlias('wap');
- $this->assertEquals('text/vnd.wap.wml', $result);
- $result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
- $expected = array('application/xml', 'application/javascript', 'application/json');
- $this->assertEquals($expected, $result);
- }
- /**
- * test accepts() on the component
- *
- * @return void
- */
- public function testAccepts() {
- $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
- $this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
- $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
- $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
- $this->assertFalse($this->RequestHandler->accepts('rss'));
- }
- /**
- * test accepts and prefers methods.
- *
- * @return void
- */
- public function testPrefers() {
- $this->RequestHandler->request->env(
- 'HTTP_ACCEPT',
- 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
- );
- $this->assertNotEquals('rss', $this->RequestHandler->prefers());
- $this->RequestHandler->ext = 'rss';
- $this->assertEquals('rss', $this->RequestHandler->prefers());
- $this->assertFalse($this->RequestHandler->prefers('xml'));
- $this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml')));
- $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
- $this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml')));
- $this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
- $this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
- $this->_init();
- $this->RequestHandler->request->env(
- 'HTTP_ACCEPT',
- 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
- );
- $this->assertEquals('xml', $this->RequestHandler->prefers());
- $this->RequestHandler->request->env('HTTP_ACCEPT', '*/*;q=0.5');
- $this->assertEquals('html', $this->RequestHandler->prefers());
- $this->assertFalse($this->RequestHandler->prefers('rss'));
- }
- /**
- * test that ajax requests involving redirects trigger requestAction instead.
- *
- * @return void
- */
- public function testAjaxRedirectAsRequestAction() {
- Configure::write('App.namespace', 'TestApp');
- Router::connect('/:controller/:action');
- $event = new Event('Controller.beforeRedirect', $this->Controller);
- $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
- $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
- $this->Controller->RequestHandler->request = $this->Controller->request;
- $this->Controller->RequestHandler->response = $this->Controller->response;
- $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
- $this->Controller->response->expects($this->once())->method('stop');
- ob_start();
- $this->Controller->RequestHandler->beforeRedirect(
- $event,
- array('controller' => 'request_handler_test', 'action' => 'destination'),
- $this->Controller->response
- );
- $result = ob_get_clean();
- $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
- }
- /**
- * test that ajax requests involving redirects don't force no layout
- * this would cause the ajax layout to not be rendered.
- *
- * @return void
- */
- public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
- Configure::write('App.namespace', 'TestApp');
- Router::connect('/:controller/:action');
- $event = new Event('Controller.beforeRedirect', $this->Controller);
- $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
- $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
- $this->Controller->RequestHandler->request = $this->Controller->request;
- $this->Controller->RequestHandler->response = $this->Controller->response;
- $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
- $this->Controller->response->expects($this->once())->method('stop');
- ob_start();
- $this->Controller->RequestHandler->beforeRedirect(
- $event,
- array('controller' => 'request_handler_test', 'action' => 'ajax2_layout'),
- $this->Controller->response
- );
- $result = ob_get_clean();
- $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
- $this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
- }
- /**
- * test that the beforeRedirect callback properly converts
- * array URLs into their correct string ones, and adds base => false so
- * the correct URLs are generated.
- *
- * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
- * @return void
- */
- public function testBeforeRedirectCallbackWithArrayUrl() {
- Configure::write('App.namespace', 'TestApp');
- Router::connect('/:controller/:action/*');
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $event = new Event('Controller.beforeRender', $this->Controller);
- Router::setRequestInfo(array(
- array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array()),
- array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
- ));
- $RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $RequestHandler->request = new Request('posts/index');
- $RequestHandler->response = $this->Controller->response;
- ob_start();
- $RequestHandler->beforeRedirect(
- $event,
- array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second'),
- $this->Controller->response
- );
- $result = ob_get_clean();
- $this->assertEquals('one: first two: second', $result);
- }
- /**
- * testAddInputTypeException method
- *
- * @expectedException \Cake\Error\Exception
- * @return void
- */
- public function testAddInputTypeException() {
- $this->RequestHandler->addInputType('csv', array('I am not callable'));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- */
- public function testCheckNotModifiedByEtagStar() {
- $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
- $event = new Event('Controller.beforeRender', $this->Controller);
- $RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
- $RequestHandler->response->etag('something');
- $RequestHandler->response->expects($this->once())->method('notModified');
- $this->assertFalse($RequestHandler->beforeRender($event));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- */
- public function testCheckNotModifiedByEtagExact() {
- $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
- $event = new Event('Controller.beforeRender');
- $RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
- $RequestHandler->response->etag('something', true);
- $RequestHandler->response->expects($this->once())->method('notModified');
- $this->assertFalse($RequestHandler->beforeRender($event));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- */
- public function testCheckNotModifiedByEtagAndTime() {
- $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
- $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
- $event = new Event('Controller.beforeRender', $this->Controller);
- $RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
- $RequestHandler->response->etag('something', true);
- $RequestHandler->response->modified('2012-01-01 00:00:00');
- $RequestHandler->response->expects($this->once())->method('notModified');
- $this->assertFalse($RequestHandler->beforeRender($event));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- */
- public function testCheckNotModifiedNoInfo() {
- $event = new Event('Controller.beforeRender', $this->Controller);
- $RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
- $RequestHandler->response->expects($this->never())->method('notModified');
- $this->assertNull($RequestHandler->beforeRender($event, '', $RequestHandler->response));
- }
- }
|