| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524 |
- <?php
- /**
- * CakeRequest Test case file.
- *
- * PHP 5
- *
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @package cake.tests.cases.libs
- * @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 CakeRequestTestCase extends CakeTestCase {
- /**
- * setup callback
- *
- * @return void
- */
- function setUp() {
- parent::setUp();
- $this->_server = $_SERVER;
- $this->_get = $_GET;
- $this->_post = $_POST;
- $this->_files = $_FILES;
- $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
- */
- function tearDown() {
- parent::tearDown();
- $_SERVER = $this->_server;
- $_GET = $this->_get;
- $_POST = $this->_post;
- $_FILES = $this->_files;
- if (!empty($this->_case)) {
- $_GET['case'] = $this->_case;
- }
- Configure::write('App', $this->_app);
- }
- /**
- * test that the autoparse = false constructor works.
- *
- * @return void
- */
- function testNoAutoParseConstruction() {
- $_GET = array(
- 'one' => 'param'
- );
- $request = new CakeRequest(null, false);
- $this->assertFalse(isset($request->query['one']));
- }
- /**
- * test construction
- *
- * @return void
- */
- function testConstructionGetParsing() {
- $_GET = array(
- 'one' => 'param',
- 'two' => 'banana'
- );
- $request = new CakeRequest('some/path');
- $this->assertEqual($request->query, $_GET);
-
- $_GET = array(
- 'one' => 'param',
- 'two' => 'banana',
- );
- $request = new CakeRequest('some/path');
- $this->assertEqual($request->query, $_GET);
- $this->assertEqual($request->url, 'some/path');
- }
- /**
- * Test that querystring args provided in the url string are parsed.
- *
- * @return void
- */
- function testQueryStringParsingFromInputUrl() {
- $_GET = array();
- $request = new CakeRequest('some/path?one=something&two=else');
- $expected = array('one' => 'something', 'two' => 'else');
- $this->assertEqual($request->query, $expected);
- $this->assertEquals('some/path?one=something&two=else', $request->url);
-
- }
- /**
- * test addParams() method
- *
- * @return void
- */
- function testAddParams() {
- $request = new CakeRequest('some/path');
- $request->params = array('controller' => 'posts', 'action' => 'view');
- $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
- $this->assertIdentical($result, $request, 'Method did not return itself. %s');
- $this->assertEqual($request->controller, 'posts');
- $this->assertEqual($request->action, 'index');
- $this->assertEqual($request->plugin, null);
- }
- /**
- * test splicing in paths.
- *
- * @return void
- */
- 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->assertIdentical($result, $request, 'Method did not return itself. %s');
- $this->assertEqual($request->webroot, '/');
- $this->assertEqual($request->base, '/base_dir');
- $this->assertEqual($request->here, '/');
- $this->assertFalse(isset($request->random));
- }
- /**
- * test parsing POST data into the object.
- *
- * @return void
- */
- function testPostParsing() {
- $_POST = array('data' => array(
- 'Article' => array('title')
- ));
- $request = new CakeRequest('some/path');
- $this->assertEqual($request->data, $_POST['data']);
- $_POST = array('one' => 1, 'two' => 'three');
- $request = new CakeRequest('some/path');
- $this->assertEquals($_POST, $request->data);
- }
- /**
- * test parsing of FILES array
- *
- * @return void
- */
- 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->assertEqual($request->data, $expected);
- $_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->assertEqual($request->data, $expected);
- $_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->assertEqual($request->data, $expected);
- $_FILES = array(
- 'something' => array(
- 'name' => 'something.txt',
- 'type' => 'text/plain',
- 'tmp_name' => '/some/file',
- 'error' => 0,
- 'size' => 123
- )
- );
- $request = new CakeRequest('some/path');
- $this->assertEqual($request->params['form'], $_FILES);
- }
- /**
- * test method overrides coming in from POST data.
- *
- * @return void
- */
- function testMethodOverrides() {
- $_POST = array('_method' => 'POST');
- $request = new CakeRequest('some/path');
- $this->assertEqual(env('REQUEST_METHOD'), 'POST');
- $_POST = array('_method' => 'DELETE');
- $request = new CakeRequest('some/path');
- $this->assertEqual(env('REQUEST_METHOD'), 'DELETE');
- $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
- $request = new CakeRequest('some/path');
- $this->assertEqual(env('REQUEST_METHOD'), 'PUT');
- }
- /**
- * test the clientIp method.
- *
- * @return void
- */
- 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->assertEqual($request->clientIp(false), '192.168.1.5');
- $this->assertEqual($request->clientIp(), '192.168.1.2');
- unset($_SERVER['HTTP_X_FORWARDED_FOR']);
- $this->assertEqual($request->clientIp(), '192.168.1.2');
- unset($_SERVER['HTTP_CLIENT_IP']);
- $this->assertEqual($request->clientIp(), '192.168.1.3');
- $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
- $this->assertEqual($request->clientIp(), '10.0.1.2');
- }
- /**
- * test the referer function.
- *
- * @return void
- */
- function testReferer() {
- $request = new CakeRequest('some/path');
- $request->webroot = '/';
- $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
- $result = $request->referer();
- $this->assertIdentical($result, 'http://cakephp.org');
- $_SERVER['HTTP_REFERER'] = '';
- $result = $request->referer();
- $this->assertIdentical($result, '/');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
- $result = $request->referer(true);
- $this->assertIdentical($result, '/some/path');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
- $result = $request->referer(false);
- $this->assertIdentical($result, FULL_BASE_URL . '/some/path');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
- $result = $request->referer(true);
- $this->assertIdentical($result, '/some/path');
- $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/recipes/add';
- $result = $request->referer(true);
- $this->assertIdentical($result, '/recipes/add');
- $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
- $result = $request->referer();
- $this->assertIdentical($result, 'cakephp.org');
- }
- /**
- * test the simple uses of is()
- *
- * @return void
- */
- 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 the method() method.
- *
- * @return void
- */
- function testMethod() {
- $_SERVER['REQUEST_METHOD'] = 'delete';
- $request = new CakeRequest('some/path');
- $this->assertEquals('delete', $request->method());
- }
- /**
- * test host retrieval.
- *
- * @return void
- */
- function testHost() {
- $_SERVER['HTTP_HOST'] = 'localhost';
- $request = new CakeRequest('some/path');
-
- $this->assertEquals('localhost', $request->host());
- }
- /**
- * test domain retrieval.
- *
- * @return void
- */
- 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
- */
- 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
- */
- 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());
- }
- /**
- * test __call expcetions
- *
- * @expectedException CakeException
- * @return void
- */
- function test__callExceptionOnUnknownMethod() {
- $request = new CakeRequest('some/path');
- $request->IamABanana();
- }
- /**
- * test is(ssl)
- *
- * @return void
- */
- 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
- */
- function test__get() {
- $request = new CakeRequest('some/path');
- $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
- $this->assertEqual($request->controller, 'posts');
- $this->assertEqual($request->action, 'view');
- $this->assertEqual($request->plugin, 'blogs');
- $this->assertIdentical($request->banana, null);
- }
- /**
- * test the array access implementation
- *
- * @return void
- */
- function testArrayAccess() {
- $request = new CakeRequest('some/path');
- $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
- $this->assertEqual($request['controller'], 'posts');
- $request['slug'] = 'speedy-slug';
- $this->assertEqual($request->slug, 'speedy-slug');
- $this->assertEqual($request['slug'], 'speedy-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->assertEqual($request['data']['Post']['title'], 'something');
- }
- /**
- * test adding detectors and having them work.
- *
- * @return void
- */
- 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('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->return = true;
- $this->assertTrue($request->isCallMe());
- $request->return = false;
- $this->assertFalse($request->isCallMe());
- }
- /**
- * helper function for testing callbacks.
- *
- * @return void
- */
- function _detectCallback($request) {
- return $request->return == true;
- }
- /**
- * test getting headers
- *
- * @return void
- */
- 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
- */
- 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/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png'
- );
- $this->assertEquals($expected, $result, 'Content types differ.');
-
- $result = $request->accepts('text/html');
- $this->assertTrue($result);
-
- $result = $request->accepts('image/gif');
- $this->assertFalse($result);
- }
- /**
- * testBaseUrlAndWebrootWithModRewrite method
- *
- * @return void
- */
- public function testBaseUrlAndWebrootWithModRewrite() {
- Configure::write('App.baseUrl', false);
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
- $_SERVER['SCRIPT_NAME'] = '/1.2.x.x/app/webroot/index.php';
- $_SERVER['PATH_INFO'] = '/posts/view/1';
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/1.2.x.x');
- $this->assertEqual($request->webroot, '/1.2.x.x/');
- $this->assertEqual($request->url, 'posts/view/1');
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
- $_SERVER['SCRIPT_NAME'] = '/index.php';
- $_SERVER['PATH_INFO'] = '/posts/add';
- $request = new CakeRequest();
- $this->assertEqual($request->base, '');
- $this->assertEqual($request->webroot, '/');
- $this->assertEqual($request->url, 'posts/add');
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
- $_SERVER['SCRIPT_NAME'] = '/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEqual('', $request->base);
- $this->assertEqual('/', $request->webroot);
- $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
- $_SERVER['SCRIPT_NAME'] = '/app/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEqual($request->base, '');
- $this->assertEqual($request->webroot, '/');
- Configure::write('App.dir', 'auth');
- $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
- $_SERVER['SCRIPT_NAME'] = '/demos/auth/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/demos/auth');
- $this->assertEqual($request->webroot, '/demos/auth/');
- Configure::write('App.dir', 'code');
- $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
- $_SERVER['SCRIPT_NAME'] = '/clients/PewterReport/code/webroot/index.php';
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/clients/PewterReport/code');
- $this->assertEqual($request->webroot, '/clients/PewterReport/code/');
- }
- /**
- * testBaseUrlwithModRewriteAlias method
- *
- * @return void
- */
- public function testBaseUrlwithModRewriteAlias() {
- $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
- $_SERVER['SCRIPT_NAME'] = '/control/index.php';
- Configure::write('App.base', '/control');
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/control');
- $this->assertEqual($request->webroot, '/control/');
- Configure::write('App.base', false);
- Configure::write('App.dir', 'affiliate');
- Configure::write('App.webroot', 'newaffiliate');
- $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
- $_SERVER['SCRIPT_NAME'] = '/newaffiliate/index.php';
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/newaffiliate');
- $this->assertEqual($request->webroot, '/newaffiliate/');
- }
- /**
- * test base, webroot, and url parsing when there is no url rewriting
- *
- * @return void
- */
- 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->assertEqual($request->base, '/cake/index.php');
- $this->assertEqual($request->webroot, '/cake/app/webroot/');
- $this->assertEqual($request->url, 'posts/index');
- }
- /**
- * testBaseUrlAndWebrootWithBaseUrl method
- *
- * @return void
- */
- public function testBaseUrlAndWebrootWithBaseUrl() {
- Configure::write('App.dir', 'app');
- Configure::write('App.baseUrl', '/app/webroot/index.php');
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/app/webroot/index.php');
- $this->assertEqual($request->webroot, '/app/webroot/');
- Configure::write('App.baseUrl', '/app/webroot/test.php');
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/app/webroot/test.php');
- $this->assertEqual($request->webroot, '/app/webroot/');
- Configure::write('App.baseUrl', '/app/index.php');
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/app/index.php');
- $this->assertEqual($request->webroot, '/app/webroot/');
- Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/CakeBB/app/webroot/index.php');
- $this->assertEqual($request->webroot, '/CakeBB/app/webroot/');
- Configure::write('App.baseUrl', '/CakeBB/app/index.php');
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/CakeBB/app/index.php');
- $this->assertEqual($request->webroot, '/CakeBB/app/webroot/');
- Configure::write('App.baseUrl', '/CakeBB/index.php');
- $request = new CakeRequest();
- $this->assertEqual($request->base, '/CakeBB/index.php');
- $this->assertEqual($request->webroot, '/CakeBB/app/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->assertEqual($request->base, '/dbhauser/index.php');
- $this->assertEqual($request->webroot, '/dbhauser/app/webroot/');
- }
- /**
- * test baseUrl with no rewrite and using the top level index.php.
- *
- * @return void
- */
- 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->assertEqual('/index.php', $request->base);
- $this->assertEqual('/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
- */
- 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->assertEqual('/index.php', $request->base);
- $this->assertEqual('/', $request->webroot);
- }
- /**
- * 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' => '/',
- ),
- ),
- );
- }
- /**
- * 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->assertEqual($_GET, $expected['urlParams'], "GET param mismatch");
- }
- }
- /**
- * test the data() method reading
- *
- * @return void
- */
- 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
- */
- 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($request->data['Model']['new_value'], 'new value');
- $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
- $this->assertEquals($request->data['Post']['title'], 'New post');
- $this->assertEquals($request->data['Comment']['1']['author'], 'Mark');
- }
- /**
- * test writing falsey values.
- *
- * @return void
- */
- 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
- */
- function testAcceptLanguage() {
- $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
- $result = CakeRequest::acceptLanguage();
- $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
- $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
- $result = CakeRequest::acceptLanguage();
- $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
-
- $result = CakeRequest::acceptLanguage('en-ca');
- $this->assertTrue($result);
- $result = CakeRequest::acceptLanguage('en-us');
- $this->assertFalse($result);
- }
- /**
- * test the here() method
- *
- * @return void
- */
- 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
- */
- function testInput() {
- $request = $this->getMock('CakeRequest', array('_readStdin'));
- $request->expects($this->once())->method('_readStdin')
- ->will($this->returnValue('I came from stdin'));
-
- $result = $request->input();
- $this->assertEquals('I came from stdin', $result);
- }
- /**
- * Test input() decoding.
- *
- * @return void
- */
- function testInputDecode() {
- $request = $this->getMock('CakeRequest', array('_readStdin'));
- $request->expects($this->once())->method('_readStdin')
- ->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
- */
- function testInputDecodeExtraParams() {
- $xml = <<<XML
- <?xml version="1.0" encoding="utf-8"?>
- <post>
- <title id="title">Test</title>
- </post>
- XML;
- $request = $this->getMock('CakeRequest', array('_readStdin'));
- $request->expects($this->once())->method('_readStdin')
- ->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
- );
- }
- /**
- * loadEnvironment method
- *
- * @param mixed $env
- * @return void
- * @access private
- */
- 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;
- }
- }
- }
- }
|