From 56df3d3faa32bb92223b363945ce0b19761f040f Mon Sep 17 00:00:00 2001 From: tof <> Date: Wed, 13 Jun 2007 12:01:20 +0000 Subject: Implements THttpCookie, THttpCookieCollection, and THttpRequest unit tests --- tests/unit/Web/THttpCookieCollectionTest.php | 30 ++- tests/unit/Web/THttpCookieTest.php | 29 ++- tests/unit/Web/THttpRequestTest.php | 285 ++++++++++++++++++++++++--- 3 files changed, 301 insertions(+), 43 deletions(-) (limited to 'tests') diff --git a/tests/unit/Web/THttpCookieCollectionTest.php b/tests/unit/Web/THttpCookieCollectionTest.php index 9c648e2b..ece8f275 100644 --- a/tests/unit/Web/THttpCookieCollectionTest.php +++ b/tests/unit/Web/THttpCookieCollectionTest.php @@ -9,23 +9,43 @@ Prado::using('System.Web.THttpRequest'); class THttpCookieCollectionTest extends PHPUnit_Framework_TestCase { public function testConstruct() { - throw new PHPUnit_Framework_IncompleteTestError(); + $coll=new THttpCookieCollection(); + self::assertType('THttpCookieCollection', $coll); } public function testInsertAt() { - throw new PHPUnit_Framework_IncompleteTestError(); + $coll=new THttpCookieCollection(); + $coll->insertAt(0, new THttpCookie('name','value')); + self::assertEquals('value',$coll->itemAt(0)->getValue()); + try { + $coll->insertAt(1, "bad parameter"); + self::fail ('Invalid data type exception not raised'); + } catch (TInvalidDataTypeException $e) {} } public function testRemoveAt() { - throw new PHPUnit_Framework_IncompleteTestError(); + $coll=new THttpCookieCollection(); + try { + $coll->removeAt(0); + self::fail('Invalid Value exception not raised'); + } catch (TInvalidDataValueException $e) {} + + $coll->insertAt(0, new THttpCookie('name','value')); + self::assertEquals('value',$coll->removeAt(0)->getValue()); } public function testItemAt() { - throw new PHPUnit_Framework_IncompleteTestError(); + $coll=new THttpCookieCollection(); + $coll->insertAt(0, new THttpCookie('name','value')); + self::assertEquals('value',$coll->itemAt(0)->getValue()); + self::assertEquals('value',$coll->itemAt('name')->getValue()); } public function testFindCookieByName() { - throw new PHPUnit_Framework_IncompleteTestError(); + $coll=new THttpCookieCollection(); + $coll->insertAt(0, new THttpCookie('name','value')); + self::assertEquals ('value', $coll->findCookieByName('name')->getValue()); + self::assertNull ($coll->findCookieByName('invalid')); } } ?> \ No newline at end of file diff --git a/tests/unit/Web/THttpCookieTest.php b/tests/unit/Web/THttpCookieTest.php index 8a96e35e..0d6ade09 100644 --- a/tests/unit/Web/THttpCookieTest.php +++ b/tests/unit/Web/THttpCookieTest.php @@ -9,31 +9,46 @@ Prado::using('System.Web.THttpRequest'); class THttpCookieTest extends PHPUnit_Framework_TestCase { public function testConstruct() { - throw new PHPUnit_Framework_IncompleteTestError(); + $cookie=new THttpCookie('name','value'); + self::assertEquals('name',$cookie->getName()); + self::assertEquals('value',$cookie->getValue()); } public function testSetDomain() { - throw new PHPUnit_Framework_IncompleteTestError(); + $cookie=new THttpCookie('name','value'); + $cookie->setDomain('pradosoft.com'); + self::assertEquals('pradosoft.com',$cookie->getdomain()); } public function testSetExpire() { - throw new PHPUnit_Framework_IncompleteTestError(); + $cookie=new THttpCookie('name','value'); + $exp=time()+3600; + $cookie->setExpire($exp); + self::assertEquals($exp,$cookie->getExpire()); } public function testSetName() { - throw new PHPUnit_Framework_IncompleteTestError(); + $cookie=new THttpCookie('name','value'); + $cookie->setName('newName'); + self::assertEquals('newName', $cookie->getName()); } public function testSetValue() { - throw new PHPUnit_Framework_IncompleteTestError(); + $cookie=new THttpCookie('name','value'); + $cookie->setValue('newValue'); + self::assertEquals('newValue', $cookie->getValue()); } public function testSetPath() { - throw new PHPUnit_Framework_IncompleteTestError(); + $cookie=new THttpCookie('name','value'); + $cookie->setPath('/admin'); + self::assertEquals('/admin', $cookie->getPath()); } public function testSetSecure() { - throw new PHPUnit_Framework_IncompleteTestError(); + $cookie=new THttpCookie('name','value'); + $cookie->setSecure(true); + self::assertTrue($cookie->getSecure()); } } ?> \ No newline at end of file diff --git a/tests/unit/Web/THttpRequestTest.php b/tests/unit/Web/THttpRequestTest.php index 5bb66304..3ddc557e 100644 --- a/tests/unit/Web/THttpRequestTest.php +++ b/tests/unit/Web/THttpRequestTest.php @@ -2,6 +2,7 @@ require_once dirname(__FILE__).'/../phpunit.php'; Prado::using('System.Web.THttpRequest'); +Prado::using('System.Security.TSecurityManager'); /** * @package System.Web @@ -27,7 +28,14 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'; $_SERVER['REMOTE_HOST'] = 'localhost'; + $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr,en-us;q=0.8,fr-fr;q=0.5,en;q=0.3'; + $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate'; + $_SERVER['HTTP_ACCEPT_CHARSET'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'; + $_COOKIE['phpsessid']='0123456789abcdef'; + + $_FILES['userfile']=array ('name' => 'test.jpg','type' => 'image/jpg', 'size' => 10240, 'tmp_name' => 'tmpXXAZECZ', 'error'=>0); if(self::$app === null) { self::$app = new TApplication(dirname(__FILE__).'/app'); } @@ -37,6 +45,13 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { $request = new THttpRequest(); $request->init(null); self::assertEquals('', $request->getUrlManager()); + // Try with unsetted REQUEST_URL & PATH_INFO + unset($_SERVER['REQUEST_URI']); + unset($_SERVER['PATH_INFO']); + $request = new THttpRequest(); + $request->init(null); + self::assertEquals('', $request->getUrlManager()); + } public function testStripSlashes() { @@ -49,6 +64,12 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { $request = new THttpRequest(); $request->init(null); self::assertType('TUri', $request->getUrl()); + // Try with $_SERVER['HTTP_HOST'] empty + $request=null; + $request = new THttpRequest(); + $request->init(null); + $_SERVER['HTTP_HOST']=''; + self::assertType('TUri', $request->getUrl()); } public function testGetUrlManager() { @@ -58,11 +79,51 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { } public function testSetUrlManager() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest(); + // Try invalid manager id + $request->setUrlManager('invalidManagerId'); + try { + $request->init(null); + self::fail ('httprequest_urlmanager_inexist exception not thrown'); + } catch (TConfigurationException $e) { + + } + $request=null; + + + // Try with valid module id, but not instance of TUrlManager + $module=new TAssetManager(); + self::$app->setModule('badmanager',$module); + $request = new THttpRequest(); + $request->setUrlManager('badmanager'); + try { + $request->init(null); + self::fail ('httprequest_urlmanager_invalid exception not thrown'); + } catch (TConfigurationException $e) { + + } + $request=null; + + // Finally, try with a valid manager + $module=new TUrlManager (); + self::$app->setModule('goodmanager',$module); + $request = new THttpRequest(); + $request->setUrlManager('goodmanager'); + $request->init(null); + self::assertEquals ('goodmanager', $request->getUrlManager()); + self::assertType ('TUrlManager',$request->getUrlManagerModule()); + } public function testSetUrlFormat() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest(); + $request->setUrlFormat('Path'); + self::assertEquals('Path', $request->getUrlFormat()); + // Test invalid + try { + $request->setUrlFormat('Bad'); + self::fail ('Bad Value exception not thrown'); + } catch (TInvalidDataValueException $e) {} } public function testGetRequestType() { @@ -136,6 +197,22 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { } public function testGetBrowser() { + /*$request = new THttpRequest(); + $request->init(null); + // Reset UserAgent, because constructor of THttpRequest unset it if called from cli ! + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'; + try { + $browser=$request->getBrowser(); + self::assertType ('array', $browser); + self::assertEquals('Firefox', $browser['browser']); + self::assertEquals('2.0.0.3', $browser['version']); + } catch (TPhpErrorException $e) { + // If not supported, skip test + if (strstr($e->getMessage(),'browscap ini directive not set')) + self::markTestSkipped('browscap ini directive not set in php.ini'); + else + self::fail ('Exception raised : '.$e->getMessage()); + }*/ throw new PHPUnit_Framework_IncompleteTestError(); } @@ -157,95 +234,241 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { } public function testGetAcceptTypes() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest(); + $request->init(null); + self::assertEquals('text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', $request->getAcceptTypes()); } public function testGetUserLanguages() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest(); + $request->init(null); + // Browser sent fr,en-us;q=0.8,fr-fr;q=0.5,en;q=0.3 + // that means that browser want fr (1) first, next en-us (0.8), then fr-fr(0.5)n and last en (0.3) + // So, we expect method to return an array with these languages, and this order + $acceptLanguages=array ('fr', 'en-us','fr-fr','en'); + self::assertEquals($acceptLanguages, $request->getUserLanguages()); } public function testSetEnableCookieValidation() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest(); + $request->init(null); + $request->setEnableCookieValidation (true); + self::assertEquals(true, $request->getEnableCookieValidation()); } public function testGetCookies() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + $request->setEnableCookieValidation (false); + $cookies=$request->getCookies(); + self::assertType('THttpCookieCollection', $cookies); + self::assertEquals('0123456789abcdef', $cookies->itemAt('phpsessid')->getValue()); + $request = null; + + // Test with cookie validation + $security=new TSecurityManager(); + self::$app->setModule ('security', $security); + $_COOKIE['phpsessid']=$security->hashData('0123456789abcdef'); + $request = new THttpRequest (); + $request->init (null); + $request->setEnableCookieValidation (true); + $cookies=$request->getCookies(); + self::assertType('THttpCookieCollection', $cookies); + self::assertEquals('0123456789abcdef', $cookies->itemAt('phpsessid')->getValue()); } public function testGetUploadedFiles() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + self::assertEquals($_FILES, $request->getUploadedFiles()); } public function testGetServerVariables() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + self::assertEquals($_SERVER, $request->getServerVariables()); } public function testGetEnvironmentVariables() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + self::assertEquals($_ENV, $request->getEnvironmentVariables()); } public function testConstructUrl() { - throw new PHPUnit_Framework_IncompleteTestError(); - } - - public function testGetAvailableServices() { - throw new PHPUnit_Framework_IncompleteTestError(); - } - - public function testSetAvailableServices() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + // Try to construct an url to the pageservice with some parameters + $url=$request->constructURL('page','Home',array('param1'=>'value1','param2'=>'value2'), true); + self::assertEquals('/demos/personal/index.php?page=Home&param1=value1&param2=value2', $url); + // Try without encode & + $url=$request->constructURL('page','Home',array('param1'=>'value1','param2'=>'value2'), false); + self::assertEquals('/demos/personal/index.php?page=Home¶m1=value1¶m2=value2', $url); } public function testSetServiceID() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->setServiceId('page'); + self::assertEquals('page', $request->getServiceId()); } public function testGetIterator() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + self::assertType ('TMapIterator', $request->getIterator()); } public function testGetCount() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + // Should return 1 (service param, and no get param) + self::assertEquals(1, $request->getCount()); + self::assertEquals(1, $request->count()); + } public function testGetKeys() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + self::assertEquals(array('page'), $request->getKeys()); } public function testItemAt() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + self::assertEquals('Home', $request->itemAt('page')); } public function testAdd() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + $request->Add('param1','value1'); + self::assertEquals('value1', $request->itemAt('param1')); } public function testRemove() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + // Remove an unknow key + self::assertNull($request->remove('param1','value1')); + // Remove a key + self::assertEquals('Home', $request->remove('page')); } public function testContains() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + self::assertTrue($request->contains('page')); + self::assertFalse($request->contains('param')); + } + + public function testClear() { + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + $request->clear(); + self::assertEquals(0,$request->getCount()); } public function testToArray() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init(null); + // Simulate a request with just a service + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + self::assertEquals(array ('page'=>'Home'), $request->toArray()); } public function testOffsetExists() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + // should not exists + self::assertFalse($request->offsetExists(0)); } public function testOffsetGet() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + // should not exists + self::assertNull($request->offsetGet(0)); } public function testOffsetSet() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + $request->offsetSet(0, 'test'); + // should not exists + self::assertEquals('test',$request->offsetGet(0)); } public function testOffsetUnset() { - throw new PHPUnit_Framework_IncompleteTestError(); + $request = new THttpRequest (); + $request->init (null); + $request->offsetSet(0, 'test'); + // Count should be 1 + self::assertEquals (1, $request->count()); + $request->offsetUnset(0); + // Now, count should be zero + self::assertEquals (0, $request->count()); + } + + public function testGetSetID() { + $request=new THttpRequest(); + $request->init(null); + $request->setID('testId'); + self::assertEquals('testId', $request->getID()); + } + + public function testGetSetUrlParamSeparator() { + $request=new THttpRequest(); + $request->init(null); + // Try an invalid separator + try { + $request->setUrlParamSeparator('&&'); + self::fail('httprequest_separator_invalid exception not thrown'); + } catch (TInvalidDataValueException $e) { + } + // Try valid one + $request->setUrlParamSeparator('&'); + self::assertEquals('&', $request->getUrlParamSeparator()); + } + + public function testGetServiceParameter() { + $request=new THttpRequest(); + $request->init(null); + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + self::assertEquals('Home', $request->getServiceParameter()); + } + + public function testGetRequestResolved() { + $request=new THttpRequest(); + $request->init(null); + self::assertFalse($request->getRequestResolved()); + $_GET['page']='Home'; + $request->resolveRequest(array('page')); + self::assertTrue($request->getRequestResolved()); } + } ?> \ No newline at end of file -- cgit v1.2.3