| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * 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://cakephp.org CakePHP(tm) Project
- * @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', ['_sendHeader', 'stop']);
- $this->Controller = new RequestHandlerTestController($request, $response);
- $this->RequestHandler = $this->Controller->components()->load('RequestHandler');
- $this->request = $request;
- Router::scope('/', function ($routes) {
- $routes->extensions('json');
- $routes->fallbacks('InflectedRoute');
- });
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- DispatcherFactory::clear();
- Router::reload();
- Router::$initialized = false;
- unset($this->RequestHandler, $this->Controller);
- }
- /**
- * Test that the constructor sets the config.
- *
- * @return void
- */
- public function testConstructorConfig()
- {
- $config = [
- 'viewClassMap' => ['json' => 'MyPlugin.MyJson']
- ];
- $controller = $this->getMock('Cake\Controller\Controller', ['redirect']);
- $collection = new ComponentRegistry($controller);
- $requestHandler = new RequestHandlerComponent($collection, $config);
- $this->assertEquals(['json' => 'MyPlugin.MyJson'], $requestHandler->config('viewClassMap'));
- }
- /**
- * testInitializeCallback method
- *
- * @return void
- */
- public function testInitializeCallback()
- {
- $this->assertNull($this->RequestHandler->ext);
- $this->Controller->request->params['_ext'] = 'rss';
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertEquals('rss', $this->RequestHandler->ext);
- }
- /**
- * test that a mapped Accept-type header will set $this->ext correctly.
- *
- * @return void
- */
- public function testInitializeContentTypeSettingExt()
- {
- Router::reload();
- Router::$initialized = true;
- $this->request->env('HTTP_ACCEPT', 'application/json');
- $this->RequestHandler->ext = null;
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
- *
- * @return void
- */
- public function testInitializeContentTypeWithjQueryAccept()
- {
- Router::reload();
- Router::$initialized = true;
- $this->request->env('HTTP_ACCEPT', 'application/json, application/javascript, */*; q=0.01');
- $this->request->env('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
- $this->RequestHandler->ext = null;
- Router::extensions('json', false);
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that RequestHandler does not set extension to csv for text/plain mimetype
- *
- * @return void
- */
- public function testInitializeContentTypeWithjQueryTextPlainAccept()
- {
- Router::reload();
- Router::$initialized = true;
- $this->request->env('HTTP_ACCEPT', 'text/plain, */*; q=0.01');
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $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()
- {
- Router::reload();
- Router::$initialized = true;
- $this->request->env('HTTP_ACCEPT', 'application/json, application/javascript, */*; q=0.01');
- $this->RequestHandler->ext = null;
- Router::extensions(['rss', 'json'], false);
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that RequestHandler does not set $this->ext when multiple accepts are sent.
- *
- * @return void
- */
- public function testInitializeNoContentTypeWithSingleAccept()
- {
- Router::reload();
- Router::$initialized = true;
- $_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
- $this->assertNull($this->RequestHandler->ext);
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $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()
- {
- $this->request->env(
- 'HTTP_ACCEPT',
- 'application/json, application/javascript, application/xml, */*; q=0.01'
- );
- $this->RequestHandler->ext = null;
- Router::extensions(['xml', 'json'], false);
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertEquals('xml', $this->RequestHandler->ext);
- $this->RequestHandler->ext = null;
- Router::extensions(['json', 'xml'], false);
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that ext is set to type with highest weight
- *
- * @return void
- */
- public function testInitializeContentTypeWithMultipleAcceptedTypes()
- {
- Router::reload();
- Router::$initialized = true;
- $this->request->env(
- 'HTTP_ACCEPT',
- 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7'
- );
- $this->RequestHandler->ext = null;
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertEquals('json', $this->RequestHandler->ext);
- }
- /**
- * Test that ext is not set with confusing android accepts headers.
- *
- * @return void
- */
- public function testInitializeAmbiguousAndroidAccepts()
- {
- Router::reload();
- Router::$initialized = true;
- $this->request->env(
- 'HTTP_ACCEPT',
- 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
- );
- $this->RequestHandler->ext = null;
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $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::extensions(['xml', 'json'], false);
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertNull($this->RequestHandler->ext);
- }
- /**
- * Test that a type mismatch doesn't incorrectly set the ext
- *
- * @return void
- */
- public function testInitializeContentTypeAndExtensionMismatch()
- {
- $this->assertNull($this->RequestHandler->ext);
- $extensions = Router::extensions();
- Router::extensions('xml', false);
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['accepts']);
- $this->Controller->request->expects($this->any())
- ->method('accepts')
- ->will($this->returnValue(['application/json']));
- $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
- $this->assertNull($this->RequestHandler->ext);
- call_user_func_array(['Cake\Routing\Router', 'extensions'], [$extensions, false]);
- }
- /**
- * testViewClassMap method
- *
- * @return void
- */
- public function testViewClassMap()
- {
- $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
- $this->RequestHandler->config(['viewClassMap' => ['json' => 'CustomJson']]);
- $this->RequestHandler->initialize([]);
- $result = $this->RequestHandler->viewClassMap();
- $expected = [
- 'json' => 'CustomJson',
- 'xml' => 'Xml',
- 'ajax' => 'Ajax'
- ];
- $this->assertEquals($expected, $result);
- $result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel');
- $expected = [
- '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);
- error_reporting($restore);
- }
- /**
- * Verify that isAjax is set on the request params for ajax requests
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testIsAjaxParams()
- {
- $this->request->env('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->Controller->beforeFilter($event);
- $this->RequestHandler->startup($event);
- $this->assertEquals(true, $this->Controller->request->params['isAjax']);
- }
- /**
- * testAutoAjaxLayout method
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testAutoAjaxLayout()
- {
- $event = new Event('Controller.startup', $this->Controller);
- $this->request->env('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
- $this->RequestHandler->initialize([]);
- $this->RequestHandler->startup($event);
- $event = new Event('Controller.beforeRender', $this->Controller);
- $this->RequestHandler->beforeRender($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([]);
- $this->RequestHandler->startup($event);
- $this->assertNotEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
- }
- /**
- * test custom JsonView class is loaded and correct.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testJsonViewLoaded()
- {
- Router::extensions(['json', 'xml', 'ajax'], false);
- $this->Controller->request->params['_ext'] = 'json';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->RequestHandler->startup($event);
- $event = new Event('Controller.beforeRender', $this->Controller);
- $this->RequestHandler->beforeRender($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
- * @triggers Controller.startup $this->Controller
- */
- public function testXmlViewLoaded()
- {
- Router::extensions(['json', 'xml', 'ajax'], false);
- $this->Controller->request->params['_ext'] = 'xml';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->RequestHandler->startup($event);
- $event = new Event('Controller.beforeRender', $this->Controller);
- $this->RequestHandler->beforeRender($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
- * @triggers Controller.startup $this->Controller
- */
- public function testAjaxViewLoaded()
- {
- Router::extensions(['json', 'xml', 'ajax'], false);
- $this->Controller->request->params['_ext'] = 'ajax';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->RequestHandler->startup($event);
- $event = new Event('Controller.beforeRender', $this->Controller);
- $this->RequestHandler->beforeRender($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
- * @triggers Controller.beforeRender $this->Controller
- */
- public function testNoViewClassExtension()
- {
- Router::extensions(['json', 'xml', 'ajax', 'csv'], false);
- $this->Controller->request->params['_ext'] = 'csv';
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->RequestHandler->startup($event);
- $this->Controller->eventManager()->on('Controller.beforeRender', function () {
- return $this->Controller->response;
- });
- $this->Controller->render();
- $this->assertEquals('RequestHandlerTest' . DS . 'csv', $this->Controller->viewBuilder()->templatePath());
- $this->assertEquals('csv', $this->Controller->viewBuilder()->layoutPath());
- }
- /**
- * testStartupCallback method
- *
- * @return void
- * @triggers Controller.beforeRender $this->Controller
- */
- public function testStartupCallback()
- {
- $event = new Event('Controller.beforeRender', $this->Controller);
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $_SERVER['CONTENT_TYPE'] = 'application/xml';
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
- $this->RequestHandler->beforeRender($event);
- $this->assertTrue(is_array($this->Controller->request->data));
- $this->assertFalse(is_object($this->Controller->request->data));
- }
- /**
- * testStartupCallback with charset.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- 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', ['_readInput']);
- $this->RequestHandler->startup($event);
- $this->assertTrue(is_array($this->Controller->request->data));
- $this->assertFalse(is_object($this->Controller->request->data));
- }
- /**
- * Test that processing data results in an array.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testStartupProcessData()
- {
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
- $this->Controller->request->expects($this->at(0))
- ->method('_readInput')
- ->will($this->returnValue(''));
- $this->Controller->request->expects($this->at(1))
- ->method('_readInput')
- ->will($this->returnValue('"invalid"'));
- $this->Controller->request->expects($this->at(2))
- ->method('_readInput')
- ->will($this->returnValue('{"valid":true}'));
- $this->Controller->request->env('REQUEST_METHOD', 'POST');
- $this->Controller->request->env('CONTENT_TYPE', 'application/json');
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->startup($event);
- $this->assertEquals([], $this->Controller->request->data);
- $this->RequestHandler->startup($event);
- $this->assertEquals(['invalid'], $this->Controller->request->data);
- $this->RequestHandler->startup($event);
- $this->assertEquals(['valid' => true], $this->Controller->request->data);
- }
- /**
- * Test that file handles are ignored as XML data.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testStartupIgnoreFileAsXml()
- {
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
- $this->Controller->request->expects($this->any())
- ->method('_readInput')
- ->will($this->returnValue('/dev/random'));
- $this->Controller->request->env('REQUEST_METHOD', 'POST');
- $this->Controller->request->env('CONTENT_TYPE', 'application/xml');
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->startup($event);
- $this->assertEquals([], $this->Controller->request->data);
- }
- /**
- * Test mapping a new type and having startup process it.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testStartupCustomTypeProcess()
- {
- $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
- $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
- $this->Controller->request->expects($this->once())
- ->method('_readInput')
- ->will($this->returnValue('"A","csv","string"'));
- $this->RequestHandler->addInputType('csv', ['str_getcsv']);
- $this->Controller->request->env('REQUEST_METHOD', 'POST');
- $this->Controller->request->env('CONTENT_TYPE', 'text/csv');
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->startup($event);
- $expected = [
- 'A', 'csv', 'string'
- ];
- $this->assertEquals($expected, $this->Controller->request->data);
- error_reporting($restore);
- }
- /**
- * testNonAjaxRedirect method
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testNonAjaxRedirect()
- {
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $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
- * @triggers Controller.startup $this->Controller
- */
- 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([]);
- $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->viewBuilder()->templatePath('request_handler_test\\rss');
- $this->RequestHandler->renderAs($this->Controller, 'js');
- $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewBuilder()->templatePath());
- }
- /**
- * 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(['1.0' => ['application/xml']]));
- $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['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', ['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', ['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',
- ['_header'],
- [$this->Controller->components()]
- );
- $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type', 'download']);
- $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
- $this->RequestHandler->request->expects($this->once())
- ->method('parseAccept')
- ->will($this->returnValue(['1.0' => ['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', ['attachment' => 'myfile.xml']);
- $this->assertTrue($result);
- }
- /**
- * test that calling renderAs() more than once continues to work.
- *
- * @link #6466
- * @return void
- */
- public function testRenderAsCalledTwice()
- {
- $this->Controller->eventManager()->on('Controller.beforeRender', function (\Cake\Event\Event $e) {
- return $e->subject()->response;
- });
- $this->Controller->render();
- $this->RequestHandler->renderAs($this->Controller, 'print');
- $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewBuilder()->templatePath());
- $this->assertEquals('print', $this->Controller->viewBuilder()->layoutPath());
- $this->RequestHandler->renderAs($this->Controller, 'js');
- $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewBuilder()->templatePath());
- $this->assertEquals('js', $this->Controller->viewBuilder()->layoutPath());
- }
- /**
- * testRequestContentTypes method
- *
- * @return void
- */
- public function testRequestContentTypes()
- {
- $this->request->env('REQUEST_METHOD', 'GET');
- $this->assertNull($this->RequestHandler->requestedWith());
- $this->request->env('REQUEST_METHOD', 'POST');
- $this->request->env('CONTENT_TYPE', 'application/json');
- $this->assertEquals('json', $this->RequestHandler->requestedWith());
- $result = $this->RequestHandler->requestedWith(['json', 'xml']);
- $this->assertEquals('json', $result);
- $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
- $this->assertFalse($result);
- $this->request->env('REQUEST_METHOD', 'PATCH');
- $this->assertEquals('json', $this->RequestHandler->requestedWith());
- $this->request->env('REQUEST_METHOD', 'DELETE');
- $this->assertEquals('json', $this->RequestHandler->requestedWith());
- $this->request->env('REQUEST_METHOD', 'POST');
- $this->request->env('CONTENT_TYPE', '');
- $this->request->env('HTTP_CONTENT_TYPE', 'application/json');
- $result = $this->RequestHandler->requestedWith(['json', 'xml']);
- $this->assertEquals('json', $result);
- $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
- $this->assertFalse($result);
- $this->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->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->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->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(['xml', 'js', 'json']);
- $expected = ['application/xml', 'application/javascript', 'application/json'];
- $this->assertEquals($expected, $result);
- }
- /**
- * test accepts() on the component
- *
- * @return void
- */
- public function testAccepts()
- {
- $this->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->assertTrue($this->RequestHandler->accepts(['js', 'xml', 'html']));
- $this->assertFalse($this->RequestHandler->accepts(['gif', 'jpeg', 'foo']));
- $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
- $this->assertFalse($this->RequestHandler->accepts('rss'));
- }
- /**
- * test accepts and prefers methods.
- *
- * @return void
- */
- public function testPrefers()
- {
- $this->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(['js', 'xml', 'xhtml']));
- $this->assertFalse($this->RequestHandler->prefers(['red', 'blue']));
- $this->assertEquals('xhtml', $this->RequestHandler->prefers(['js', 'json', 'xhtml']));
- $this->assertTrue($this->RequestHandler->prefers(['rss']), 'Should return true if input matches ext.');
- $this->assertFalse($this->RequestHandler->prefers(['html']), 'No match with ext, return false.');
- $this->_init();
- $this->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->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
- * @triggers Controller.beforeRedirect $this->Controller
- */
- 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', ['_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));
- $response = $this->Controller->RequestHandler->beforeRedirect(
- $event,
- ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
- $this->Controller->response
- );
- $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
- }
- /**
- * Test that AJAX requests involving redirects handle querystrings
- *
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestActionWithQueryString()
- {
- 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', ['_sendHeader', 'stop']);
- $this->Controller->RequestHandler->request = $this->Controller->request;
- $this->Controller->RequestHandler->response = $this->Controller->response;
- $this->Controller->request->expects($this->any())
- ->method('is')
- ->with('ajax')
- ->will($this->returnValue(true));
- $response = $this->Controller->RequestHandler->beforeRedirect(
- $event,
- '/request_action/params_pass?a=b&x=y?ish',
- $this->Controller->response
- );
- $data = json_decode($response, true);
- $this->assertEquals('/request_action/params_pass', $data['here']);
- $response = $this->Controller->RequestHandler->beforeRedirect(
- $event,
- '/request_action/query_pass?a=b&x=y?ish',
- $this->Controller->response
- );
- $data = json_decode($response, true);
- $this->assertEquals('y?ish', $data['x']);
- }
- /**
- * Test that AJAX requests involving redirects handle cookie data
- *
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestActionWithCookieData()
- {
- 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', ['_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));
- $cookies = [
- 'foo' => 'bar'
- ];
- $this->Controller->request->cookies = $cookies;
- $response = $this->Controller->RequestHandler->beforeRedirect(
- $event,
- '/request_action/cookie_pass',
- $this->Controller->response
- );
- $data = json_decode($response, true);
- $this->assertEquals($cookies, $data);
- }
- /**
- * Tests that AJAX requests involving redirects don't let the status code bleed through.
- *
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestActionStatusCode()
- {
- 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', ['_sendHeader', 'stop']);
- $this->Controller->response->statusCode(302);
- $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));
- $response = $this->Controller->RequestHandler->beforeRedirect(
- $event,
- ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
- $this->Controller->response
- );
- $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
- $this->assertSame(200, $response->statusCode());
- }
- /**
- * test that ajax requests involving redirects don't force no layout
- * this would cause the ajax layout to not be rendered.
- *
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- 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', ['_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));
- $response = $this->Controller->RequestHandler->beforeRedirect(
- $event,
- ['controller' => 'RequestHandlerTest', 'action' => 'ajax2_layout'],
- $this->Controller->response
- );
- $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
- $this->assertRegExp('/Ajax!/', $response->body(), '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
- * @triggers Controller.beforeRender $this->Controller
- */
- 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([
- ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
- ['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,
- ['controller' => 'RequestHandlerTest', 'action' => 'param_method', 'first', 'second'],
- $this->Controller->response
- );
- $result = ob_get_clean();
- $this->assertEquals('one: first two: second', $result);
- }
- /**
- * testAddInputTypeException method
- *
- * @expectedException \Cake\Core\Exception\Exception
- * @return void
- */
- public function testAddInputTypeException()
- {
- $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
- $this->RequestHandler->addInputType('csv', ['I am not callable']);
- error_reporting($restore);
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- * @triggers Controller.beforeRender $this->Controller
- */
- 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', ['notModified', 'stop']);
- $RequestHandler->response->etag('something');
- $RequestHandler->response->expects($this->once())->method('notModified');
- $this->assertFalse($RequestHandler->beforeRender($event));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- * @triggers Controller.beforeRender
- */
- 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', ['notModified', 'stop']);
- $RequestHandler->response->etag('something', true);
- $RequestHandler->response->expects($this->once())->method('notModified');
- $this->assertFalse($RequestHandler->beforeRender($event));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- * @triggers Controller.beforeRender $this->Controller
- */
- 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', ['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
- * @triggers Controller.beforeRender $this->Controller
- */
- public function testCheckNotModifiedNoInfo()
- {
- $event = new Event('Controller.beforeRender', $this->Controller);
- $RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
- $RequestHandler->response->expects($this->never())->method('notModified');
- $this->assertNull($RequestHandler->beforeRender($event, '', $RequestHandler->response));
- }
- /**
- * Test default options in construction
- *
- * @return void
- */
- public function testConstructDefaultOptions()
- {
- $requestHandler = new RequestHandlerComponent($this->Controller->components());
- $viewClass = $requestHandler->config('viewClassMap');
- $expected = [
- 'json' => 'Json',
- 'xml' => 'Xml',
- 'ajax' => 'Ajax',
- ];
- $this->assertEquals($expected, $viewClass);
- $inputs = $requestHandler->config('inputTypeMap');
- $this->assertArrayHasKey('json', $inputs);
- $this->assertArrayHasKey('xml', $inputs);
- }
- /**
- * Test options in constructor replace defaults
- *
- * @return void
- */
- public function testConstructReplaceOptions()
- {
- $requestHandler = new RequestHandlerComponent(
- $this->Controller->components(),
- [
- 'viewClassMap' => ['json' => 'Json'],
- 'inputTypeMap' => ['json' => ['json_decode', true]]
- ]
- );
- $viewClass = $requestHandler->config('viewClassMap');
- $expected = [
- 'json' => 'Json',
- ];
- $this->assertEquals($expected, $viewClass);
- $inputs = $requestHandler->config('inputTypeMap');
- $this->assertArrayHasKey('json', $inputs);
- $this->assertCount(1, $inputs);
- }
- }
|