mirror of
https://github.com/brmlab/brmsklad.git
synced 2025-08-03 14:43:49 +02:00
Backup of current cakephp version
This commit is contained in:
parent
b8f82da6f8
commit
5a580df460
925 changed files with 238041 additions and 1 deletions
64
lib/Cake/Test/Case/Network/Http/BasicAuthenticationTest.php
Normal file
64
lib/Cake/Test/Case/Network/Http/BasicAuthenticationTest.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
/**
|
||||
* BasicAuthenticationTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
||||
* @package Cake.Test.Case.Network.Http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('HttpSocket', 'Network/Http');
|
||||
App::uses('BasicAuthentication', 'Network/Http');
|
||||
|
||||
/**
|
||||
* BasicMethodTest class
|
||||
*
|
||||
* @package Cake.Test.Case.Network.Http
|
||||
*/
|
||||
class BasicAuthenticationTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* testAuthentication method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAuthentication() {
|
||||
$http = new HttpSocket();
|
||||
$auth = array(
|
||||
'method' => 'Basic',
|
||||
'user' => 'mark',
|
||||
'pass' => 'secret'
|
||||
);
|
||||
|
||||
BasicAuthentication::authentication($http, $auth);
|
||||
$this->assertEquals('Basic bWFyazpzZWNyZXQ=', $http->request['header']['Authorization']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testProxyAuthentication method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testProxyAuthentication() {
|
||||
$http = new HttpSocket();
|
||||
$proxy = array(
|
||||
'method' => 'Basic',
|
||||
'user' => 'mark',
|
||||
'pass' => 'secret'
|
||||
);
|
||||
|
||||
BasicAuthentication::proxyAuthentication($http, $proxy);
|
||||
$this->assertEquals('Basic bWFyazpzZWNyZXQ=', $http->request['header']['Proxy-Authorization']);
|
||||
}
|
||||
|
||||
}
|
195
lib/Cake/Test/Case/Network/Http/DigestAuthenticationTest.php
Normal file
195
lib/Cake/Test/Case/Network/Http/DigestAuthenticationTest.php
Normal file
|
@ -0,0 +1,195 @@
|
|||
<?php
|
||||
/**
|
||||
* DigestAuthenticationTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
||||
* @package Cake.Test.Case.Network.Http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('HttpSocket', 'Network/Http');
|
||||
App::uses('DigestAuthentication', 'Network/Http');
|
||||
|
||||
class DigestHttpSocket extends HttpSocket {
|
||||
|
||||
/**
|
||||
* nextHeader attribute
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $nextHeader = '';
|
||||
|
||||
/**
|
||||
* request method
|
||||
*
|
||||
* @param mixed $request
|
||||
* @return void
|
||||
*/
|
||||
public function request($request = array()) {
|
||||
if ($request === false) {
|
||||
if (isset($this->response['header']['WWW-Authenticate'])) {
|
||||
unset($this->response['header']['WWW-Authenticate']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$this->response['header']['WWW-Authenticate'] = $this->nextHeader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* DigestAuthenticationTest class
|
||||
*
|
||||
* @package Cake.Test.Case.Network.Http
|
||||
*/
|
||||
class DigestAuthenticationTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* Socket property
|
||||
*
|
||||
* @var mixed null
|
||||
*/
|
||||
public $HttpSocket = null;
|
||||
|
||||
/**
|
||||
* This function sets up a HttpSocket instance we are going to use for testing
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->HttpSocket = new DigestHttpSocket();
|
||||
$this->HttpSocket->request['method'] = 'GET';
|
||||
$this->HttpSocket->request['uri']['path'] = '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* We use this function to clean up after the test case was executed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function tearDown() {
|
||||
unset($this->HttpSocket);
|
||||
}
|
||||
|
||||
/**
|
||||
* testBasic method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBasic() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
|
||||
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(isset($this->HttpSocket->request['header']['Authorization']));
|
||||
$this->assertEquals('The batcave', $auth['realm']);
|
||||
$this->assertEquals('4cded326c6c51', $auth['nonce']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testQop method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testQop() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$expected = 'Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="da7e2a46b471d77f70a9bb3698c8902b"';
|
||||
$this->assertEquals($expected, $this->HttpSocket->request['header']['Authorization']);
|
||||
$this->assertFalse(isset($auth['qop']));
|
||||
$this->assertFalse(isset($auth['nc']));
|
||||
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$expected = '@Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="[a-z0-9]{32}", qop="auth", nc=00000001, cnonce="[a-z0-9]+"@';
|
||||
$this->assertRegExp($expected, $this->HttpSocket->request['header']['Authorization']);
|
||||
$this->assertEquals('auth', $auth['qop']);
|
||||
$this->assertEquals(2, $auth['nc']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testOpaque method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testOpaque() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertFalse(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'));
|
||||
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"') > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* testMultipleRequest method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMultipleRequest() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000001') > 0);
|
||||
$this->assertEquals(2, $auth['nc']);
|
||||
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000002') > 0);
|
||||
$this->assertEquals(3, $auth['nc']);
|
||||
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
|
||||
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
|
||||
|
||||
$this->HttpSocket->nextHeader = '';
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000003') > 0);
|
||||
$this->assertEquals(4, $auth['nc']);
|
||||
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
|
||||
$responseB = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
|
||||
$this->assertNotEquals($response, $responseB);
|
||||
}
|
||||
|
||||
/**
|
||||
* testPathChanged method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPathChanged() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$this->HttpSocket->request['uri']['path'] = '/admin';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
|
||||
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
|
||||
$this->assertNotEquals('da7e2a46b471d77f70a9bb3698c8902b', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* testNoDigestResponse method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNoDigestResponse() {
|
||||
$this->HttpSocket->nextHeader = false;
|
||||
$this->HttpSocket->request['uri']['path'] = '/admin';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
|
||||
}
|
||||
|
||||
}
|
558
lib/Cake/Test/Case/Network/Http/HttpResponseTest.php
Normal file
558
lib/Cake/Test/Case/Network/Http/HttpResponseTest.php
Normal file
|
@ -0,0 +1,558 @@
|
|||
<?php
|
||||
/**
|
||||
* HttpResponseTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
||||
* @package Cake.Test.Case.Network.Http
|
||||
* @since CakePHP(tm) v 1.2.0.4206
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('HttpResponse', 'Network/Http');
|
||||
|
||||
/**
|
||||
* TestHttpResponse class
|
||||
*
|
||||
* @package Cake.Test.Case.Network.Http
|
||||
*/
|
||||
class TestHttpResponse extends HttpResponse {
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param array $header Header as an indexed array (field => value)
|
||||
* @return array Parsed header
|
||||
*/
|
||||
public function parseHeader($header) {
|
||||
return parent::_parseHeader($header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param string $body A string containing the body to decode
|
||||
* @param boolean|string $encoding Can be false in case no encoding is being used, or a string representing the encoding
|
||||
* @return mixed Array or false
|
||||
*/
|
||||
public function decodeBody($body, $encoding = 'chunked') {
|
||||
return parent::_decodeBody($body, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param string $body A string containing the chunked body to decode
|
||||
* @return mixed Array or false
|
||||
*/
|
||||
public function decodeChunkedBody($body) {
|
||||
return parent::_decodeChunkedBody($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param string $token Token to unescape
|
||||
* @return string Unescaped token
|
||||
*/
|
||||
public function unescapeToken($token, $chars = null) {
|
||||
return parent::_unescapeToken($token, $chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param boolean $hex true to get them as HEX values, false otherwise
|
||||
* @return array Escape chars
|
||||
*/
|
||||
public function tokenEscapeChars($hex = true, $chars = null) {
|
||||
return parent::_tokenEscapeChars($hex, $chars);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpResponseTest class
|
||||
*
|
||||
* @package Cake.Test.Case.Network.Http
|
||||
*/
|
||||
class HttpResponseTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* This function sets up a HttpResponse
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->HttpResponse = new TestHttpResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* testBody
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBody() {
|
||||
$this->HttpResponse->body = 'testing';
|
||||
$this->assertEquals('testing', $this->HttpResponse->body());
|
||||
|
||||
$this->HttpResponse->body = null;
|
||||
$this->assertSame($this->HttpResponse->body(), '');
|
||||
}
|
||||
|
||||
/**
|
||||
* testToString
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testToString() {
|
||||
$this->HttpResponse->body = 'other test';
|
||||
$this->assertEquals('other test', $this->HttpResponse->body());
|
||||
$this->assertEquals('other test', (string)$this->HttpResponse);
|
||||
$this->assertTrue(strpos($this->HttpResponse, 'test') > 0);
|
||||
|
||||
$this->HttpResponse->body = null;
|
||||
$this->assertEquals('', (string)$this->HttpResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetHeader
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetHeader() {
|
||||
$this->HttpResponse->headers = array(
|
||||
'foo' => 'Bar',
|
||||
'Some' => 'ok',
|
||||
'HeAdEr' => 'value',
|
||||
'content-Type' => 'text/plain'
|
||||
);
|
||||
|
||||
$this->assertEquals('Bar', $this->HttpResponse->getHeader('foo'));
|
||||
$this->assertEquals('Bar', $this->HttpResponse->getHeader('Foo'));
|
||||
$this->assertEquals('Bar', $this->HttpResponse->getHeader('FOO'));
|
||||
$this->assertEquals('value', $this->HttpResponse->getHeader('header'));
|
||||
$this->assertEquals('text/plain', $this->HttpResponse->getHeader('Content-Type'));
|
||||
$this->assertSame($this->HttpResponse->getHeader(0), null);
|
||||
|
||||
$this->assertEquals('Bar', $this->HttpResponse->getHeader('foo', false));
|
||||
$this->assertEquals('not from class', $this->HttpResponse->getHeader('foo', array('foo' => 'not from class')));
|
||||
}
|
||||
|
||||
/**
|
||||
* testIsOk
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIsOk() {
|
||||
$this->HttpResponse->code = 0;
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = -1;
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = 201;
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = 'what?';
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = 200;
|
||||
$this->assertTrue($this->HttpResponse->isOk());
|
||||
}
|
||||
|
||||
/**
|
||||
* testIsRedirect
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIsRedirect() {
|
||||
$this->HttpResponse->code = 0;
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = -1;
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 201;
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 'what?';
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 301;
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 302;
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 303;
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 307;
|
||||
$this->assertFalse($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 301;
|
||||
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
|
||||
$this->assertTrue($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 302;
|
||||
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
|
||||
$this->assertTrue($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 303;
|
||||
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
|
||||
$this->assertTrue($this->HttpResponse->isRedirect());
|
||||
$this->HttpResponse->code = 307;
|
||||
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
|
||||
$this->assertTrue($this->HttpResponse->isRedirect());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that HttpSocket::parseHeader can take apart a given (and valid) $header string and turn it into an array.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseHeader() {
|
||||
$r = $this->HttpResponse->parseHeader(array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
|
||||
$this->assertEquals(array('foo' => 'Bar', 'fOO-bAr' => 'quux'), $r);
|
||||
|
||||
$r = $this->HttpResponse->parseHeader(true);
|
||||
$this->assertEquals(false, $r);
|
||||
|
||||
$header = "Host: cakephp.org\t\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Host' => 'cakephp.org'
|
||||
);
|
||||
$this->assertEquals($expected, $r);
|
||||
|
||||
$header = "Date:Sat, 07 Apr 2007 10:10:25 GMT\r\nX-Powered-By: PHP/5.1.2\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Date' => 'Sat, 07 Apr 2007 10:10:25 GMT',
|
||||
'X-Powered-By' => 'PHP/5.1.2'
|
||||
);
|
||||
$this->assertEquals($expected, $r);
|
||||
|
||||
$header = "people: Jim,John\r\nfoo-LAND: Bar\r\ncAKe-PHP: rocks\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'people' => 'Jim,John',
|
||||
'foo-LAND' => 'Bar',
|
||||
'cAKe-PHP' => 'rocks'
|
||||
);
|
||||
$this->assertEquals($expected, $r);
|
||||
|
||||
$header = "People: Jim,John,Tim\r\nPeople: Lisa,Tina,Chelsea\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'People' => array('Jim,John,Tim', 'Lisa,Tina,Chelsea')
|
||||
);
|
||||
$this->assertEquals($expected, $r);
|
||||
|
||||
$header = "Multi-Line: I am a \r\nmulti line\t\r\nfield value.\r\nSingle-Line: I am not\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Multi-Line' => "I am a\r\nmulti line\r\nfield value.",
|
||||
'Single-Line' => 'I am not'
|
||||
);
|
||||
$this->assertEquals($expected, $r);
|
||||
|
||||
$header = "Esc\"@\"ped: value\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Esc@ped' => 'value'
|
||||
);
|
||||
$this->assertEquals($expected, $r);
|
||||
}
|
||||
|
||||
/**
|
||||
* testParseResponse method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseResponse() {
|
||||
$tests = array(
|
||||
'simple-request' => array(
|
||||
'response' => array(
|
||||
'status-line' => "HTTP/1.x 200 OK\r\n",
|
||||
'header' => "Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n",
|
||||
'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
|
||||
),
|
||||
'expectations' => array(
|
||||
'httpVersion' => 'HTTP/1.x',
|
||||
'code' => 200,
|
||||
'reasonPhrase' => 'OK',
|
||||
'headers' => array('Date' => 'Mon, 16 Apr 2007 04:14:16 GMT', 'Server' => 'CakeHttp Server'),
|
||||
'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
|
||||
)
|
||||
),
|
||||
'no-header' => array(
|
||||
'response' => array(
|
||||
'status-line' => "HTTP/1.x 404 OK\r\n",
|
||||
'header' => null
|
||||
),
|
||||
'expectations' => array(
|
||||
'code' => 404,
|
||||
'headers' => array()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$testResponse = array();
|
||||
$expectations = array();
|
||||
|
||||
foreach ($tests as $name => $test) {
|
||||
$testResponse = array_merge($testResponse, $test['response']);
|
||||
$testResponse['response'] = $testResponse['status-line'] . $testResponse['header'] . "\r\n" . $testResponse['body'];
|
||||
$this->HttpResponse->parseResponse($testResponse['response']);
|
||||
$expectations = array_merge($expectations, $test['expectations']);
|
||||
|
||||
foreach ($expectations as $property => $expectedVal) {
|
||||
$this->assertEquals($expectedVal, $this->HttpResponse->{$property}, 'Test "' . $name . '": response.' . $property . ' - %s');
|
||||
}
|
||||
|
||||
foreach (array('status-line', 'header', 'body', 'response') as $field) {
|
||||
$this->assertEquals($this->HttpResponse['raw'][$field], $testResponse[$field], 'Test response.raw.' . $field . ': %s');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* data provider function for testInvalidParseResponseData
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function invalidParseResponseDataProvider() {
|
||||
return array(
|
||||
array(array('foo' => 'bar')),
|
||||
array(true),
|
||||
array("HTTP Foo\r\nBar: La"),
|
||||
array('HTTP/1.1 TEST ERROR')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* testInvalidParseResponseData
|
||||
*
|
||||
* @dataProvider invalidParseResponseDataProvider
|
||||
* @expectedException SocketException
|
||||
* return void
|
||||
*/
|
||||
public function testInvalidParseResponseData($value) {
|
||||
$this->HttpResponse->parseResponse($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDecodeBody method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDecodeBody() {
|
||||
$r = $this->HttpResponse->decodeBody(true);
|
||||
$this->assertEquals(false, $r);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody('Foobar', false);
|
||||
$this->assertEquals(array('body' => 'Foobar', 'header' => false), $r);
|
||||
|
||||
$encoding = 'chunked';
|
||||
$sample = array(
|
||||
'encoded' => "19\r\nThis is a chunked message\r\n0\r\n",
|
||||
'decoded' => array('body' => "This is a chunked message", 'header' => false)
|
||||
);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
|
||||
$this->assertEquals($r, $sample['decoded']);
|
||||
|
||||
$encoding = 'chunked';
|
||||
$sample = array(
|
||||
'encoded' => "19\nThis is a chunked message\r\n0\n",
|
||||
'decoded' => array('body' => "This is a chunked message", 'header' => false)
|
||||
);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
|
||||
$this->assertEquals($r, $sample['decoded'], 'Inconsistent line terminators should be tolerated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* testDecodeFooCoded
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDecodeFooCoded() {
|
||||
$r = $this->HttpResponse->decodeBody(true);
|
||||
$this->assertEquals(false, $r);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody('Foobar', false);
|
||||
$this->assertEquals(array('body' => 'Foobar', 'header' => false), $r);
|
||||
|
||||
$encoding = 'foo-bar';
|
||||
$sample = array(
|
||||
'encoded' => '!Foobar!',
|
||||
'decoded' => array('body' => '!Foobar!', 'header' => false),
|
||||
);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
|
||||
$this->assertEquals($r, $sample['decoded']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDecodeChunkedBody method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDecodeChunkedBody() {
|
||||
$r = $this->HttpResponse->decodeChunkedBody(true);
|
||||
$this->assertEquals(false, $r);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\n0\r\n";
|
||||
$decoded = "This is a chunked message";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals(false, $r['header']);
|
||||
|
||||
$encoded = "19 \r\nThis is a chunked message\r\n0\r\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\n";
|
||||
$decoded = "This is a chunked message\nThat is cool\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals(false, $r['header']);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE;foo-chunk=5\r\n\nThat is cool\n\r\n0\r\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals(false, $r['header']);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals(array('foo-header' => 'bar', 'cake' => 'PHP'), $r['header']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDecodeChunkedBodyError method
|
||||
*
|
||||
* @expectedException SocketException
|
||||
* @return void
|
||||
*/
|
||||
public function testDecodeChunkedBodyError() {
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
}
|
||||
|
||||
/**
|
||||
* testParseCookies method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseCookies() {
|
||||
$header = array(
|
||||
'Set-Cookie' => array(
|
||||
'foo=bar',
|
||||
'people=jim,jack,johnny";";Path=/accounts',
|
||||
'google=not=nice'
|
||||
),
|
||||
'Transfer-Encoding' => 'chunked',
|
||||
'Date' => 'Sun, 18 Nov 2007 18:57:42 GMT',
|
||||
);
|
||||
$cookies = $this->HttpResponse->parseCookies($header);
|
||||
$expected = array(
|
||||
'foo' => array(
|
||||
'value' => 'bar'
|
||||
),
|
||||
'people' => array(
|
||||
'value' => 'jim,jack,johnny";"',
|
||||
'path' => '/accounts',
|
||||
),
|
||||
'google' => array(
|
||||
'value' => 'not=nice',
|
||||
)
|
||||
);
|
||||
$this->assertEquals($expected, $cookies);
|
||||
|
||||
$header['Set-Cookie'][] = 'cakephp=great; Secure';
|
||||
$expected['cakephp'] = array('value' => 'great', 'secure' => true);
|
||||
$cookies = $this->HttpResponse->parseCookies($header);
|
||||
$this->assertEquals($expected, $cookies);
|
||||
|
||||
$header['Set-Cookie'] = 'foo=bar';
|
||||
unset($expected['people'], $expected['cakephp'], $expected['google']);
|
||||
$cookies = $this->HttpResponse->parseCookies($header);
|
||||
$this->assertEquals($expected, $cookies);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that escaped token strings are properly unescaped by HttpSocket::unescapeToken
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUnescapeToken() {
|
||||
$this->assertEquals('Foo', $this->HttpResponse->unescapeToken('Foo'));
|
||||
|
||||
$escape = $this->HttpResponse->tokenEscapeChars(false);
|
||||
foreach ($escape as $char) {
|
||||
$token = 'My-special-"' . $char . '"-Token';
|
||||
$unescapedToken = $this->HttpResponse->unescapeToken($token);
|
||||
$expectedToken = 'My-special-' . $char . '-Token';
|
||||
|
||||
$this->assertEquals($expectedToken, $unescapedToken, 'Test token unescaping for ASCII ' . ord($char));
|
||||
}
|
||||
|
||||
$token = 'Extreme-":"Token-" "-""""@"-test';
|
||||
$escapedToken = $this->HttpResponse->unescapeToken($token);
|
||||
$expectedToken = 'Extreme-:Token- -"@-test';
|
||||
$this->assertEquals($expectedToken, $escapedToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* testArrayAccess
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayAccess() {
|
||||
$this->HttpResponse->httpVersion = 'HTTP/1.1';
|
||||
$this->HttpResponse->code = 200;
|
||||
$this->HttpResponse->reasonPhrase = 'OK';
|
||||
$this->HttpResponse->headers = array(
|
||||
'Server' => 'CakePHP',
|
||||
'ContEnt-Type' => 'text/plain'
|
||||
);
|
||||
$this->HttpResponse->cookies = array(
|
||||
'foo' => array('value' => 'bar'),
|
||||
'bar' => array('value' => 'foo')
|
||||
);
|
||||
$this->HttpResponse->body = 'This is a test!';
|
||||
$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\nServer: CakePHP\r\nContEnt-Type: text/plain\r\n\r\nThis is a test!";
|
||||
$expectedOne = "HTTP/1.1 200 OK\r\n";
|
||||
$this->assertEquals($expectedOne, $this->HttpResponse['raw']['status-line']);
|
||||
$expectedTwo = "Server: CakePHP\r\nContEnt-Type: text/plain\r\n";
|
||||
$this->assertEquals($expectedTwo, $this->HttpResponse['raw']['header']);
|
||||
$expectedThree = 'This is a test!';
|
||||
$this->assertEquals($expectedThree, $this->HttpResponse['raw']['body']);
|
||||
$expected = $expectedOne . $expectedTwo . "\r\n" . $expectedThree;
|
||||
$this->assertEquals($expected, $this->HttpResponse['raw']['response']);
|
||||
|
||||
$expected = 'HTTP/1.1';
|
||||
$this->assertEquals($expected, $this->HttpResponse['status']['http-version']);
|
||||
$expected = 200;
|
||||
$this->assertEquals($expected, $this->HttpResponse['status']['code']);
|
||||
$expected = 'OK';
|
||||
$this->assertEquals($expected, $this->HttpResponse['status']['reason-phrase']);
|
||||
|
||||
$expected = array(
|
||||
'Server' => 'CakePHP',
|
||||
'ContEnt-Type' => 'text/plain'
|
||||
);
|
||||
$this->assertEquals($expected, $this->HttpResponse['header']);
|
||||
|
||||
$expected = 'This is a test!';
|
||||
$this->assertEquals($expected, $this->HttpResponse['body']);
|
||||
|
||||
$expected = array(
|
||||
'foo' => array('value' => 'bar'),
|
||||
'bar' => array('value' => 'foo')
|
||||
);
|
||||
$this->assertEquals($expected, $this->HttpResponse['cookies']);
|
||||
|
||||
$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\n\r\nThis is a test!";
|
||||
$this->assertSame($this->HttpResponse['raw']['header'], null);
|
||||
}
|
||||
|
||||
}
|
1670
lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
Normal file
1670
lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue