Location: PHPKode > projects > OpenNitro > trunk/Tests/Nitro/HTTPRequest.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Tests :: HTTPRequest                                             |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003-2007 June Systems BV                                   |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// +---------------------------------------------------------------------------+
//
// $Id: HTTPRequest.inc.php 229 2008-04-17 09:20:31Z oli $
//

/**
 * Set error_reporting
 */
error_reporting(E_ALL & ~E_NOTICE);

/**
 * Include required files
 */
require_once NITRO_ROOT.'Nitro/HTTPRequest.inc.php';

/**
 * Common TestCase
 */
class HTTPRequest_Test extends Nitro_TestCase {
	
	function HTTPRequest_Test($Name = "HTTPRequest_Test")
	{
		parent::Nitro_TestCase($Name);
	}
	
	function setUp()
	{
		$this->HTTPRequest = new HTTPRequest();
	}
	
	function tearDown()
	{
		unset($this->HTTPRequest);
	}
	
	function test_HTTPRequest()
	{
		$this->Assert(is_object($this->HTTPRequest));
		$this->Assert(isset($this->HTTPRequest->Method));
		$this->AssertEquals($this->HTTPRequest->Method, 'GET');
		$this->Assert(isset($this->HTTPRequest->PostMethod), 'PostMethod is not set');
		$this->AssertEquals($this->HTTPRequest->PostMethod, 'urlencoded', 'PostMethod is not set to default value if "urlencoded"');
	}

	function test_SetPOSTData()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->POST, NULL);

		// set data
		$this->HTTPRequest->SetPOSTData('Post Data');

		// check set data
		$this->AssertEquals($this->HTTPRequest->POST, 'Post Data');
		$this->AssertEquals($this->HTTPRequest->Method, 'POST');
	}

	function test_AddPOSTData()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->POST, NULL);

		// add data
		$this->HTTPRequest->AddPOSTData('test', 'Post Data');

		// check added data
		$this->AssertEquals($this->HTTPRequest->POST, Array('test' => 'Post Data'));
		$this->AssertEquals($this->HTTPRequest->Method, 'POST');

		// add more data
		$this->HTTPRequest->AddPOSTData('test 2', 'Post Data 2');

		// check added data
		$this->AssertEquals($this->HTTPRequest->POST, Array('test' => 'Post Data', 'test 2' => 'Post Data 2'));
		$this->AssertEquals($this->HTTPRequest->Method, 'POST');
	}
	
	/**
	 * Test addFILEData with contents given to the function
	 */
	function test_addFILEData()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->FILE, NULL);

		// set file data
		$this->HTTPRequest->addFILEData('FileVariable', 'FileName.txt', 'File contents');
		
		// check set data
		$this->Assert(is_Array($this->HTTPRequest->FILE['FileVariable']));
		$this->AssertEquals('FileName.txt', $this->HTTPRequest->FILE['FileVariable']['FileName']);
		$this->AssertEquals('File contents', $this->HTTPRequest->FILE['FileVariable']['FileContents']);
		$this->AssertEquals('text/plain', $this->HTTPRequest->FILE['FileVariable']['MimeType']);
		$this->AssertEquals('POST', $this->HTTPRequest->Method);
		$this->AssertEquals('multipart', $this->HTTPRequest->PostMethod);
	}

	/**
	 * Test addFILEData with contents from file
	 */
	function test_addFILEData_2()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->FILE, NULL);

		// set file data
		$this->HTTPRequest->addFILEData('FileVariable', NITRO_ROOT.'Nitro/Defaults/Texts/Page.html', FALSE);
		
		// check set data
		$this->Assert(is_Array($this->HTTPRequest->FILE['FileVariable']));
		$this->AssertEquals('Page.html', $this->HTTPRequest->FILE['FileVariable']['FileName']);
		$this->AssertEquals(file_get_contents(NITRO_ROOT.'Nitro/Defaults/Texts/Page.html'), $this->HTTPRequest->FILE['FileVariable']['FileContents']);
		$this->AssertEquals('text/html', $this->HTTPRequest->FILE['FileVariable']['MimeType']);
		$this->AssertEquals('POST', $this->HTTPRequest->Method);
		$this->AssertEquals('multipart', $this->HTTPRequest->PostMethod);
	}
	
	/**
	 * Test addFILEData with given mimetype
	 */
	function test_addFILEData_3()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->FILE, NULL);

		// set file data
		$this->HTTPRequest->addFILEData('FileVariable', 'Page.html', 'File contents', 'text/fake');
		
		// check set data
		$this->Assert(is_Array($this->HTTPRequest->FILE['FileVariable']));
		$this->AssertEquals('Page.html', $this->HTTPRequest->FILE['FileVariable']['FileName']);
		$this->AssertEquals('File contents', $this->HTTPRequest->FILE['FileVariable']['FileContents']);
		$this->AssertEquals('text/fake', $this->HTTPRequest->FILE['FileVariable']['MimeType']);
		$this->AssertEquals('POST', $this->HTTPRequest->Method);
		$this->AssertEquals('multipart', $this->HTTPRequest->PostMethod);
	}
	
	function test_SetCOOKIEData()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->COOKIE, NULL);

		// set cookie data
		$COOKIEData = 'test cookie data';
		$this->HTTPRequest->SetCOOKIEData($COOKIEData);
		
		// check set cookie
		$this->AssertEquals($this->HTTPRequest->COOKIE, $COOKIEData);
	}

	function test_SetUserAgent()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->UserAgent, 'OpenNitro HTTPRequest Object');

		// set user agent
		$UserAgent = 'test user agent';
		$this->HTTPRequest->SetUserAgent($UserAgent);
		
		// check set user agent
		$this->AssertEquals($this->HTTPRequest->UserAgent, $UserAgent);
	}

	function test_SetAuthorization()
	{
		// check initial
		$this->AssertEquals($this->HTTPRequest->Username, NULL);
		$this->AssertEquals($this->HTTPRequest->Password, NULL);

		// set authorization
		$Username = 'user name';
		$Password = 'pass word';
		$this->HTTPRequest->SetAuthorization($Username, $Password);
		
		// check set authorization
		$this->AssertEquals($this->HTTPRequest->Username, $Username);
		$this->AssertEquals($this->HTTPRequest->Password, $Password);
	}

	/**
	 * TestCase for simple HTTP GET
	 */
	function test_Get()
	{
		$File = NITRO_ROOT.'Nitro/Defaults/Scripts/default.js';
		$Response = $this->HTTPRequest->Get('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['SCRIPT_NAME']).'/GetObject.php?NitroDefault=Scripts/default.js');

		$this->Assert(is_object($Response));
		$this->AssertEquals('HTTP/1.1 200 OK', $Response->GetParsedHeader('Status'));
		$this->AssertEquals((string)filesize($File), $Response->GetParsedHeader('Content-Length'));
		$this->AssertEquals('application/x-javascript', $Response->GetParsedHeader('Content-Type'));
		$this->AssertEquals(file_get_contents($File), $Response->GetBody());
	}

	function test_ParseURL()
	{
		$URL = 'http://www.opennitro.org/index.php';
		$expected = Array(
			'RequestType'	=> 'http',
			'Username'		=> '',
			'Password'		=> '',
			'Host'				=> 'www.opennitro.org',
			'Port'				=> '',
			'Script'			=> 'index.php',
			'GetString'		=> ''
		);
		$this->AssertEquals($expected, $this->HTTPRequest->ParseURL($URL));
		$this->AssertEquals($expected, HTTPRequest::ParseURL($URL));
	}

	function test_ParseURL_2()
	{
		$URL = 'http://user:hide@address.com:8080/index.php?P=test';
		$expected = Array(
			'RequestType'	=> 'http',
			'Username'		=> 'user',
			'Password'		=> 'pass',
			'Host'				=> 'www.opennitro.org',
			'Port'				=> '8080',
			'Script'			=> 'index.php',
			'GetString'		=> 'P=test'
		);
		$this->AssertEquals($expected, $this->HTTPRequest->ParseURL($URL));
		$this->AssertEquals($expected, HTTPRequest::ParseURL($URL));
	}

	function test_ParseURL_3()
	{
		$URL = 'http://hide@address.com:8080/index.php?P=test';
		$expected = Array(
			'RequestType'	=> 'http',
			'Username'		=> 'user',
			'Password'		=> '',
			'Host'				=> 'www.opennitro.org',
			'Port'				=> '8080',
			'Script'			=> 'index.php',
			'GetString'		=> 'P=test'
		);
		$this->AssertEquals($expected, $this->HTTPRequest->ParseURL($URL));
		$this->AssertEquals($expected, HTTPRequest::ParseURL($URL));
	}

	function test_ParseURL_4()
	{
		$URL = 'http://user:www.opennitro.org:8080/index.php?P=test';
		$expected = Array(
			'RequestType'	=> 'http',
			'Username'		=> 'user',
			'Password'		=> '',
			'Host'				=> 'www.opennitro.org',
			'Port'				=> '8080',
			'Script'			=> 'index.php',
			'GetString'		=> 'P=test'
		);
		$this->AssertEquals(FALSE, $this->HTTPRequest->ParseURL($URL));
		$this->AssertEquals(FALSE, HTTPRequest::ParseURL($URL));
	}

	/**
	 * TestCase for HTTPRequest::_FlattenArray_R
	 */
	function test__FlattenArray_R()
	{
		$Data = array(
			'void1' => NULL,
			'void2' => FALSE,
			'empty' => array(),
			'bla' 	=> array(0 => array(0 => 'foo', 1 => 'bar')),
			' gnu%' => array(0 => array(0 => 'foo', 1 => array('1' => 'gnets', '2' => '$narf'))),
			' gnu?' => array('abc' => array(1 => array('1' => 'gnets', '2' => '$narf'), 0 => 'foo')),
		);
		$Expected = array(
			'void1' => 'void1=',
			'void2' => 'void2=',
			'empty' => 'empty[]=',
			'bla' 	=> 'bla[0][0]=foo&bla[0][1]=bar',
			' gnu%' => '%20gnu%25[0][0]=foo&%20gnu%25[0][1][1]=gnets&%20gnu%25[0][1][2]=%24narf',
			' gnu?' => '%20gnu%3F[abc][1][1]=gnets&%20gnu%3F[abc][1][2]=%24narf&%20gnu%3F[abc][0]=foo',
		);
		
		foreach ($Data AS $key => $mixed) {
			$this->AssertEquals($Expected[$key], HTTPRequest::_FlattenArray_R(rawurlencode($key), $mixed));
		}
	}

	function test__processPostData()
	{
		$this->HTTPRequest->setPOSTData('test=test&test2=test2');
		$expected = Array(
			"postData" => "test=test&test2=test2\r\n",
			"Request" => "Content-Length: 23\r\nContent-Type: application/x-www-form-urlencoded\r\n"
		);
		$this->AssertEquals($expected, $this->HTTPRequest->_processPostData());
	}

	function test__processPostData_2()
	{
		$this->HTTPRequest->setPOSTData(Array('test' => 'test', 'test2' => 'test2'));
		$expected = Array(
			"postData" => "test=test&test2=test2\r\n",
			"Request" => "Content-Length: 23\r\nContent-Type: application/x-www-form-urlencoded\r\n"
		);
		$this->AssertEquals($expected, $this->HTTPRequest->_processPostData());
	}

	function test__processPostData_3()
	{
		$Boundary = '--------boundary';
		$this->HTTPRequest->setPOSTMethod('multipart', $Boundary);
		$this->HTTPRequest->setPOSTData(Array('test' => 'test', 'test2' => 'test2'));
		$expected = Array(
			"postData" => "--------boundary\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\ntest\r\n--------boundary\r\nContent-Disposition: form-data; name=\"test2\"\r\n\r\ntest2\r\n--------boundary--\r\n",
			"Request" => "Content-Length: 164\r\nContent-Type: multipart/form-data; boundary=".$Boundary."\r\n"
		);
		$this->AssertEquals($expected, $this->HTTPRequest->_processPostData());
	}

	function test__processPostData_4()
	{
		$Boundary = '--------boundary';
		$this->HTTPRequest->setPOSTMethod('multipart', $Boundary);
		$this->HTTPRequest->setPOSTData(Array('test' => 'test', 'test2' => 'test2', 'test3' => Array('artest1' => 1, 'artest2' => 2)));
		$expected = Array(
			"postData" => "--------boundary\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\ntest\r\n--------boundary\r\nContent-Disposition: form-data; name=\"test2\"\r\n\r\ntest2\r\n--------boundary\r\nContent-Disposition: form-data; name=\"test3[artest1]\"\r\n\r\n1\r\n--------boundary\r\nContent-Disposition: form-data; name=\"test3[artest2]\"\r\n\r\n2\r\n--------boundary--\r\n",
			"Request" => "Content-Length: 320\r\nContent-Type: multipart/form-data; boundary=".$Boundary."\r\n"
		);
		$this->AssertEquals($expected, $this->HTTPRequest->_processPostData());
	}
}
?>
Return current item: OpenNitro