| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 1.2.0
- * @license https://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\Event\Event;
- use Cake\Http\Response;
- use Cake\Http\ServerRequest;
- use Cake\Routing\DispatcherFactory;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- use TestApp\Controller\RequestHandlerTestController;
- use Zend\Diactoros\Stream;
- /**
- * RequestHandlerComponentTest class
- */
- class RequestHandlerComponentTest extends TestCase
- {
- /**
- * Controller property
- *
- * @var RequestHandlerTestController
- */
- public $Controller;
- /**
- * RequestHandler property
- *
- * @var RequestHandlerComponent
- */
- public $RequestHandler;
- /**
- * Backup of $_SERVER
- *
- * @var array
- */
- protected $server = [];
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->server = $_SERVER;
- static::setAppNamespace();
- DispatcherFactory::add('Routing');
- DispatcherFactory::add('ControllerFactory');
- $this->_init();
- }
- /**
- * init method
- *
- * @return void
- */
- protected function _init()
- {
- $request = new ServerRequest('controller_posts/index');
- $response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['_sendHeader', 'stop'])
- ->getMock();
- $this->Controller = new RequestHandlerTestController($request, $response);
- $this->RequestHandler = $this->Controller->components()->load('RequestHandler');
- $this->request = $request;
- Router::scope('/', function ($routes) {
- $routes->setExtensions('json');
- $routes->fallbacks('InflectedRoute');
- });
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- DispatcherFactory::clear();
- Router::reload();
- Router::$initialized = false;
- $_SERVER = $this->server;
- unset($this->RequestHandler, $this->Controller);
- }
- /**
- * Test that the constructor sets the config.
- *
- * @return void
- */
- public function testConstructorConfig()
- {
- $config = [
- 'viewClassMap' => ['json' => 'MyPlugin.MyJson']
- ];
- $controller = $this->getMockBuilder('Cake\Controller\Controller')
- ->setMethods(['redirect'])
- ->getMock();
- $collection = new ComponentRegistry($controller);
- $requestHandler = new RequestHandlerComponent($collection, $config);
- $this->assertEquals(['json' => 'MyPlugin.MyJson'], $requestHandler->getConfig('viewClassMap'));
- }
- /**
- * testInitializeCallback method
- *
- * @return void
- */
- public function testInitializeCallback()
- {
- $this->assertNull($this->RequestHandler->ext);
- $this->Controller->request = $this->Controller->request->withParam('_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->Controller->request = $this->request->withHeader('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->Controller->request = $this->request
- ->withHeader('Accept', 'application/json, application/javascript, */*; q=0.01')
- ->withHeader('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->Controller->request = $this->request->withHeader('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->Controller->request = $this->request->withHeader('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->Controller->request = $this->request->withHeader(
- '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->Controller->request = $this->request->withHeader(
- '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 = $this->request->withEnv(
- '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->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['accepts'])
- ->getMock();
- $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);
- Router::extensions($extensions, false);
- }
- /**
- * testViewClassMap
- *
- * @return void
- */
- public function testViewClassMap()
- {
- $this->RequestHandler->setConfig(['viewClassMap' => ['json' => 'CustomJson']]);
- $result = $this->RequestHandler->getConfig('viewClassMap');
- $expected = [
- 'json' => 'CustomJson',
- 'xml' => 'Xml',
- 'ajax' => 'Ajax'
- ];
- $this->assertEquals($expected, $result);
- $this->RequestHandler->setConfig(['viewClassMap' => ['xls' => 'Excel.Excel']]);
- $result = $this->RequestHandler->getConfig('viewClassMap');
- $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->viewBuilder()->getClassName());
- }
- /**
- * test addInputType method
- *
- * @group deprecated
- * @return void
- */
- public function testDeprecatedAddInputType()
- {
- $this->deprecated(function () {
- $this->RequestHandler->addInputType('csv', ['str_getcsv']);
- $result = $this->RequestHandler->getConfig('inputTypeMap');
- $this->assertArrayHasKey('csv', $result);
- });
- }
- /**
- * testViewClassMap method
- *
- * @group deprecated
- * @return void
- */
- public function testViewClassMapMethod()
- {
- $this->deprecated(function () {
- $this->RequestHandler->setConfig(['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);
- });
- }
- /**
- * Verify that isAjax is set on the request params for ajax requests
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testIsAjaxParams()
- {
- $this->Controller->request = $this->request->withHeader('X-Requested-With', 'XMLHttpRequest');
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->Controller->beforeFilter($event);
- $this->RequestHandler->startup($event);
- $this->assertTrue($this->Controller->request->getParam('isAjax'));
- }
- /**
- * testAutoAjaxLayout method
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testAutoAjaxLayout()
- {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->request = $this->request->withHeader('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->getLayout());
- $this->_init();
- $this->Controller->request = $this->Controller->request->withParam('_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 = $this->Controller->request->withParam('_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->getLayoutPath());
- $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 = $this->Controller->request->withParam('_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->getLayoutPath());
- $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 = $this->Controller->request->withParam('_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->getLayout());
- }
- /**
- * 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 = $this->Controller->request->withParam('_ext', 'csv');
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->RequestHandler->startup($event);
- $this->Controller->getEventManager()->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 = new ServerRequest();
- $this->RequestHandler->beforeRender($event);
- $this->assertInternalType('array', $this->Controller->request->getData());
- $this->assertNotInternalType('object', $this->Controller->request->getData());
- }
- /**
- * 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 = new ServerRequest();
- $this->RequestHandler->startup($event);
- $this->assertInternalType('array', $this->Controller->request->getData());
- $this->assertNotInternalType('object', $this->Controller->request->getData());
- }
- /**
- * Test that processing data results in an array.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testStartupProcessData()
- {
- $this->Controller->request = new ServerRequest([
- 'environment' => [
- 'REQUEST_METHOD' => 'POST',
- 'CONTENT_TYPE' => 'application/json'
- ]
- ]);
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->startup($event);
- $this->assertEquals([], $this->Controller->request->getData());
- $stream = new Stream('php://memory', 'w');
- $stream->write('"invalid"');
- $this->Controller->request = $this->Controller->request->withBody($stream);
- $this->RequestHandler->startup($event);
- $this->assertEquals(['invalid'], $this->Controller->request->getData());
- $stream = new Stream('php://memory', 'w');
- $stream->write('{"valid":true}');
- $this->Controller->request = $this->Controller->request->withBody($stream);
- $this->RequestHandler->startup($event);
- $this->assertEquals(['valid' => true], $this->Controller->request->getData());
- }
- /**
- * Test that file handles are ignored as XML data.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testStartupIgnoreFileAsXml()
- {
- $this->Controller->request = new ServerRequest([
- 'input' => '/dev/random',
- 'environment' => [
- 'REQUEST_METHOD' => 'POST',
- 'CONTENT_TYPE' => 'application/xml'
- ]
- ]);
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->startup($event);
- $this->assertEquals([], $this->Controller->request->getData());
- }
- /**
- * Test mapping a new type and having startup process it.
- *
- * @group deprecated
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testStartupCustomTypeProcess()
- {
- $this->deprecated(function () {
- $this->Controller->request = new ServerRequest([
- 'input' => '"A","csv","string"',
- 'environment' => [
- 'REQUEST_METHOD' => 'POST',
- 'CONTENT_TYPE' => 'text/csv'
- ]
- ]);
- $this->RequestHandler->addInputType('csv', ['str_getcsv']);
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->startup($event);
- $expected = [
- 'A', 'csv', 'string'
- ];
- $this->assertEquals($expected, $this->Controller->request->getData());
- });
- }
- /**
- * test beforeRedirect when disabled.
- *
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testBeforeRedirectDisabled()
- {
- static::setAppNamespace();
- Router::connect('/:controller/:action');
- $this->Controller->request = $this->Controller->request->withHeader('X-Requested-With', 'XMLHttpRequest');
- $event = new Event('Controller.startup', $this->Controller);
- $this->RequestHandler->initialize([]);
- $this->RequestHandler->setConfig('enableBeforeRedirect', false);
- $this->RequestHandler->startup($event);
- $this->assertNull($this->RequestHandler->beforeRedirect($event, '/posts/index', $this->Controller->response));
- }
- /**
- * testNonAjaxRedirect method
- *
- * @group deprecated
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testNonAjaxRedirect()
- {
- $this->deprecated(function () {
- $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.
- *
- * @group deprecated
- * @return void
- * @triggers Controller.startup $this->Controller
- */
- public function testAjaxRedirectWithNoUrl()
- {
- $this->deprecated(function () {
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $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->RequestHandler->renderAs($this->Controller, 'rss');
- $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->Controller->request = $this->request->withHeader('Accept', 'application/xml;q=1.0');
- $this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
- $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
- $this->assertEquals('application/xml', $this->Controller->response->getType());
- $this->assertEquals('UTF-8', $this->Controller->response->getCharset());
- $this->assertContains('myfile.xml', $this->Controller->response->getHeaderLine('Content-Disposition'));
- }
- /**
- * test that respondAs works as expected.
- *
- * @return void
- */
- public function testRespondAs()
- {
- $result = $this->RequestHandler->respondAs('json');
- $this->assertTrue($result);
- $this->assertEquals('application/json', $this->Controller->response->getType());
- $result = $this->RequestHandler->respondAs('text/xml');
- $this->assertTrue($result);
- $this->assertEquals('text/xml', $this->Controller->response->getType());
- }
- /**
- * test that attachment headers work with respondAs
- *
- * @return void
- */
- public function testRespondAsWithAttachment()
- {
- $result = $this->RequestHandler->respondAs('xml', ['attachment' => 'myfile.xml']);
- $this->assertTrue($result);
- $response = $this->Controller->response;
- $this->assertContains('myfile.xml', $response->getHeaderLine('Content-Disposition'));
- $this->assertContains('application/xml', $response->getType());
- }
- /**
- * test that calling renderAs() more than once continues to work.
- *
- * @link #6466
- * @return void
- */
- public function testRenderAsCalledTwice()
- {
- $this->Controller->getEventManager()->on('Controller.beforeRender', function (\Cake\Event\Event $e) {
- return $e->getSubject()->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->Controller->request = $this->request->withEnv('REQUEST_METHOD', 'GET');
- $this->assertNull($this->RequestHandler->requestedWith());
- $this->Controller->request = $this->request->withEnv('REQUEST_METHOD', 'POST')
- ->withEnv('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->Controller->request = $this->request
- ->withEnv('REQUEST_METHOD', 'PATCH')
- ->withEnv('CONTENT_TYPE', 'application/json');
- $this->assertEquals('json', $this->RequestHandler->requestedWith());
- $this->Controller->request = $this->request
- ->withEnv('REQUEST_METHOD', 'DELETE')
- ->withEnv('CONTENT_TYPE', 'application/json');
- $this->assertEquals('json', $this->RequestHandler->requestedWith());
- $this->Controller->request = $this->request
- ->withEnv('REQUEST_METHOD', 'POST')
- ->withEnv('CONTENT_TYPE', 'application/json');
- $result = $this->RequestHandler->requestedWith(['json', 'xml']);
- $this->assertEquals('json', $result);
- $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
- $this->assertFalse($result);
- $this->Controller->request = $this->request->withHeader(
- '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->Controller->request = $this->request->withHeader(
- '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->Controller->request = $this->request->withHeader(
- '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->Controller->request = $this->request->withHeader(
- '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->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['is'])
- ->getMock();
- $request->expects($this->once())->method('is')
- ->with('mobile')
- ->will($this->returnValue(true));
- $this->Controller->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->Controller->request = $this->request->withHeader(
- '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->Controller->request = $this->request->withHeader('Accept', '*/*;q=0.5');
- $this->assertFalse($this->RequestHandler->accepts('rss'));
- }
- /**
- * test accepts and prefers methods.
- *
- * @return void
- */
- public function testPrefers()
- {
- $this->Controller->request = $this->request->withHeader(
- '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->Controller->request = $this->request->withHeader(
- '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->Controller->request = $this->request->withHeader('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.
- *
- * @group deprecated
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestAction()
- {
- $this->deprecated(function () {
- static::setAppNamespace();
- Router::connect('/:controller/:action');
- $event = new Event('Controller.beforeRedirect', $this->Controller);
- $this->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->Controller->request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['is'])
- ->getMock();
- $this->Controller->response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['_sendHeader', 'stop'])
- ->getMock();
- $this->Controller->request->expects($this->any())
- ->method('is')
- ->will($this->returnValue(true));
- $response = $this->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
- *
- * @group deprecated
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestActionWithQueryString()
- {
- $this->deprecated(function () {
- static::setAppNamespace();
- Router::connect('/:controller/:action');
- $this->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->Controller->request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['is'])
- ->getMock();
- $this->Controller->response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['_sendHeader', 'stop'])
- ->getMock();
- $this->Controller->request->expects($this->any())
- ->method('is')
- ->with('ajax')
- ->will($this->returnValue(true));
- $event = new Event('Controller.beforeRedirect', $this->Controller);
- $response = $this->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->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
- *
- * @group deprecated
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestActionWithCookieData()
- {
- $this->deprecated(function () {
- static::setAppNamespace();
- Router::connect('/:controller/:action');
- $event = new Event('Controller.beforeRedirect', $this->Controller);
- $this->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->Controller->request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['is'])
- ->getMock();
- $this->Controller->response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['_sendHeader', 'stop'])
- ->getMock();
- $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
- $cookies = [
- 'foo' => 'bar'
- ];
- $this->Controller->request->cookies = $cookies;
- $response = $this->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.
- *
- * @group deprecated
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestActionStatusCode()
- {
- $this->deprecated(function () {
- static::setAppNamespace();
- Router::connect('/:controller/:action');
- $event = new Event('Controller.beforeRedirect', $this->Controller);
- $this->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->Controller->request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['is'])
- ->getMock();
- $this->Controller->response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['_sendHeader', 'stop'])
- ->getMock();
- $this->Controller->response->statusCode(302);
- $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
- $response = $this->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.
- *
- * @group deprecated
- * @return void
- * @triggers Controller.beforeRedirect $this->Controller
- */
- public function testAjaxRedirectAsRequestActionStillRenderingLayout()
- {
- $this->deprecated(function () {
- static::setAppNamespace();
- Router::connect('/:controller/:action');
- $event = new Event('Controller.beforeRedirect', $this->Controller);
- $this->RequestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->Controller->request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['is'])
- ->getMock();
- $this->Controller->response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['_sendHeader', 'stop'])
- ->getMock();
- $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
- $response = $this->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.
- *
- * @group deprecated
- * @return void
- * @triggers Controller.beforeRender $this->Controller
- */
- public function testBeforeRedirectCallbackWithArrayUrl()
- {
- $this->deprecated(function () {
- static::setAppNamespace();
- 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());
- $this->Controller->request = new ServerRequest('posts/index');
- 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
- *
- * @group deprecated
- * @return void
- */
- public function testAddInputTypeException()
- {
- $this->expectException(\Cake\Core\Exception\Exception::class);
- $this->deprecated(function () {
- $this->RequestHandler->addInputType('csv', ['I am not callable']);
- });
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- * @triggers Controller.beforeRender $this->Controller
- */
- public function testCheckNotModifiedByEtagStar()
- {
- $response = new Response();
- $response = $response->withEtag('something')
- ->withHeader('Content-Type', 'text/plain')
- ->withStringBody('keeper');
- $this->Controller->response = $response;
- $this->Controller->request = $this->request->withHeader('If-None-Match', '*');
- $event = new Event('Controller.beforeRender', $this->Controller);
- $requestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->assertFalse($requestHandler->beforeRender($event));
- $this->assertEquals(304, $this->Controller->response->getStatusCode());
- $this->assertEquals('', (string)$this->Controller->response->getBody());
- $this->assertFalse($this->Controller->response->hasHeader('Content-Type'), 'header should not be removed.');
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- * @triggers Controller.beforeRender
- */
- public function testCheckNotModifiedByEtagExact()
- {
- $response = new Response();
- $response = $response->withEtag('something', true)
- ->withHeader('Content-Type', 'text/plain')
- ->withStringBody('keeper');
- $this->Controller->response = $response;
- $this->Controller->request = $this->request->withHeader('If-None-Match', 'W/"something", "other"');
- $event = new Event('Controller.beforeRender', $this->Controller);
- $requestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->assertFalse($requestHandler->beforeRender($event));
- $this->assertEquals(304, $this->Controller->response->getStatusCode());
- $this->assertEquals('', (string)$this->Controller->response->getBody());
- $this->assertFalse($this->Controller->response->hasHeader('Content-Type'));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- * @triggers Controller.beforeRender $this->Controller
- */
- public function testCheckNotModifiedByEtagAndTime()
- {
- $this->Controller->request = $this->request
- ->withHeader('If-None-Match', 'W/"something", "other"')
- ->withHeader('If-Modified-Since', '2012-01-01 00:00:00');
- $response = new Response();
- $response = $response->withEtag('something', true)
- ->withHeader('Content-type', 'text/plain')
- ->withStringBody('should be removed')
- ->withModified('2012-01-01 00:00:00');
- $this->Controller->response = $response;
- $event = new Event('Controller.beforeRender', $this->Controller);
- $requestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->assertFalse($requestHandler->beforeRender($event));
- $this->assertEquals(304, $this->Controller->response->getStatusCode());
- $this->assertEquals('', (string)$this->Controller->response->getBody());
- $this->assertFalse($this->Controller->response->hasHeader('Content-type'));
- }
- /**
- * Test checkNotModified method
- *
- * @return void
- * @triggers Controller.beforeRender $this->Controller
- */
- public function testCheckNotModifiedNoInfo()
- {
- $response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['notModified', 'stop'])
- ->getMock();
- $response->expects($this->never())->method('notModified');
- $this->Controller->response = $response;
- $event = new Event('Controller.beforeRender', $this->Controller);
- $requestHandler = new RequestHandlerComponent($this->Controller->components());
- $this->assertNull($requestHandler->beforeRender($event));
- }
- /**
- * Test default options in construction
- *
- * @return void
- */
- public function testConstructDefaultOptions()
- {
- $requestHandler = new RequestHandlerComponent($this->Controller->components());
- $viewClass = $requestHandler->getConfig('viewClassMap');
- $expected = [
- 'json' => 'Json',
- 'xml' => 'Xml',
- 'ajax' => 'Ajax',
- ];
- $this->assertEquals($expected, $viewClass);
- $inputs = $requestHandler->getConfig('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->getConfig('viewClassMap');
- $expected = [
- 'json' => 'Json',
- ];
- $this->assertEquals($expected, $viewClass);
- $inputs = $requestHandler->getConfig('inputTypeMap');
- $this->assertArrayHasKey('json', $inputs);
- $this->assertCount(1, $inputs);
- }
- /**
- * test beforeRender() doesn't override response type set in controller action
- *
- * @return void
- */
- public function testBeforeRender()
- {
- $this->Controller->set_response_type();
- $event = new Event('Controller.beforeRender', $this->Controller);
- $this->RequestHandler->beforeRender($event);
- $this->assertEquals('text/plain', $this->Controller->response->getType());
- }
- }
|