MDL-32250 import latest SimpleTest 1.1.0

This commit is contained in:
Petr Skoda
2012-03-29 15:07:53 +02:00
parent a280078197
commit 0bb48cc2a1
88 changed files with 17451 additions and 5860 deletions
+223 -126
View File
@@ -3,9 +3,9 @@
* base include file for SimpleTest
* @package SimpleTest
* @subpackage WebTester
* @version $Id$
* @version $Id: encoding.php 2011 2011-04-29 08:22:48Z pp11 $
*/
/**#@+
* include other SimpleTest class files
*/
@@ -18,28 +18,28 @@ require_once(dirname(__FILE__) . '/socket.php');
* @subpackage WebTester
*/
class SimpleEncodedPair {
var $_key;
var $_value;
private $key;
private $value;
/**
* Stashes the data for rendering later.
* @param string $key Form element name.
* @param string $value Data to send.
*/
function SimpleEncodedPair($key, $value) {
$this->_key = $key;
$this->_value = $value;
function __construct($key, $value) {
$this->key = $key;
$this->value = $value;
}
/**
* The pair as a single string.
* @return string Encoded pair.
* @access public
*/
function asRequest() {
return urlencode($this->_key) . '=' . urlencode($this->_value);
return urlencode($this->key) . '=' . urlencode($this->value);
}
/**
* The MIME part as a string.
* @return string MIME part encoding.
@@ -47,11 +47,11 @@ class SimpleEncodedPair {
*/
function asMime() {
$part = 'Content-Disposition: form-data; ';
$part .= "name=\"" . $this->_key . "\"\r\n";
$part .= "\r\n" . $this->_value;
$part .= "name=\"" . $this->key . "\"\r\n";
$part .= "\r\n" . $this->value;
return $part;
}
/**
* Is this the value we are looking for?
* @param string $key Identifier.
@@ -59,25 +59,25 @@ class SimpleEncodedPair {
* @access public
*/
function isKey($key) {
return $key == $this->_key;
return $key == $this->key;
}
/**
* Is this the value we are looking for?
* @return string Identifier.
* @access public
*/
function getKey() {
return $this->_key;
return $this->key;
}
/**
* Is this the value we are looking for?
* @return string Content.
* @access public
*/
function getValue() {
return $this->_value;
return $this->value;
}
}
@@ -87,22 +87,22 @@ class SimpleEncodedPair {
* @subpackage WebTester
*/
class SimpleAttachment {
var $_key;
var $_content;
var $_filename;
private $key;
private $content;
private $filename;
/**
* Stashes the data for rendering later.
* @param string $key Key to add value to.
* @param string $content Raw data.
* @param hash $filename Original filename.
*/
function SimpleAttachment($key, $content, $filename) {
$this->_key = $key;
$this->_content = $content;
$this->_filename = $filename;
function __construct($key, $content, $filename) {
$this->key = $key;
$this->content = $content;
$this->filename = $filename;
}
/**
* The pair as a single string.
* @return string Encoded pair.
@@ -111,7 +111,7 @@ class SimpleAttachment {
function asRequest() {
return '';
}
/**
* The MIME part as a string.
* @return string MIME part encoding.
@@ -119,32 +119,32 @@ class SimpleAttachment {
*/
function asMime() {
$part = 'Content-Disposition: form-data; ';
$part .= 'name="' . $this->_key . '"; ';
$part .= 'filename="' . $this->_filename . '"';
$part .= "\r\nContent-Type: " . $this->_deduceMimeType();
$part .= "\r\n\r\n" . $this->_content;
$part .= 'name="' . $this->key . '"; ';
$part .= 'filename="' . $this->filename . '"';
$part .= "\r\nContent-Type: " . $this->deduceMimeType();
$part .= "\r\n\r\n" . $this->content;
return $part;
}
/**
* Attempts to figure out the MIME type from the
* file extension and the content.
* @return string MIME type.
* @access private
*/
function _deduceMimeType() {
if ($this->_isOnlyAscii($this->_content)) {
protected function deduceMimeType() {
if ($this->isOnlyAscii($this->content)) {
return 'text/plain';
}
return 'application/octet-stream';
}
/**
* Tests each character is in the range 0-127.
* @param string $ascii String to test.
* @access private
*/
function _isOnlyAscii($ascii) {
protected function isOnlyAscii($ascii) {
for ($i = 0, $length = strlen($ascii); $i < $length; $i++) {
if (ord($ascii[$i]) > 127) {
return false;
@@ -152,7 +152,7 @@ class SimpleAttachment {
}
return true;
}
/**
* Is this the value we are looking for?
* @param string $key Identifier.
@@ -160,25 +160,25 @@ class SimpleAttachment {
* @access public
*/
function isKey($key) {
return $key == $this->_key;
return $key == $this->key;
}
/**
* Is this the value we are looking for?
* @return string Identifier.
* @access public
*/
function getKey() {
return $this->_key;
return $this->key;
}
/**
* Is this the value we are looking for?
* @return string Content.
* @access public
*/
function getValue() {
return $this->_filename;
return $this->filename;
}
}
@@ -189,8 +189,8 @@ class SimpleAttachment {
* @subpackage WebTester
*/
class SimpleEncoding {
var $_request;
private $request;
/**
* Starts empty.
* @param array $query Hash of parameters.
@@ -198,22 +198,22 @@ class SimpleEncoding {
* as lists on a single key.
* @access public
*/
function SimpleEncoding($query = false) {
function __construct($query = false) {
if (! $query) {
$query = array();
}
$this->clear();
$this->merge($query);
}
/**
* Empties the request of parameters.
* @access public
*/
function clear() {
$this->_request = array();
$this->request = array();
}
/**
* Adds a parameter to the query.
* @param string $key Key to add value to.
@@ -226,23 +226,23 @@ class SimpleEncoding {
}
if (is_array($value)) {
foreach ($value as $item) {
$this->_addPair($key, $item);
$this->addPair($key, $item);
}
} else {
$this->_addPair($key, $value);
$this->addPair($key, $value);
}
}
/**
* Adds a new value into the request.
* @param string $key Key to add value to.
* @param string/array $value New data.
* @access private
*/
function _addPair($key, $value) {
$this->_request[] = new SimpleEncodedPair($key, $value);
protected function addPair($key, $value) {
$this->request[] = new SimpleEncodedPair($key, $value);
}
/**
* Adds a MIME part to the query. Does nothing for a
* form encoded packet.
@@ -252,9 +252,9 @@ class SimpleEncoding {
* @access public
*/
function attach($key, $content, $filename) {
$this->_request[] = new SimpleAttachment($key, $content, $filename);
$this->request[] = new SimpleAttachment($key, $content, $filename);
}
/**
* Adds a set of parameters to this query.
* @param array/SimpleQueryString $query Multiple values are
@@ -263,14 +263,14 @@ class SimpleEncoding {
*/
function merge($query) {
if (is_object($query)) {
$this->_request = array_merge($this->_request, $query->getAll());
$this->request = array_merge($this->request, $query->getAll());
} elseif (is_array($query)) {
foreach ($query as $key => $value) {
$this->add($key, $value);
}
}
}
/**
* Accessor for single value.
* @return string/array False if missing, string
@@ -280,7 +280,7 @@ class SimpleEncoding {
*/
function getValue($key) {
$values = array();
foreach ($this->_request as $pair) {
foreach ($this->request as $pair) {
if ($pair->isKey($key)) {
$values[] = $pair->getValue();
}
@@ -293,25 +293,25 @@ class SimpleEncoding {
return $values;
}
}
/**
* Accessor for listing of pairs.
* @return array All pair objects.
* @access public
*/
function getAll() {
return $this->_request;
return $this->request;
}
/**
* Renders the query string as a URL encoded
* request part.
* @return string Part of URL.
* @access protected
*/
function _encode() {
protected function encode() {
$statements = array();
foreach ($this->_request as $pair) {
foreach ($this->request as $pair) {
if ($statement = $pair->asRequest()) {
$statements[] = $statement;
}
@@ -327,7 +327,7 @@ class SimpleEncoding {
* @subpackage WebTester
*/
class SimpleGetEncoding extends SimpleEncoding {
/**
* Starts empty.
* @param array $query Hash of parameters.
@@ -335,10 +335,10 @@ class SimpleGetEncoding extends SimpleEncoding {
* as lists on a single key.
* @access public
*/
function SimpleGetEncoding($query = false) {
$this->SimpleEncoding($query);
function __construct($query = false) {
parent::__construct($query);
}
/**
* HTTP request method.
* @return string Always GET.
@@ -347,7 +347,7 @@ class SimpleGetEncoding extends SimpleEncoding {
function getMethod() {
return 'GET';
}
/**
* Writes no extra headers.
* @param SimpleSocket $socket Socket to write to.
@@ -355,7 +355,7 @@ class SimpleGetEncoding extends SimpleEncoding {
*/
function writeHeadersTo(&$socket) {
}
/**
* No data is sent to the socket as the data is encoded into
* the URL.
@@ -364,7 +364,7 @@ class SimpleGetEncoding extends SimpleEncoding {
*/
function writeTo(&$socket) {
}
/**
* Renders the query string as a URL encoded
* request part for attaching to a URL.
@@ -372,7 +372,7 @@ class SimpleGetEncoding extends SimpleEncoding {
* @access public
*/
function asUrlRequest() {
return $this->_encode();
return $this->encode();
}
}
@@ -382,7 +382,7 @@ class SimpleGetEncoding extends SimpleEncoding {
* @subpackage WebTester
*/
class SimpleHeadEncoding extends SimpleGetEncoding {
/**
* Starts empty.
* @param array $query Hash of parameters.
@@ -390,10 +390,10 @@ class SimpleHeadEncoding extends SimpleGetEncoding {
* as lists on a single key.
* @access public
*/
function SimpleHeadEncoding($query = false) {
$this->SimpleGetEncoding($query);
function __construct($query = false) {
parent::__construct($query);
}
/**
* HTTP request method.
* @return string Always HEAD.
@@ -405,13 +405,12 @@ class SimpleHeadEncoding extends SimpleGetEncoding {
}
/**
* Bundle of POST parameters. Can include
* repeated parameters.
* Bundle of URL parameters for a DELETE request.
* @package SimpleTest
* @subpackage WebTester
*/
class SimplePostEncoding extends SimpleEncoding {
class SimpleDeleteEncoding extends SimpleGetEncoding {
/**
* Starts empty.
* @param array $query Hash of parameters.
@@ -419,13 +418,103 @@ class SimplePostEncoding extends SimpleEncoding {
* as lists on a single key.
* @access public
*/
function SimplePostEncoding($query = false) {
function __construct($query = false) {
parent::__construct($query);
}
/**
* HTTP request method.
* @return string Always DELETE.
* @access public
*/
function getMethod() {
return 'DELETE';
}
}
/**
* Bundles an entity-body for transporting
* a raw content payload with the request.
* @package SimpleTest
* @subpackage WebTester
*/
class SimpleEntityEncoding extends SimpleEncoding {
private $content_type;
private $body;
function __construct($query = false, $content_type = false) {
$this->content_type = $content_type;
if (is_string($query)) {
$this->body = $query;
parent::__construct();
} else {
parent::__construct($query);
}
}
/**
* Returns the media type of the entity body
* @return string
* @access public
*/
function getContentType() {
if (!$this->content_type) {
return ($this->body) ? 'text/plain' : 'application/x-www-form-urlencoded';
}
return $this->content_type;
}
/**
* Dispatches the form headers down the socket.
* @param SimpleSocket $socket Socket to write to.
* @access public
*/
function writeHeadersTo(&$socket) {
$socket->write("Content-Length: " . (integer)strlen($this->encode()) . "\r\n");
$socket->write("Content-Type: " . $this->getContentType() . "\r\n");
}
/**
* Dispatches the form data down the socket.
* @param SimpleSocket $socket Socket to write to.
* @access public
*/
function writeTo(&$socket) {
$socket->write($this->encode());
}
/**
* Renders the request body
* @return Encoded entity body
* @access protected
*/
protected function encode() {
return ($this->body) ? $this->body : parent::encode();
}
}
/**
* Bundle of POST parameters. Can include
* repeated parameters.
* @package SimpleTest
* @subpackage WebTester
*/
class SimplePostEncoding extends SimpleEntityEncoding {
/**
* Starts empty.
* @param array $query Hash of parameters.
* Multiple values are
* as lists on a single key.
* @access public
*/
function __construct($query = false, $content_type = false) {
if (is_array($query) and $this->hasMoreThanOneLevel($query)) {
$query = $this->rewriteArrayWithMultipleLevels($query);
}
$this->SimpleEncoding($query);
parent::__construct($query, $content_type);
}
function hasMoreThanOneLevel($query) {
foreach ($query as $key => $value) {
if (is_array($value)) {
@@ -449,11 +538,10 @@ class SimplePostEncoding extends SimpleEncoding {
if ($this->hasMoreThanOneLevel($query_)) {
$query_ = $this->rewriteArrayWithMultipleLevels($query_);
}
return $query_;
}
/**
* HTTP request method.
* @return string Always POST.
@@ -462,26 +550,7 @@ class SimplePostEncoding extends SimpleEncoding {
function getMethod() {
return 'POST';
}
/**
* Dispatches the form headers down the socket.
* @param SimpleSocket $socket Socket to write to.
* @access public
*/
function writeHeadersTo(&$socket) {
$socket->write("Content-Length: " . (integer)strlen($this->_encode()) . "\r\n");
$socket->write("Content-Type: application/x-www-form-urlencoded\r\n");
}
/**
* Dispatches the form data down the socket.
* @param SimpleSocket $socket Socket to write to.
* @access public
*/
function writeTo(&$socket) {
$socket->write($this->_encode());
}
/**
* Renders the query string as a URL encoded
* request part for attaching to a URL.
@@ -494,14 +563,12 @@ class SimplePostEncoding extends SimpleEncoding {
}
/**
* Bundle of POST parameters in the multipart
* format. Can include file uploads.
* Encoded entity body for a PUT request.
* @package SimpleTest
* @subpackage WebTester
*/
class SimpleMultipartEncoding extends SimplePostEncoding {
var $_boundary;
class SimplePutEncoding extends SimpleEntityEncoding {
/**
* Starts empty.
* @param array $query Hash of parameters.
@@ -509,43 +576,73 @@ class SimpleMultipartEncoding extends SimplePostEncoding {
* as lists on a single key.
* @access public
*/
function SimpleMultipartEncoding($query = false, $boundary = false) {
$this->SimplePostEncoding($query);
$this->_boundary = ($boundary === false ? uniqid('st') : $boundary);
function __construct($query = false, $content_type = false) {
parent::__construct($query, $content_type);
}
/**
* HTTP request method.
* @return string Always PUT.
* @access public
*/
function getMethod() {
return 'PUT';
}
}
/**
* Bundle of POST parameters in the multipart
* format. Can include file uploads.
* @package SimpleTest
* @subpackage WebTester
*/
class SimpleMultipartEncoding extends SimplePostEncoding {
private $boundary;
/**
* Starts empty.
* @param array $query Hash of parameters.
* Multiple values are
* as lists on a single key.
* @access public
*/
function __construct($query = false, $boundary = false) {
parent::__construct($query);
$this->boundary = ($boundary === false ? uniqid('st') : $boundary);
}
/**
* Dispatches the form headers down the socket.
* @param SimpleSocket $socket Socket to write to.
* @access public
*/
function writeHeadersTo(&$socket) {
$socket->write("Content-Length: " . (integer)strlen($this->_encode()) . "\r\n");
$socket->write("Content-Type: multipart/form-data, boundary=" . $this->_boundary . "\r\n");
$socket->write("Content-Length: " . (integer)strlen($this->encode()) . "\r\n");
$socket->write("Content-Type: multipart/form-data; boundary=" . $this->boundary . "\r\n");
}
/**
* Dispatches the form data down the socket.
* @param SimpleSocket $socket Socket to write to.
* @access public
*/
function writeTo(&$socket) {
$socket->write($this->_encode());
$socket->write($this->encode());
}
/**
* Renders the query string as a URL encoded
* request part.
* @return string Part of URL.
* @access public
*/
function _encode() {
function encode() {
$stream = '';
foreach ($this->_request as $pair) {
$stream .= "--" . $this->_boundary . "\r\n";
foreach ($this->getAll() as $pair) {
$stream .= "--" . $this->boundary . "\r\n";
$stream .= $pair->asMime() . "\r\n";
}
$stream .= "--" . $this->_boundary . "--\r\n";
$stream .= "--" . $this->boundary . "--\r\n";
return $stream;
}
}