| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107 |
- <?php
- /**
- * CakeRequest Test case file.
- *
- * PHP 5
- *
- * 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
- * @package Cake.Test.Case.Network
- * @since CakePHP(tm) v 2.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('Dispatcher', 'Routing');
- App::uses('Xml', 'Utility');
- App::uses('CakeRequest', 'Network');
- class TestCakeRequest extends CakeRequest {
- public function reConstruct($url = 'some/path', $parseEnvironment = true) {
- $this->_base();
- if (empty($url)) {
- $url = $this->_url();
- }
- if ($url[0] === '/') {
- $url = substr($url, 1);
- }
- $this->url = $url;
- if ($parseEnvironment) {
- $this->_processPost();
- $this->_processGet();
- $this->_processFiles();
- }
- $this->here = $this->base . '/' . $this->url;
- }
- }
- class CakeRequestTest extends CakeTestCase {
- /**
- * setup callback
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->_app = Configure::read('App');
- $this->_case = null;
- if (isset($_GET['case'])) {
- $this->_case = $_GET['case'];
- unset($_GET['case']);
- }
- Configure::write('App.baseUrl', false);
- }
- /**
- * tearDown
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- if (!empty($this->_case)) {
- $_GET['case'] = $this->_case;
- }
- Configure::write('App', $this->_app);
- }
- /**
- * test that the autoparse = false constructor works.
- *
- * @return void
- */
- public function testNoAutoParseConstruction() {
- $_GET = array(
- 'one' => 'param'
- );
- $request = new CakeRequest(null, false);
- $this->assertFalse(isset($request->query['one']));
- }
- /**
- * test construction
- *
- * @return void
- */
- public function testConstructionGetParsing() {
- $_GET = array(
- 'one' => 'param',
- 'two' => 'banana'
- );
- $request = new CakeRequest('some/path');
- $this->assertEquals($request->query, $_GET);
- $_GET = array(
- 'one' => 'param',
- 'two' => 'banana',
- );
- $request = new CakeRequest('some/path');
- $this->assertEquals($request->query, $_GET);
- $this->assertEquals('some/path', $request->url);
- }
- /**
- * Test that querystring args provided in the url string are parsed.
- *
- * @return void
- */
- public function testQueryStringParsingFromInputUrl() {
- $_GET = array();
- $request = new CakeRequest('some/path?one=something&two=else');
- $expected = array('one' => 'something', 'two' => 'else');
- $this->assertEquals($expected, $request->query);
- $this->assertEquals('some/path?one=something&two=else', $request->url);
- }
- /**
- * Test that named arguments + querystrings are handled correctly.
- *
- * @return void
- */
- public function testQueryStringAndNamedParams() {
- $_SERVER['REQUEST_URI'] = '/tasks/index/page:1?ts=123456';
- $request = new CakeRequest();
- $this->assertEquals('tasks/index/page:1', $request->url);
- $_SERVER['REQUEST_URI'] = '/tasks/index/page:1/?ts=123456';
- $request = new CakeRequest();
- $this->assertEquals('tasks/index/page:1/', $request->url);
- $_SERVER['REQUEST_URI'] = '/some/path?url=http://cakephp.org';
- $request = new CakeRequest();
- $this->assertEquals('some/path', $request->url);
- $_SERVER['REQUEST_URI'] = FULL_BASE_URL . '/other/path?url=http://cakephp.org';
- $request = new CakeRequest();
- $this->assertEquals('other/path', $request->url);
- }
- /**
- * test addParams() method
- *
- * @return void
- */
- public function testAddParams() {
- $request = new CakeRequest('some/path');
- $request->params = array('controller' => 'posts', 'action' => 'view');
- $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
- $this->assertSame($result, $request, 'Method did not return itself. %s');
- $this->assertEquals('posts', $request->controller);
- $this->assertEquals('index', $request->action);
- $this->assertEquals(null, $request->plugin);
- }
- /**
- * test splicing in paths.
- *
- * @return void
- */
- public function testAddPaths() {
- $request = new CakeRequest('some/path');
- $request->webroot = '/some/path/going/here/';
- $result = $request->addPaths(array(
- 'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
- ));
- $this->assertSame($result, $request, 'Method did not return itself. %s');
- $this->assertEquals('/', $request->webroot);
- $this->assertEquals('/base_dir', $request->base);
- $this->assertEquals('/', $request->here);
- $this->assertFalse(isset($request->random));
- }
- /**
- * test parsing POST data into the object.
- *
- * @return void
- */
- public function testPostParsing() {
- $_POST = array('data' => array(
- 'Article' => array('title')
- ));
- $request = new CakeRequest('some/path');
- $this->assertEquals($_POST['data'], $request->data);
- $_POST = array('one' => 1, 'two' => 'three');
- $request = new CakeRequest('some/path');
- $this->assertEquals($_POST, $request->data);
- $_POST = array(
- 'data' => array(
- 'Article' => array('title' => 'Testing'),
- ),
- 'action' => 'update'
- );
- $request = new CakeRequest('some/path');
- $expected = array(
- 'Article' => array('title' => 'Testing'),
- 'action' => 'update'
- );
- $this->assertEquals($expected, $request->data);
- $_POST = array('data' => array(
- 'Article' => array('title'),
- 'Tag' => array('Tag' => array(1, 2))
- ));
- $request = new CakeRequest('some/path');
- $this->assertEquals($_POST['data'], $request->data);
- $_POST = array('data' => array(
- 'Article' => array('title' => 'some title'),
- 'Tag' => array('Tag' => array(1, 2))
- ));
- $request = new CakeRequest('some/path');
- $this->assertEquals($_POST['data'], $request->data);
- $_POST = array(
- 'a' => array(1, 2),
- 'b' => array(1, 2)
- );
- $request = new CakeRequest('some/path');
- $this->assertEquals($_POST, $request->data);
- }
- /**
- * test parsing PUT data into the object.
- *
- * @return void
- */
- public function testPutParsing() {
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=UTF-8';
- $data = array('data' => array(
- 'Article' => array('title')
- ));
- $request = $this->getMock('TestCakeRequest', array('_readInput'));
- $request->expects($this->at(0))->method('_readInput')
- ->will($this->returnValue('data[Article][]=title'));
- $request->reConstruct();
- $this->assertEquals($data['data'], $request->data);
- $data = array('one' => 1, 'two' => 'three');
- $request = $this->getMock('TestCakeRequest', array('_readInput'));
- $request->expects($this->at(0))->method('_readInput')
- ->will($this->returnValue('one=1&two=three'));
- $request->reConstruct();
- $this->assertEquals($data, $request->data);
- $data = array(
- 'data' => array(
- 'Article' => array('title' => 'Testing'),
- ),
- 'action' => 'update'
- );
- $request = $this->getMock('TestCakeRequest', array('_readInput'));
- $request->expects($this->at(0))->method('_readInput')
- ->will($this->returnValue('data[Article][title]=Testing&action=update'));
- $request->reConstruct();
- $expected = array(
- 'Article' => array('title' => 'Testing'),
- 'action' => 'update'
- );
- $this->assertEquals($expected, $request->data);
- $_SERVER['REQUEST_METHOD'] = 'DELETE';
- $data = array('data' => array(
- 'Article' => array('title'),
- 'Tag' => array('Tag' => array(1, 2))
- ));
- $request = $this->getMock('TestCakeRequest', array('_readInput'));
- $request->expects($this->at(0))->method('_readInput')
- ->will($this->returnValue('data[Article][]=title&Tag[Tag][]=1&Tag[Tag][]=2'));
- $request->reConstruct();
- $this->assertEquals($data['data'], $request->data);
- $data = array('data' => array(
- 'Article' => array('title' => 'some title'),
- 'Tag' => array('Tag' => array(1, 2))
- ));
- $request = $this->getMock('TestCakeRequest', array('_readInput'));
- $request->expects($this->at(0))->method('_readInput')
- ->will($this->returnValue('data[Article][title]=some%20title&Tag[Tag][]=1&Tag[Tag][]=2'));
- $request->reConstruct();
- $this->assertEquals($data['data'], $request->data);
- $data = array(
- 'a' => array(1, 2),
- 'b' => array(1, 2)
- );
- $request = $this->getMock('TestCakeRequest', array('_readInput'));
- $request->expects($this->at(0))->method('_readInput')
- ->will($this->returnValue('a[]=1&a[]=2&b[]=1&b[]=2'));
- $request->reConstruct();
- $this->assertEquals($data, $request->data);
- }
- /**
- * test parsing json PUT data into the object.
- *
- * @return void
- */
- public function testPutParsingJSON() {
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $_SERVER['CONTENT_TYPE'] = 'application/json';
- $request = $this->getMock('TestCakeRequest', array('_readInput'));
- $request->expects($this->at(0))->method('_readInput')
- ->will($this->returnValue('{"Article":["title"]}'));
- $request->reConstruct();
- $result = $request->input('json_decode', true);
- $this->assertEquals(array('title'), $result['Article']);
- }
- /**
- * test parsing of FILES array
- *
- * @return void
- */
- public function testFilesParsing() {
- $_FILES = array(
- 'data' => array(
- 'name' => array(
- 'File' => array(
- array('data' => 'cake_sqlserver_patch.patch'),
- array('data' => 'controller.diff'),
- array('data' => ''),
- array('data' => ''),
- ),
- 'Post' => array('attachment' => 'jquery-1.2.1.js'),
- ),
- 'type' => array(
- 'File' => array(
- array('data' => ''),
- array('data' => ''),
- array('data' => ''),
- array('data' => ''),
- ),
- 'Post' => array('attachment' => 'application/x-javascript'),
- ),
- 'tmp_name' => array(
- 'File' => array(
- array('data' => '/private/var/tmp/phpy05Ywj'),
- array('data' => '/private/var/tmp/php7MBztY'),
- array('data' => ''),
- array('data' => ''),
- ),
- 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
- ),
- 'error' => array(
- 'File' => array(
- array('data' => 0),
- array('data' => 0),
- array('data' => 4),
- array('data' => 4)
- ),
- 'Post' => array('attachment' => 0)
- ),
- 'size' => array(
- 'File' => array(
- array('data' => 6271),
- array('data' => 350),
- array('data' => 0),
- array('data' => 0),
- ),
- 'Post' => array('attachment' => 80469)
- ),
- )
- );
- $request = new CakeRequest('some/path');
- $expected = array(
- 'File' => array(
- array(
- 'data' => array(
- 'name' => 'cake_sqlserver_patch.patch',
- 'type' => '',
- 'tmp_name' => '/private/var/tmp/phpy05Ywj',
- 'error' => 0,
- 'size' => 6271,
- )
- ),
- array(
- 'data' => array(
- 'name' => 'controller.diff',
- 'type' => '',
- 'tmp_name' => '/private/var/tmp/php7MBztY',
- 'error' => 0,
- 'size' => 350,
- )
- ),
- array(
- 'data' => array(
- 'name' => '',
- 'type' => '',
- 'tmp_name' => '',
- 'error' => 4,
- 'size' => 0,
- )
- ),
- array(
- 'data' => array(
- 'name' => '',
- 'type' => '',
- 'tmp_name' => '',
- 'error' => 4,
- 'size' => 0,
- )
- ),
- ),
- 'Post' => array(
- 'attachment' => array(
- 'name' => 'jquery-1.2.1.js',
- 'type' => 'application/x-javascript',
- 'tmp_name' => '/private/var/tmp/phpEwlrIo',
- 'error' => 0,
- 'size' => 80469,
- )
- )
- );
- $this->assertEquals($expected, $request->data);
- $_FILES = array(
- 'data' => array(
- 'name' => array(
- 'Document' => array(
- 1 => array(
- 'birth_cert' => 'born on.txt',
- 'passport' => 'passport.txt',
- 'drivers_license' => 'ugly pic.jpg'
- ),
- 2 => array(
- 'birth_cert' => 'aunt betty.txt',
- 'passport' => 'betty-passport.txt',
- 'drivers_license' => 'betty-photo.jpg'
- ),
- ),
- ),
- 'type' => array(
- 'Document' => array(
- 1 => array(
- 'birth_cert' => 'application/octet-stream',
- 'passport' => 'application/octet-stream',
- 'drivers_license' => 'application/octet-stream',
- ),
- 2 => array(
- 'birth_cert' => 'application/octet-stream',
- 'passport' => 'application/octet-stream',
- 'drivers_license' => 'application/octet-stream',
- )
- )
- ),
- 'tmp_name' => array(
- 'Document' => array(
- 1 => array(
- 'birth_cert' => '/private/var/tmp/phpbsUWfH',
- 'passport' => '/private/var/tmp/php7f5zLt',
- 'drivers_license' => '/private/var/tmp/phpMXpZgT',
- ),
- 2 => array(
- 'birth_cert' => '/private/var/tmp/php5kHZt0',
- 'passport' => '/private/var/tmp/phpnYkOuM',
- 'drivers_license' => '/private/var/tmp/php9Rq0P3',
- )
- )
- ),
- 'error' => array(
- 'Document' => array(
- 1 => array(
- 'birth_cert' => 0,
- 'passport' => 0,
- 'drivers_license' => 0,
- ),
- 2 => array(
- 'birth_cert' => 0,
- 'passport' => 0,
- 'drivers_license' => 0,
- )
- )
- ),
- 'size' => array(
- 'Document' => array(
- 1 => array(
- 'birth_cert' => 123,
- 'passport' => 458,
- 'drivers_license' => 875,
- ),
- 2 => array(
- 'birth_cert' => 876,
- 'passport' => 976,
- 'drivers_license' => 9783,
- )
- )
- )
- )
- );
- $request = new CakeRequest('some/path');
- $expected = array(
- 'Document' => array(
- 1 => array(
- 'birth_cert' => array(
- 'name' => 'born on.txt',
- 'tmp_name' => '/private/var/tmp/phpbsUWfH',
- 'error' => 0,
- 'size' => 123,
- 'type' => 'application/octet-stream',
- ),
- 'passport' => array(
- 'name' => 'passport.txt',
- 'tmp_name' => '/private/var/tmp/php7f5zLt',
- 'error' => 0,
- 'size' => 458,
- 'type' => 'application/octet-stream',
- ),
- 'drivers_license' => array(
- 'name' => 'ugly pic.jpg',
- 'tmp_name' => '/private/var/tmp/phpMXpZgT',
- 'error' => 0,
- 'size' => 875,
- 'type' => 'application/octet-stream',
- ),
- ),
- 2 => array(
- 'birth_cert' => array(
- 'name' => 'aunt betty.txt',
- 'tmp_name' => '/private/var/tmp/php5kHZt0',
- 'error' => 0,
- 'size' => 876,
- 'type' => 'application/octet-stream',
- ),
- 'passport' => array(
- 'name' => 'betty-passport.txt',
- 'tmp_name' => '/private/var/tmp/phpnYkOuM',
- 'error' => 0,
- 'size' => 976,
- 'type' => 'application/octet-stream',
- ),
- 'drivers_license' => array(
- 'name' => 'betty-photo.jpg',
- 'tmp_name' => '/private/var/tmp/php9Rq0P3',
- 'error' => 0,
- 'size' => 9783,
- 'type' => 'application/octet-stream',
- ),
- ),
- )
- );
- $this->assertEquals($expected, $request->data);
- $_FILES = array(
- 'data' => array(
- 'name' => array('birth_cert' => 'born on.txt'),
- 'type' => array('birth_cert' => 'application/octet-stream'),
- 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
- 'error' => array('birth_cert' => 0),
- 'size' => array('birth_cert' => 123)
- )
- );
- $request = new CakeRequest('some/path');
- $expected = array(
- 'birth_cert' => array(
- 'name' => 'born on.txt',
- 'type' => 'application/octet-stream',
- 'tmp_name' => '/private/var/tmp/phpbsUWfH',
- 'error' => 0,
- 'size' => 123
- )
- );
- $this->assertEquals($expected, $request->data);
- $_FILES = array(
- 'something' => array(
- 'name' => 'something.txt',
- 'type' => 'text/plain',
- 'tmp_name' => '/some/file',
- 'error' => 0,
- 'size' => 123
- )
- );
- $request = new CakeRequest('some/path');
- $this->assertEquals($request->params['form'], $_FILES);
- }
- /**
- * Test that files in the 0th index work.
- */
- public function testFilesZeroithIndex() {
- $_FILES = array(
- 0 => array(
- 'name' => 'cake_sqlserver_patch.patch',
- 'type' => 'text/plain',
- 'tmp_name' => '/private/var/tmp/phpy05Ywj',
- 'error' => 0,
- 'size' => 6271,
- ),
- );
- $request = new CakeRequest('some/path');
- $this->assertEquals($_FILES, $request->params['form']);
- }
- /**
- * test method overrides coming in from POST data.
- *
- * @return void
- */
- public function testMethodOverrides() {
- $_POST = array('_method' => 'POST');
- $request = new CakeRequest('some/path');
- $this->assertEquals(env('REQUEST_METHOD'), 'POST');
- $_POST = array('_method' => 'DELETE');
- $request = new CakeRequest('some/path');
- $this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
- $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
- $request = new CakeRequest('some/path');
- $this->assertEquals(env('REQUEST_METHOD'), 'PUT');
- }
- /**
- * test the clientIp method.
- *
- * @return void
- */
- public function testclientIp() {
- $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
- $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
- $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
- $request = new CakeRequest('some/path');
- $this->assertEquals('192.168.1.5', $request->clientIp(false));
- $this->assertEquals('192.168.1.2', $request->clientIp());
- unset($_SERVER['HTTP_X_FORWARDED_FOR']);
- $this->assertEquals('192.168.1.2', $request->clientIp());
- unset($_SERVER['HTTP_CLIENT_IP']);
- $this->assertEquals('192.168.1.3', $request->clientIp());
- $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
- $this->assertEquals('10.0.1.2', $request->clientIp());
- }
- /**
- * test the referer function.
- *
- * @return void
- */
- public function testReferer() {
- $request = new CakeRequest('some/path');
- $request->webroot = '/';
- $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
- $result = $request->referer();
- $this->assertSame($result, 'http://cakephp.org');
- $_SERVER['HTTP_REFERER'] = '';
- $result = $request->referer();
- $this->assertSame($result, '/');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
- $result = $request->referer(true);
- $this->assertSame($result, '/some/path');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
- $result = $request->referer(false);
- $this->assertSame($result, FULL_BASE_URL . '/some/path');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
- $result = $request->referer(true);
- $this->assertSame($result, '/some/path');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/recipes/add';
- $result = $request->referer(true);
- $this->assertSame($result, '/recipes/add');
- $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
- $result = $request->referer();
- $this->assertSame($result, 'cakephp.org');
- }
- /**
- * test the simple uses of is()
- *
- * @return void
- */
- public function testIsHttpMethods() {
- $request = new CakeRequest('some/path');
- $this->assertFalse($request->is('undefined-behavior'));
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $this->assertTrue($request->is('get'));
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $this->assertTrue($request->is('POST'));
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $this->assertTrue($request->is('put'));
- $this->assertFalse($request->is('get'));
- $_SERVER['REQUEST_METHOD'] = 'DELETE';
- $this->assertTrue($request->is('delete'));
- $this->assertTrue($request->isDelete());
- $_SERVER['REQUEST_METHOD'] = 'delete';
- $this->assertFalse($request->is('delete'));
- }
- /**
- * Test is() with multiple types.
- *
- * @return void
- */
- public function testIsMultiple() {
- $request = new CakeRequest('some/path');
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $this->assertTrue($request->is(array('get', 'post')));
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $this->assertTrue($request->is(array('get', 'post')));
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $this->assertFalse($request->is(array('get', 'post')));
- }
- /**
- * Test isAll()
- *
- * @return void
- */
- public function testIsAll() {
- $request = new CakeRequest('some/path');
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'GET';
- $this->assertTrue($request->isAll(array('ajax', 'get')));
- $this->assertFalse($request->isAll(array('post', 'get')));
- $this->assertFalse($request->isAll(array('ajax', 'post')));
- }
- /**
- * test the method() method.
- *
- * @return void
- */
- public function testMethod() {
- $_SERVER['REQUEST_METHOD'] = 'delete';
- $request = new CakeRequest('some/path');
- $this->assertEquals('delete', $request->method());
- }
- /**
- * test host retrieval.
- *
- * @return void
- */
- public function testHost() {
- $_SERVER['HTTP_HOST'] = 'localhost';
- $request = new CakeRequest('some/path');
- $this->assertEquals('localhost', $request->host());
- }
- /**
- * test domain retrieval.
- *
- * @return void
- */
- public function testDomain() {
- $_SERVER['HTTP_HOST'] = 'something.example.com';
- $request = new CakeRequest('some/path');
- $this->assertEquals('example.com', $request->domain());
- $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
- $this->assertEquals('example.co.uk', $request->domain(2));
- }
- /**
- * test getting subdomains for a host.
- *
- * @return void
- */
- public function testSubdomain() {
- $_SERVER['HTTP_HOST'] = 'something.example.com';
- $request = new CakeRequest('some/path');
- $this->assertEquals(array('something'), $request->subdomains());
- $_SERVER['HTTP_HOST'] = 'www.something.example.com';
- $this->assertEquals(array('www', 'something'), $request->subdomains());
- $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
- $this->assertEquals(array('www', 'something'), $request->subdomains(2));
- $_SERVER['HTTP_HOST'] = 'example.co.uk';
- $this->assertEquals(array(), $request->subdomains(2));
- }
- /**
- * test ajax, flash and friends
- *
- * @return void
- */
- public function testisAjaxFlashAndFriends() {
- $request = new CakeRequest('some/path');
- $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
- $this->assertTrue($request->is('flash'));
- $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
- $this->assertTrue($request->is('flash'));
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $this->assertTrue($request->is('ajax'));
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
- $this->assertFalse($request->is('ajax'));
- $this->assertFalse($request->isAjax());
- $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
- $this->assertTrue($request->is('mobile'));
- $this->assertTrue($request->isMobile());
- $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
- $this->assertTrue($request->is('mobile'));
- $this->assertTrue($request->isMobile());
- $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
- $this->assertTrue($request->is('mobile'));
- $this->assertTrue($request->isMobile());
- }
- /**
- * test __call expcetions
- *
- * @expectedException CakeException
- * @return void
- */
- public function testMagicCallExceptionOnUnknownMethod() {
- $request = new CakeRequest('some/path');
- $request->IamABanana();
- }
- /**
- * test is(ssl)
- *
- * @return void
- */
- public function testIsSsl() {
- $request = new CakeRequest('some/path');
- $_SERVER['HTTPS'] = 1;
- $this->assertTrue($request->is('ssl'));
- $_SERVER['HTTPS'] = 'on';
- $this->assertTrue($request->is('ssl'));
- $_SERVER['HTTPS'] = '1';
- $this->assertTrue($request->is('ssl'));
- $_SERVER['HTTPS'] = 'I am not empty';
- $this->assertTrue($request->is('ssl'));
- $_SERVER['HTTPS'] = 1;
- $this->assertTrue($request->is('ssl'));
- $_SERVER['HTTPS'] = 'off';
- $this->assertFalse($request->is('ssl'));
- $_SERVER['HTTPS'] = false;
- $this->assertFalse($request->is('ssl'));
- $_SERVER['HTTPS'] = '';
- $this->assertFalse($request->is('ssl'));
- }
- /**
- * test getting request params with object properties.
- *
- * @return void
- */
- public function testMagicget() {
- $request = new CakeRequest('some/path');
- $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
- $this->assertEquals('posts', $request->controller);
- $this->assertEquals('view', $request->action);
- $this->assertEquals('blogs', $request->plugin);
- $this->assertSame($request->banana, null);
- }
- /**
- * Test isset()/empty() with overloaded properties.
- *
- * @return void
- */
- public function testMagicisset() {
- $request = new CakeRequest('some/path');
- $request->params = array(
- 'controller' => 'posts',
- 'action' => 'view',
- 'plugin' => 'blogs',
- 'named' => array()
- );
- $this->assertTrue(isset($request->controller));
- $this->assertFalse(isset($request->notthere));
- $this->assertFalse(empty($request->controller));
- $this->assertTrue(empty($request->named));
- }
- /**
- * test the array access implementation
- *
- * @return void
- */
- public function testArrayAccess() {
- $request = new CakeRequest('some/path');
- $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
- $this->assertEquals('posts', $request['controller']);
- $request['slug'] = 'speedy-slug';
- $this->assertEquals('speedy-slug', $request->slug);
- $this->assertEquals('speedy-slug', $request['slug']);
- $this->assertTrue(isset($request['action']));
- $this->assertFalse(isset($request['wrong-param']));
- $this->assertTrue(isset($request['plugin']));
- unset($request['plugin']);
- $this->assertFalse(isset($request['plugin']));
- $this->assertNull($request['plugin']);
- $this->assertNull($request->plugin);
- $request = new CakeRequest('some/path?one=something&two=else');
- $this->assertTrue(isset($request['url']['one']));
- $request->data = array('Post' => array('title' => 'something'));
- $this->assertEquals('something', $request['data']['Post']['title']);
- }
- /**
- * test adding detectors and having them work.
- *
- * @return void
- */
- public function testAddDetector() {
- $request = new CakeRequest('some/path');
- $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
- $_SERVER['TEST_VAR'] = 'something';
- $this->assertTrue($request->is('compare'), 'Value match failed.');
- $_SERVER['TEST_VAR'] = 'wrong';
- $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
- $request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
- $_SERVER['TEST_VAR'] = 'foo';
- $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
- $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
- $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
- $_SERVER['TEST_VAR'] = 'not foo';
- $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
- $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
- $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
- $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
- $_SERVER['TEST_VAR'] = 'banana';
- $this->assertTrue($request->isBanana());
- $_SERVER['TEST_VAR'] = 'wrong value';
- $this->assertFalse($request->isBanana());
- $request->addDetector('mobile', array('options' => array('Imagination')));
- $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
- $this->assertTrue($request->isMobile());
- $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
- $this->assertTrue($request->isMobile());
- $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
- $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
- $request->params['action'] = 'index';
- $this->assertTrue($request->isIndex());
- $request->params['action'] = 'add';
- $this->assertFalse($request->isIndex());
- $request->return = true;
- $this->assertTrue($request->isCallMe());
- $request->return = false;
- $this->assertFalse($request->isCallMe());
- }
- /**
- * helper function for testing callbacks.
- *
- * @return void
- */
- public function detectCallback($request) {
- return (bool)$request->return;
- }
- /**
- * test getting headers
- *
- * @return void
- */
- public function testHeader() {
- $_SERVER['HTTP_HOST'] = 'localhost';
- $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-ca) AppleWebKit/534.8+ (KHTML, like Gecko) Version/5.0 Safari/533.16';
- $request = new CakeRequest('/', false);
- $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
- $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
- }
- /**
- * test accepts() with and without parameters
- *
- * @return void
- */
- public function testAccepts() {
- $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
- $request = new CakeRequest('/', false);
- $result = $request->accepts();
- $expected = array(
- 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
- );
- $this->assertEquals($expected, $result, 'Content types differ.');
- $result = $request->accepts('text/html');
- $this->assertTrue($result);
- $result = $request->accepts('image/gif');
- $this->assertFalse($result);
- }
- /**
- * Test that accept header types are trimmed for comparisons.
- *
- * @return void
- */
- public function testAcceptWithWhitespace() {
- $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
- $request = new CakeRequest('/', false);
- $result = $request->accepts();
- $expected = array(
- 'text/xml', 'text/html', 'text/plain', 'image/png'
- );
- $this->assertEquals($expected, $result, 'Content types differ.');
- $this->assertTrue($request->accepts('text/html'));
- }
- /**
- * Content types from accepts() should respect the client's q preference values.
- *
- * @return void
- */
- public function testAcceptWithQvalueSorting() {
- $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
- $request = new CakeRequest('/', false);
- $result = $request->accepts();
- $expected = array('application/xml', 'text/html', 'application/json');
- $this->assertEquals($expected, $result);
- }
- /**
- * Test the raw parsing of accept headers into the q value formatting.
- *
- * @return void
- */
- public function testParseAcceptWithQValue() {
- $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
- $request = new CakeRequest('/', false);
- $result = $request->parseAccept();
- $expected = array(
- '1.0' => array('application/xml', 'image/png'),
- '0.8' => array('text/html'),
- '0.7' => array('application/json'),
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Test parsing accept with a confusing accept value.
- *
- * @return void
- */
- public function testParseAcceptNoQValues() {
- $_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
- $request = new CakeRequest('/', false);
- $result = $request->parseAccept();
- $expected = array(
- '1.0' => array('application/json', 'text/plain', '*/*'),
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testBaseUrlAndWebrootWithModRewrite method
- *
- * @return void
- */
- public function testBaseUrlAndWebrootWithModRewrite() {
- Configure::write('App.baseUrl', false);
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
- $_SERVER['PHP_SELF'] = '/urlencode me/app/webroot/index.php';
- $_SERVER['PATH_INFO'] = '/posts/view/1';
- $request = new CakeRequest();
- $this->assertEquals('/urlencode%20me', $request->base);
- $this->assertEquals('/urlencode%20me/', $request->webroot);
- $this->assertEquals('posts/view/1', $request->url);
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
- $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
- $_SERVER['PATH_INFO'] = '/posts/view/1';
- $request = new CakeRequest();
- $this->assertEquals('/1.2.x.x', $request->base);
- $this->assertEquals('/1.2.x.x/', $request->webroot);
- $this->assertEquals('posts/view/1', $request->url);
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
- $_SERVER['PHP_SELF'] = '/index.php';
- $_SERVER['PATH_INFO'] = '/posts/add';
- $request = new CakeRequest();
- $this->assertEquals('', $request->base);
- $this->assertEquals('/', $request->webroot);
- $this->assertEquals('posts/add', $request->url);
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
- $_SERVER['PHP_SELF'] = '/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEquals('', $request->base);
- $this->assertEquals('/', $request->webroot);
- $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
- $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEquals('', $request->base);
- $this->assertEquals('/', $request->webroot);
- Configure::write('App.dir', 'auth');
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
- $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/demos/auth', $request->base);
- $this->assertEquals('/demos/auth/', $request->webroot);
- Configure::write('App.dir', 'code');
- $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
- $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/clients/PewterReport/code', $request->base);
- $this->assertEquals('/clients/PewterReport/code/', $request->webroot);
- }
- /**
- * testBaseUrlwithModRewriteAlias method
- *
- * @return void
- */
- public function testBaseUrlwithModRewriteAlias() {
- $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
- $_SERVER['PHP_SELF'] = '/control/index.php';
- Configure::write('App.base', '/control');
- $request = new CakeRequest();
- $this->assertEquals('/control', $request->base);
- $this->assertEquals('/control/', $request->webroot);
- Configure::write('App.base', false);
- Configure::write('App.dir', 'affiliate');
- Configure::write('App.webroot', 'newaffiliate');
- $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
- $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/newaffiliate', $request->base);
- $this->assertEquals('/newaffiliate/', $request->webroot);
- }
- /**
- * test base, webroot, and url parsing when there is no url rewriting
- *
- * @return void
- */
- public function testBaseUrlWithNoModRewrite() {
- $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
- $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
- $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
- $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
- Configure::write('App', array(
- 'dir' => APP_DIR,
- 'webroot' => WEBROOT_DIR,
- 'base' => false,
- 'baseUrl' => '/cake/index.php'
- ));
- $request = new CakeRequest();
- $this->assertEquals('/cake/index.php', $request->base);
- $this->assertEquals('/cake/app/webroot/', $request->webroot);
- $this->assertEquals('posts/index', $request->url);
- }
- /**
- * testBaseUrlAndWebrootWithBaseUrl method
- *
- * @return void
- */
- public function testBaseUrlAndWebrootWithBaseUrl() {
- Configure::write('App.dir', 'app');
- Configure::write('App.baseUrl', '/app/webroot/index.php');
- $request = new CakeRequest();
- $this->assertEquals('/app/webroot/index.php', $request->base);
- $this->assertEquals('/app/webroot/', $request->webroot);
- Configure::write('App.baseUrl', '/app/webroot/test.php');
- $request = new CakeRequest();
- $this->assertEquals('/app/webroot/test.php', $request->base);
- $this->assertEquals('/app/webroot/', $request->webroot);
- Configure::write('App.baseUrl', '/app/index.php');
- $request = new CakeRequest();
- $this->assertEquals('/app/index.php', $request->base);
- $this->assertEquals('/app/webroot/', $request->webroot);
- Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
- $request = new CakeRequest();
- $this->assertEquals('/CakeBB/app/webroot/index.php', $request->base);
- $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
- Configure::write('App.baseUrl', '/CakeBB/app/index.php');
- $request = new CakeRequest();
- $this->assertEquals('/CakeBB/app/index.php', $request->base);
- $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
- Configure::write('App.baseUrl', '/CakeBB/index.php');
- $request = new CakeRequest();
- $this->assertEquals('/CakeBB/index.php', $request->base);
- $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
- Configure::write('App.baseUrl', '/dbhauser/index.php');
- $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
- $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/dbhauser/index.php', $request->base);
- $this->assertEquals('/dbhauser/app/webroot/', $request->webroot);
- }
- /**
- * test baseUrl with no rewrite and using the top level index.php.
- *
- * @return void
- */
- public function testBaseUrlNoRewriteTopLevelIndex() {
- Configure::write('App.baseUrl', '/index.php');
- $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
- $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/index.php', $request->base);
- $this->assertEquals('/app/webroot/', $request->webroot);
- }
- /**
- * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
- *
- * @return void
- */
- public function testBaseUrlWithAppAndWebrootInDirname() {
- Configure::write('App.baseUrl', '/approval/index.php');
- $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
- $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/approval/index.php', $request->base);
- $this->assertEquals('/approval/app/webroot/', $request->webroot);
- Configure::write('App.baseUrl', '/webrootable/index.php');
- $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
- $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/webrootable/index.php', $request->base);
- $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
- }
- /**
- * test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
- *
- * @return void
- */
- public function testBaseUrlNoRewriteWebrootIndex() {
- Configure::write('App.baseUrl', '/index.php');
- $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
- $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEquals('/index.php', $request->base);
- $this->assertEquals('/', $request->webroot);
- }
- /**
- * Test that a request with a . in the main GET parameter is filtered out.
- * PHP changes GET parameter keys containing dots to _.
- *
- * @return void
- */
- public function testGetParamsWithDot() {
- $_GET = array();
- $_GET['/posts/index/add_add'] = '';
- $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
- $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
- $request = new CakeRequest();
- $this->assertEquals(array(), $request->query);
- }
- /**
- * Test that a request with urlencoded bits in the main GET parameter are filtered out.
- *
- * @return void
- */
- public function testGetParamWithUrlencodedElement() {
- $_GET = array();
- $_GET['/posts/add/∂∂'] = '';
- $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
- $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
- $request = new CakeRequest();
- $this->assertEquals(array(), $request->query);
- }
- /**
- * generator for environment configurations
- *
- * @return void
- */
- public static function environmentGenerator() {
- return array(
- array(
- 'IIS - No rewrite base path',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'SCRIPT_NAME' => '/index.php',
- 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/index.php',
- 'URL' => '/index.php',
- 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
- 'ORIG_PATH_INFO' => '/index.php',
- 'PATH_INFO' => '',
- 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'PHP_SELF' => '/index.php',
- ),
- ),
- array(
- 'base' => '/index.php',
- 'webroot' => '/app/webroot/',
- 'url' => ''
- ),
- ),
- array(
- 'IIS - No rewrite with path, no PHP_SELF',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/index.php?',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'QUERY_STRING' => '/posts/add',
- 'REQUEST_URI' => '/index.php?/posts/add',
- 'PHP_SELF' => '',
- 'URL' => '/index.php?/posts/add',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'argv' => array('/posts/add'),
- 'argc' => 1
- ),
- ),
- array(
- 'url' => 'posts/add',
- 'base' => '/index.php?',
- 'webroot' => '/app/webroot/'
- )
- ),
- array(
- 'IIS - No rewrite sub dir 2',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot',
- ),
- 'SERVER' => array(
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/site/index.php',
- 'URL' => '/site/index.php',
- 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'PHP_SELF' => '/site/index.php',
- 'argv' => array(),
- 'argc' => 0
- ),
- ),
- array(
- 'url' => '',
- 'base' => '/site/index.php',
- 'webroot' => '/site/app/webroot/'
- ),
- ),
- array(
- 'IIS - No rewrite sub dir 2 with path',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'GET' => array('/posts/add' => ''),
- 'SERVER' => array(
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
- 'QUERY_STRING' => '/posts/add',
- 'REQUEST_URI' => '/site/index.php/posts/add',
- 'URL' => '/site/index.php/posts/add',
- 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'PHP_SELF' => '/site/index.php/posts/add',
- 'argv' => array('/posts/add'),
- 'argc' => 1
- ),
- ),
- array(
- 'url' => 'posts/add',
- 'base' => '/site/index.php',
- 'webroot' => '/site/app/webroot/'
- )
- ),
- array(
- 'Apache - No rewrite, document root set to webroot, requesting path',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/index.php/posts/index',
- 'SCRIPT_NAME' => '/index.php',
- 'PATH_INFO' => '/posts/index',
- 'PHP_SELF' => '/index.php/posts/index',
- ),
- ),
- array(
- 'url' => 'posts/index',
- 'base' => '/index.php',
- 'webroot' => '/'
- ),
- ),
- array(
- 'Apache - No rewrite, document root set to webroot, requesting root',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/index.php',
- 'SCRIPT_NAME' => '/index.php',
- 'PATH_INFO' => '',
- 'PHP_SELF' => '/index.php',
- ),
- ),
- array(
- 'url' => '',
- 'base' => '/index.php',
- 'webroot' => '/'
- ),
- ),
- array(
- 'Apache - No rewrite, document root set above top level cake dir, requesting path',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/index.php/posts/index',
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_INFO' => '/posts/index',
- 'PHP_SELF' => '/site/index.php/posts/index',
- ),
- ),
- array(
- 'url' => 'posts/index',
- 'base' => '/site/index.php',
- 'webroot' => '/site/app/webroot/',
- ),
- ),
- array(
- 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/index.php/',
- 'SCRIPT_NAME' => '/site/index.php',
- 'PHP_SELF' => '/site/index.php/',
- ),
- ),
- array(
- 'url' => '',
- 'base' => '/site/index.php',
- 'webroot' => '/site/app/webroot/',
- ),
- ),
- array(
- 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'GET' => array('a' => 'b', 'c' => 'd'),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_INFO' => '/posts/index',
- 'PHP_SELF' => '/site/index.php/posts/index',
- 'QUERY_STRING' => 'a=b&c=d'
- ),
- ),
- array(
- 'urlParams' => array('a' => 'b', 'c' => 'd'),
- 'url' => 'posts/index',
- 'base' => '/site/index.php',
- 'webroot' => '/site/app/webroot/',
- ),
- ),
- array(
- 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/',
- 'SCRIPT_NAME' => '/site/app/webroot/index.php',
- 'PHP_SELF' => '/site/app/webroot/index.php',
- ),
- ),
- array(
- 'url' => '',
- 'base' => '/site',
- 'webroot' => '/site/',
- ),
- ),
- array(
- 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'SCRIPT_NAME' => '/site/app/webroot/index.php',
- 'PHP_SELF' => '/site/app/webroot/index.php',
- 'PATH_INFO' => null,
- 'REQUEST_URI' => null,
- ),
- ),
- array(
- 'url' => '',
- 'base' => '/site',
- 'webroot' => '/site/',
- ),
- ),
- array(
- 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
- 'SCRIPT_NAME' => '/index.php',
- 'PHP_SELF' => '/index.php',
- 'PATH_INFO' => null,
- 'REQUEST_URI' => null,
- ),
- ),
- array(
- 'url' => '',
- 'base' => '',
- 'webroot' => '/',
- ),
- ),
- array(
- 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => FULL_BASE_URL . '/site/posts/index',
- 'SCRIPT_NAME' => '/site/app/webroot/index.php',
- 'PHP_SELF' => '/site/app/webroot/index.php',
- ),
- ),
- array(
- 'url' => 'posts/index',
- 'base' => '/site',
- 'webroot' => '/site/',
- ),
- ),
- array(
- 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
- array(
- 'App' => array(
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'app',
- 'webroot' => 'webroot'
- ),
- 'GET' => array('/posts/add' => ''),
- 'SERVER' => array(
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
- 'SCRIPT_NAME' => '/index.php',
- 'QUERY_STRING' => '/posts/add&',
- 'PHP_SELF' => '/index.php',
- 'PATH_INFO' => null,
- 'REQUEST_URI' => '/posts/add',
- ),
- ),
- array(
- 'url' => 'posts/add',
- 'base' => '',
- 'webroot' => '/',
- 'urlParams' => array()
- ),
- ),
- );
- }
- /**
- * testEnvironmentDetection method
- *
- * @dataProvider environmentGenerator
- * @return void
- */
- public function testEnvironmentDetection($name, $env, $expected) {
- $_GET = array();
- $this->_loadEnvironment($env);
- $request = new CakeRequest();
- $this->assertEquals($expected['url'], $request->url, "url error");
- $this->assertEquals($expected['base'], $request->base, "base error");
- $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
- if (isset($expected['urlParams'])) {
- $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
- }
- }
- /**
- * test the query() method
- *
- * @return void
- */
- public function testQuery() {
- $_GET = array();
- $_GET['foo'] = 'bar';
- $request = new CakeRequest();
- $result = $request->query('foo');
- $this->assertEquals('bar', $result);
- $result = $request->query('imaginary');
- $this->assertNull($result);
- }
- /**
- * test the query() method with arrays passed via $_GET
- *
- * @return void
- */
- public function testQueryWithArray() {
- $_GET = array();
- $_GET['test'] = array('foo', 'bar');
- $request = new CakeRequest();
- $result = $request->query('test');
- $this->assertEquals(array('foo', 'bar'), $result);
- $result = $request->query('test.1');
- $this->assertEquals('bar', $result);
- $result = $request->query('test.2');
- $this->assertNull($result);
- }
- /**
- * Test using param()
- *
- * @return void
- */
- public function testReadingParams() {
- $request = new CakeRequest();
- $request->addParams(array(
- 'controller' => 'posts',
- 'admin' => true,
- 'truthy' => 1,
- 'zero' => '0',
- ));
- $this->assertFalse($request->param('not_set'));
- $this->assertTrue($request->param('admin'));
- $this->assertEquals(1, $request->param('truthy'));
- $this->assertEquals('posts', $request->param('controller'));
- $this->assertEquals('0', $request->param('zero'));
- }
- /**
- * test the data() method reading
- *
- * @return void
- */
- public function testDataReading() {
- $_POST['data'] = array(
- 'Model' => array(
- 'field' => 'value'
- )
- );
- $request = new CakeRequest('posts/index');
- $result = $request->data('Model');
- $this->assertEquals($_POST['data']['Model'], $result);
- $result = $request->data('Model.imaginary');
- $this->assertNull($result);
- }
- /**
- * test writing with data()
- *
- * @return void
- */
- public function testDataWriting() {
- $_POST['data'] = array(
- 'Model' => array(
- 'field' => 'value'
- )
- );
- $request = new CakeRequest('posts/index');
- $result = $request->data('Model.new_value', 'new value');
- $this->assertSame($result, $request, 'Return was not $this');
- $this->assertEquals('new value', $request->data['Model']['new_value']);
- $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
- $this->assertEquals('New post', $request->data['Post']['title']);
- $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
- }
- /**
- * test writing falsey values.
- *
- * @return void
- */
- public function testDataWritingFalsey() {
- $request = new CakeRequest('posts/index');
- $request->data('Post.null', null);
- $this->assertNull($request->data['Post']['null']);
- $request->data('Post.false', false);
- $this->assertFalse($request->data['Post']['false']);
- $request->data('Post.zero', 0);
- $this->assertSame(0, $request->data['Post']['zero']);
- $request->data('Post.empty', '');
- $this->assertSame('', $request->data['Post']['empty']);
- }
- /**
- * test accept language
- *
- * @return void
- */
- public function testAcceptLanguage() {
- // Weird language
- $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
- $result = CakeRequest::acceptLanguage();
- $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
- // No qualifier
- $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
- $result = CakeRequest::acceptLanguage();
- $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
- // With qualifier
- $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
- $result = CakeRequest::acceptLanguage();
- $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
- // With spaces
- $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
- $result = CakeRequest::acceptLanguage();
- $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
- // Checking if requested
- $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
- $result = CakeRequest::acceptLanguage();
- $result = CakeRequest::acceptLanguage('en-ca');
- $this->assertTrue($result);
- $result = CakeRequest::acceptLanguage('en-CA');
- $this->assertTrue($result);
- $result = CakeRequest::acceptLanguage('en-us');
- $this->assertFalse($result);
- $result = CakeRequest::acceptLanguage('en-US');
- $this->assertFalse($result);
- }
- /**
- * test the here() method
- *
- * @return void
- */
- public function testHere() {
- Configure::write('App.base', '/base_path');
- $_GET = array('test' => 'value');
- $request = new CakeRequest('/posts/add/1/name:value');
- $result = $request->here();
- $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
- $result = $request->here(false);
- $this->assertEquals('/posts/add/1/name:value?test=value', $result);
- $request = new CakeRequest('/posts/base_path/1/name:value');
- $result = $request->here();
- $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
- $result = $request->here(false);
- $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
- }
- /**
- * Test the input() method.
- *
- * @return void
- */
- public function testInput() {
- $request = $this->getMock('CakeRequest', array('_readInput'));
- $request->expects($this->once())->method('_readInput')
- ->will($this->returnValue('I came from stdin'));
- $result = $request->input();
- $this->assertEquals('I came from stdin', $result);
- }
- /**
- * Test input() decoding.
- *
- * @return void
- */
- public function testInputDecode() {
- $request = $this->getMock('CakeRequest', array('_readInput'));
- $request->expects($this->once())->method('_readInput')
- ->will($this->returnValue('{"name":"value"}'));
- $result = $request->input('json_decode');
- $this->assertEquals(array('name' => 'value'), (array)$result);
- }
- /**
- * Test input() decoding with additional arguments.
- *
- * @return void
- */
- public function testInputDecodeExtraParams() {
- $xml = <<<XML
- <?xml version="1.0" encoding="utf-8"?>
- <post>
- <title id="title">Test</title>
- </post>
- XML;
- $request = $this->getMock('CakeRequest', array('_readInput'));
- $request->expects($this->once())->method('_readInput')
- ->will($this->returnValue($xml));
- $result = $request->input('Xml::build', array('return' => 'domdocument'));
- $this->assertInstanceOf('DOMDocument', $result);
- $this->assertEquals(
- 'Test',
- $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
- );
- }
- /**
- * Test is('requested') and isRequested()
- *
- * @return void
- */
- public function testIsRequested() {
- $request = new CakeRequest('/posts/index');
- $request->addParams(array(
- 'controller' => 'posts',
- 'action' => 'index',
- 'plugin' => null,
- 'requested' => 1
- ));
- $this->assertTrue($request->is('requested'));
- $this->assertTrue($request->isRequested());
- $request = new CakeRequest('/posts/index');
- $request->addParams(array(
- 'controller' => 'posts',
- 'action' => 'index',
- 'plugin' => null,
- ));
- $this->assertFalse($request->is('requested'));
- $this->assertFalse($request->isRequested());
- }
- /**
- * TestOnlyAllow
- *
- * @return void
- */
- public function testOnlyAllow() {
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $request = new CakeRequest('/posts/edit/1');
- $this->assertTrue($request->onlyAllow(array('put')));
- $_SERVER['REQUEST_METHOD'] = 'DELETE';
- $this->assertTrue($request->onlyAllow('post', 'delete'));
- }
- /**
- * TestOnlyAllow throwing exception
- *
- */
- public function testOnlyAllowException() {
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $request = new CakeRequest('/posts/edit/1');
- try {
- $request->onlyAllow('POST', 'DELETE');
- $this->fail('An expected exception has not been raised.');
- } catch (MethodNotAllowedException $e) {
- $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
- }
- $this->setExpectedException('MethodNotAllowedException');
- $request->onlyAllow('POST');
- }
- /**
- * loadEnvironment method
- *
- * @param array $env
- * @return void
- */
- protected function _loadEnvironment($env) {
- if (isset($env['App'])) {
- Configure::write('App', $env['App']);
- }
- if (isset($env['GET'])) {
- foreach ($env['GET'] as $key => $val) {
- $_GET[$key] = $val;
- }
- }
- if (isset($env['POST'])) {
- foreach ($env['POST'] as $key => $val) {
- $_POST[$key] = $val;
- }
- }
- if (isset($env['SERVER'])) {
- foreach ($env['SERVER'] as $key => $val) {
- $_SERVER[$key] = $val;
- }
- }
- }
- }
|