[limb-svn] r6414 - in 3.x/trunk/limb/web_agent: . src src/agent src/agent/liveinternet src/request tests tests/cases tests/cases/agent tests/cases/agent/liveinternet

svn at limb-project.com svn at limb-project.com
Sun Oct 14 13:12:00 MSD 2007


Author: cmz
Date: 2007-10-14 13:12:00 +0400 (Sun, 14 Oct 2007)
New Revision: 6414
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6414

Added:
   3.x/trunk/limb/web_agent/common.inc.php
   3.x/trunk/limb/web_agent/src/
   3.x/trunk/limb/web_agent/src/agent/
   3.x/trunk/limb/web_agent/src/agent/liveinternet/
   3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetAgent.class.php
   3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetValues.class.php
   3.x/trunk/limb/web_agent/src/lmbWebAgent.class.php
   3.x/trunk/limb/web_agent/src/lmbWebAgentCookieIterator.class.php
   3.x/trunk/limb/web_agent/src/lmbWebAgentCookies.class.php
   3.x/trunk/limb/web_agent/src/lmbWebAgentHeaders.class.php
   3.x/trunk/limb/web_agent/src/lmbWebAgentKit.class.php
   3.x/trunk/limb/web_agent/src/lmbWebAgentValues.class.php
   3.x/trunk/limb/web_agent/src/lmbWebServerCookie.class.php
   3.x/trunk/limb/web_agent/src/lmbWebServerCookiesCollection.class.php
   3.x/trunk/limb/web_agent/src/lmbWebServerResponse.class.php
   3.x/trunk/limb/web_agent/src/request/
   3.x/trunk/limb/web_agent/src/request/lmbAbstractWebAgentRequest.class.php
   3.x/trunk/limb/web_agent/src/request/lmbSocketWebAgentRequest.class.php
   3.x/trunk/limb/web_agent/tests/
   3.x/trunk/limb/web_agent/tests/.setup.php
   3.x/trunk/limb/web_agent/tests/cases/
   3.x/trunk/limb/web_agent/tests/cases/agent/
   3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/
   3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetAgentTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetValuesTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentCookiesTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentHeadersTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentValuesTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookieTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookiesCollectionTest.class.php
   3.x/trunk/limb/web_agent/tests/cases/lmbWebServerResponseTest.class.php
Log:
-- importing package web_agent

Added: 3.x/trunk/limb/web_agent/common.inc.php
===================================================================
--- 3.x/trunk/limb/web_agent/common.inc.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/common.inc.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,16 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package web_agent
+ * @version $Id$
+ */
+require_once('limb/core/common.inc.php');
+
+

Added: 3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetAgent.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetAgent.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetAgent.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,60 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../lmbWebAgent.class.php');
+lmb_require(dirname(__FILE__).'/lmbLiveInternetValues.class.php');
+
+/**
+ * Liveinternet agent
+ *
+ * @package web_agent
+ * @version $Id: lmbLiveInternetAgent.class.php 81 2007-10-11 15:41:36Z CatMan $
+ */
+class lmbLiveInternetAgent extends lmbWebAgent {
+
+  protected $project;
+
+  function __construct($project, $request = null)
+  {
+    parent::__construct($request);
+    $this->project = $project;
+    $this->values = new lmbLiveInternetValues();
+  }
+
+  function getProject()
+  {
+    return $this->project;
+  }
+
+  function requestStatPage($page = '')
+  {
+    $url = $this->getProjectUrl().$page;
+    $this->doRequest($url);
+  }
+
+  function auth($password)
+  {
+    $agent = new lmbWebAgent($this->request);
+    $agent->getValues()->import(
+      array(
+        'url' => 'http://'.$this->project,
+        'password' => $password,
+        'ok' => ' ok '
+      )
+    );
+    $agent->doRequest($this->getProjectUrl(), 'POST', 0);
+    $agent->getCookies()->copyTo($this->cookies);
+  }
+
+  function getProjectUrl()
+  {
+  	return 'http://www.liveinternet.ru/stat/'.$this->project.'/';
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetValues.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetValues.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/agent/liveinternet/lmbLiveInternetValues.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,39 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../lmbWebAgentValues.class.php');
+
+/**
+ * Values of Liveinternet agent
+ *
+ * @package web_agent
+ * @version $Id: lmbLiveInternetValues.class.php 89 2007-10-12 15:28:50Z CatMan $
+ */
+class lmbLiveInternetValues extends lmbWebAgentValues {
+
+  function buildQuery($encoding = 'utf-8')
+  {
+  	$vars = array();
+    foreach($this->convert($encoding) as $name => $value)
+    {
+    	if(is_array($value))
+      {
+      	foreach($value as $v)
+        {
+        	$vars[] = http_build_query(array($name => $v), '', ';');
+        }
+      }
+      else
+        $vars[] = http_build_query(array($name => $value), '', ';');
+    }
+    return implode(';', $vars);
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebAgent.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebAgent.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebAgent.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,182 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package web_agent
+ */
+lmb_require(dirname(__FILE__).'/lmbWebAgentKit.class.php');
+lmb_require(dirname(__FILE__).'/lmbWebServerCookiesCollection.class.php');
+lmb_require(dirname(__FILE__).'/lmbWebAgentValues.class.php');
+
+/**
+ * Web agent
+ *
+ * @package web_agent
+ * @version $Id: lmbWebAgent.class.php 89 2007-10-12 15:28:50Z CatMan $
+ */
+class lmbWebAgent {
+  protected $request;
+  protected $cookies;
+  protected $values = null;
+  protected $response = null;
+  protected $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7';
+  protected $content = '';
+  protected $accept_charset = 'utf-8,windows-1251;q=0.7,*;q=0.7';
+  protected $charset = 'utf-8';
+
+  function __construct($request = null)
+  {
+    $this->cookies = new lmbWebServerCookiesCollection();
+    $this->values = new lmbWebAgentValues();
+
+    if($request)
+      $this->request = $request;
+    else
+      $this->request = lmbWebAgentKit::createRequest();
+  }
+
+  function getCookies()
+  {
+  	return $this->cookies;
+  }
+
+  function getValues()
+  {
+  	return $this->values;
+  }
+
+  function getResponse()
+  {
+    return $this->response;
+  }
+
+  function getContent()
+  {
+    return $this->content;
+  }
+
+  function setUserAgent($user_agent)
+  {
+    $this->user_agent = $user_agent;
+  }
+
+  function setAcceptCharset($accept_charset)
+  {
+    $this->accept_charset = $accept_charset;
+  }
+
+  function setCharset($charset)
+  {
+    $this->charset = $charset;
+  }
+
+  function getCharset()
+  {
+    return $this->charset;
+  }
+
+  function doRequest($url, $method = 'GET', $max_redirects = 5, $content = null)
+  {
+    $this->prepareRequest($url, $method, $content);
+    $this->response = $this->request->doRequest($url, $method);
+    $this->analyzeResponse($url);
+    $this->doRedirect($max_redirects);
+  }
+
+  function doRedirect($max_redirects = 5)
+  {
+  	if(!$max_redirects || !$this->response->getHeaders()->has('location')) return;
+    $location = $this->response->getHeaders()->get('location');
+    $bu_vals = $this->values;
+    $this->values = new lmbWebAgentValues();
+    $this->doRequest($location, 'GET', $max_redirects - 1);
+    $this->values = $bu_vals;
+  }
+
+  protected function prepareRequest(&$url, $method, $content)
+  {
+    $this->request->clean();
+    $this->request->setUserAgent($this->user_agent);
+    $this->request->setAcceptCharset($this->accept_charset);
+    $this->prepareCookies($url);
+    $this->prepareContent($url, $method, $content);
+  }
+
+  protected function prepareContent(&$url, $method, $content)
+  {
+    $encoding = $this->charset ? $this->charset : 'utf-8';
+    $query = $this->values->buildQuery($encoding);
+    if($method == 'GET')
+    {
+      if($query)
+        $url .= '?'.$query;
+    }
+    elseif($method == 'POST')
+    {
+      if($content === null)
+        $content = $query;
+    }
+    if($content)
+    {
+      if($method != 'POST' && $encoding != 'utf-8')
+        $content = mb_convert_encoding($content, $encoding, 'utf-8');
+      $this->request->setContent($content);
+      if($method == 'POST')
+        $this->request->getHeaders()->set('content-type', 'application/x-www-form-urlencoded');
+    }
+  }
+
+  protected function prepareCookies($url)
+  {
+    $url = parse_url($url);
+    foreach($this->cookies as $cookie)
+    {
+      if(strpos($cookie->domain, '.') === 0)
+      {
+        if(substr($url['host'], -strlen($cookie->domain) + 1) != substr($cookie->domain, 1))
+          continue;
+      }
+      elseif($cookie->domain != $url['host'])
+        continue;
+      $path = isset($url['path']) ? $url['path'] : '/';
+      if(strpos($path, $cookie->path) !== 0)
+        continue;
+      $this->request->getCookies()->set($cookie->name, $cookie->value);
+    }
+  }
+
+  protected function analyzeResponse($url)
+  {
+    $this->analyzeCookies($url);
+    $this->analyzeContent();
+  }
+
+  protected function analyzeCookies($url)
+  {
+    $url = parse_url($url);
+    foreach($this->response->getCookies() as $cookie)
+    {
+      $newcookie = clone $cookie;
+      if(!$newcookie->domain)
+        $newcookie->domain = $url['host'];
+      if(!$newcookie->path)
+        $newcookie->path = isset($url['path']) ? $url['path'] : '/';
+      $this->getCookies()->add($newcookie);
+    }
+  }
+
+  protected function analyzeContent()
+  {
+    $this->charset = $this->response->getCharset();
+    $this->content = $this->response->getContent();
+    if($this->charset && $this->charset != 'utf-8')
+      $this->content = mb_convert_encoding($this->content, 'utf-8', $this->charset);
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebAgentCookieIterator.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebAgentCookieIterator.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebAgentCookieIterator.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,51 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * Web agent cookies
+ *
+ * @package web_agent
+ * @version $Id: lmbWebAgentCookieIterator.class.php 39 2007-10-03 21:08:36Z CatMan $
+ */
+class lmbWebAgentCookieIterator implements Iterator {
+
+  protected $cookies;
+
+  function __construct($cookies)
+  {
+    $this->cookies = $cookies;
+  }
+
+  function rewind()
+  {
+    reset($this->cookies);
+  }
+
+  function current()
+  {
+    return current($this->cookies);
+  }
+
+  function key()
+  {
+    return key($this->cookies);
+  }
+
+  function next()
+  {
+    return next($this->cookies);
+  }
+
+  function valid()
+  {
+    return $this->current() !== false;
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebAgentCookies.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebAgentCookies.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebAgentCookies.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,65 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/lmbWebAgentCookieIterator.class.php');
+
+/**
+ * Web agent cookies
+ *
+ * @package web_agent
+ * @version $Id: lmbWebAgentCookies.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebAgentCookies implements IteratorAggregate {
+
+  protected $cookies;
+
+  function __construct($cookies = array())
+  {
+    $this->cookies = $cookies;
+  }
+
+  function set($name, $value)
+  {
+    $this->cookies[$name] = $value;
+  }
+
+  function get($name)
+  {
+    return $this->has($name) ? $this->cookies[$name] : '';
+  }
+
+  function has($name)
+  {
+    return isset($this->cookies[$name]);
+  }
+
+  function hasCookies()
+  {
+    return (boolean)$this->cookies;
+  }
+
+  function export()
+  {
+    $cookies = array();
+    foreach($this->cookies as $name => $value)
+      $cookies[] = $name.'='.rawurlencode($value);
+    return implode('; ', $cookies);
+  }
+
+  function getIterator()
+  {
+    return new lmbWebAgentCookieIterator($this->cookies);
+  }
+
+  function clean()
+  {
+  	$this->cookies = array();
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebAgentHeaders.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebAgentHeaders.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebAgentHeaders.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,153 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * Web agent headers
+ *
+ * @package web_agent
+ * @version $Id: lmbWebAgentHeaders.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebAgentHeaders {
+
+  const last = -1;
+
+  protected $headers = array();
+
+  function __construct($headers = array())
+  {
+    $this->import($headers);
+  }
+
+  function set($name, $value)
+  {
+    $this->setRaw(strtolower($name), $value);
+  }
+
+  function setRaw($name, $value = null)
+  {
+    if(!$this->has($name))
+      $this->headers[$name] = array();
+    $this->headers[$name][] = $value;
+  }
+
+  function get($name, $num = self::last)
+  {
+    if(!$this->has($name))
+      return null;
+    if($num == self::last)
+      return end($this->headers[$name]);
+    if(isset($this->headers[$name][$num]))
+      return $this->headers[$name][$num];
+    return null;
+  }
+
+  function countHeaders($name)
+  {
+    if(!$this->has($name))
+      return 0;
+    return count($this->headers[$name]);
+  }
+
+  function getFirst()
+  {
+    if(!$this->headers)
+      return null;
+    $keys = array_keys($this->headers);
+    $first = $keys[0];
+    $value = $this->get($first);
+    if($value === null)
+      return $first;
+    return $value;
+  }
+
+  function has($name)
+  {
+    return isset($this->headers[$name]);
+  }
+
+  function import($headers)
+  {
+  	foreach($headers as $name => $value)
+    {
+      if($value === null)
+        $this->setRaw($name);
+      else
+        $this->set($name, $value);
+    }
+  }
+
+  function parse($header)
+  {
+    $header = trim($header);
+    $expl = explode(':', $header, 2);
+    $name = trim($expl[0]);
+    if(!$name) return false;
+    if(count($expl) > 1)
+      $this->set($name, trim($expl[1]));
+    else
+      $this->setRaw($name);
+    return true;
+  }
+
+  function exportHeader($name, $num = self::last)
+  {
+    $value = $this->get($name, $num);
+    if($value === null) return null;
+    $name = self::normalizeName($name);
+    return $name.': '.$value;
+  }
+
+  function exportHeaders()
+  {
+    $result = array();
+    $names = array_keys($this->headers);
+    foreach($names as $name)
+    {
+    	$n = 0;
+      while(($header = $this->exportHeader($name, $n ++)) !== null)
+      {
+      	$result[] = $header;
+      }
+      if($n == 1)
+        $result[] = $name;
+    }
+    return implode("\r\n", $result)."\r\n";
+  }
+
+  function remove($name)
+  {
+    if($this->has($name))
+      unset($this->headers[$name]);
+  }
+
+  function copyTo(lmbWebAgentHeaders $dest)
+  {
+    foreach($this->headers as $name => $values)
+    {
+    	foreach($values as $value)
+        $dest->setRaw($name, $value);
+    }
+  }
+
+  function clean()
+  {
+    $this->headers = array();
+  }
+
+  static function normalizeName($name)
+  {
+  	$new_name = array();
+    foreach(explode('-', $name) as $piece)
+    {
+    	$new_name[] = ucfirst($piece);
+    }
+    return implode('-', $new_name);
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebAgentKit.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebAgentKit.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebAgentKit.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,32 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * Web agent kit
+ *
+ * @package web_agent
+ * @version $Id: lmbWebAgentKit.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebAgentKit {
+
+  function createRequest($req = 'socket')
+  {
+    if(defined('LIMB_WEB_AGENT_REQUEST'))
+      $req = LIMB_WEB_AGENT_REQUEST;
+
+    $class = 'lmb'.ucfirst($req).'WebAgentRequest';
+
+    $class_path = dirname(__FILE__).'/request/'.$class.'.class.php';
+    lmb_require($class_path);
+
+    return new $class;
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebAgentValues.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebAgentValues.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebAgentValues.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,37 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/core/src/lmbObject.class.php');
+
+/**
+ * Web agent values
+ *
+ * @package web_agent
+ * @version $Id: lmbWebAgentValues.class.php 89 2007-10-12 15:28:50Z CatMan $
+ */
+class lmbWebAgentValues extends lmbObject {
+
+  function buildQuery($encoding = 'utf-8')
+  {
+  	return http_build_query($this->convert($encoding));
+  }
+
+  function convert($encoding)
+  {
+    $vars = $this->_getObjectVars();
+    if($encoding != 'utf-8')
+    {
+      foreach($vars as &$var)
+        $var = mb_convert_encoding($var, $encoding, 'utf-8');
+    }
+    return $vars;
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebServerCookie.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebServerCookie.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebServerCookie.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,78 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * Web agent cookie
+ *
+ * @package web_agent
+ * @version $Id: lmbWebServerCookie.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebServerCookie {
+  protected $vars = array(
+    'name' => '',
+    'value' => '',
+    'expires' => '',
+    'path' => '',
+    'domain' => '',
+    'secure' => false,
+  );
+
+  function __construct($server_cookie)
+  {
+    $this->parse($server_cookie);
+  }
+
+  protected function parse($server_cookie)
+  {
+    $cook_params = explode(';', $server_cookie);
+    if(!$cook_params[0]) continue;
+    $cookie_name = '';
+    foreach($cook_params as $n => $param)
+    {
+      $expl = explode('=', trim($param), 2);
+      $name = $expl[0];
+      $value = isset($expl[1]) ? $expl[1] : '';
+      if($n == 0)
+      {
+        $this->name = $name;
+        $this->value = rawurldecode($value);
+      }
+      else
+      {
+        switch($name)
+        {
+          case 'expires':
+            $this->expires = $value;
+            break;
+          case 'path':
+            $this->path = $value;
+            break;
+          case 'domain':
+            $this->domain = $value;
+            break;
+          case 'secure':
+            $this->secure = true;
+            break;
+        }
+      }
+    }
+  }
+
+  function __get($name)
+  {
+    return $this->vars[$name];
+  }
+
+  function __set($name, $value)
+  {
+    $this->vars[$name] = $value;
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebServerCookiesCollection.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebServerCookiesCollection.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebServerCookiesCollection.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,62 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/lmbWebServerCookie.class.php');
+lmb_require(dirname(__FILE__).'/lmbWebAgentCookieIterator.class.php');
+
+/**
+ * Web server cookies collection
+ *
+ * @package web_agent
+ * @version $Id: lmbWebServerCookiesCollection.class.php 43 2007-10-05 15:33:11Z CatMan $
+ */
+class lmbWebServerCookiesCollection implements IteratorAggregate {
+
+  protected $cookies = array();
+
+  function add(lmbWebServerCookie $cookie)
+  {
+    $num = $this->search($cookie->name, $cookie->path, $cookie->domain);
+    if($num === false)
+      $this->cookies[] = $cookie;
+    else
+      $this->cookies[$num] = $cookie;
+  }
+
+  function search($name, $path = '', $domain = '')
+  {
+    foreach($this->cookies as $n => $cookie)
+    {
+      if($cookie->name == $name && $cookie->path == $path && $cookie->domain == $domain)
+        return $n;
+    }
+    return false;
+  }
+
+  function get($num)
+  {
+    return isset($this->cookies[$num]) ? $this->cookies[$num] : false;
+  }
+
+  function getIterator()
+  {
+    return new lmbWebAgentCookieIterator($this->cookies);
+  }
+
+  function copyTo(lmbWebServerCookiesCollection $dest)
+  {
+    foreach($this->cookies as $cookie)
+    {
+    	$new_cookie = clone $cookie;
+      $dest->add($new_cookie);
+    }
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/lmbWebServerResponse.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/lmbWebServerResponse.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/lmbWebServerResponse.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,68 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/lmbWebServerCookiesCollection.class.php');
+lmb_require(dirname(__FILE__).'/lmbWebAgentHeaders.class.php');
+
+/**
+ * Web server response
+ *
+ * @package web_agent
+ * @version $Id: lmbWebServerResponse.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebServerResponse {
+  protected $content;
+  protected $status;
+  protected $mediatype;
+  protected $charset;
+  protected $headers;
+  protected $cookies;
+
+  function __construct($content, $status, $mediatype, $charset, lmbWebServerCookiesCollection $cookies, lmbWebAgentHeaders $headers)
+  {
+    $this->content = $content;
+    $this->status = $status;
+    $this->mediatype = $mediatype;
+    $this->charset = $charset;
+    $this->headers = $headers;
+    $this->cookies = $cookies;
+  }
+
+  function getContent()
+  {
+    return $this->content;
+  }
+
+  function getStatus()
+  {
+    return $this->status;
+  }
+
+  function getMediaType()
+  {
+    return $this->mediatype;
+  }
+
+  function getCharset()
+  {
+    return $this->charset;
+  }
+
+  function getCookies()
+  {
+    return $this->cookies;
+  }
+
+  function getHeaders()
+  {
+    return $this->headers;
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/src/request/lmbAbstractWebAgentRequest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/request/lmbAbstractWebAgentRequest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/request/lmbAbstractWebAgentRequest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,70 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../lmbWebAgentCookies.class.php');
+lmb_require(dirname(__FILE__).'/../lmbWebAgentHeaders.class.php');
+
+/**
+ * Abstract class of webagent request
+ *
+ * @package web_agent
+ * @version $Id: lmbAbstractWebAgentRequest.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+abstract class lmbAbstractWebAgentRequest
+{
+  protected $cookies;
+  protected $user_agent = '';
+  protected $accept_charset = '';
+  protected $headers;
+  protected $content = '';
+
+  function __construct()
+  {
+  	$this->cookies = new lmbWebAgentCookies();
+    $this->headers = new lmbWebAgentHeaders();
+  }
+
+  function getCookies()
+  {
+  	return $this->cookies;
+  }
+
+  function setUserAgent($user_agent)
+  {
+    $this->user_agent = $user_agent;
+  }
+
+  function setAcceptCharset($accept_charset)
+  {
+    $this->accept_charset = $accept_charset;
+  }
+
+  function getHeaders()
+  {
+  	return $this->headers;
+  }
+
+  function setContent($content)
+  {
+    $this->content = $content;
+  }
+
+  function clean()
+  {
+    $this->cookies->clean();
+    $this->user_agent = '';
+    $this->accept_charset = '';
+    $this->headers->clean();
+    $this->content = '';
+  }
+
+  abstract function doRequest($url, $method = 'GET');
+
+}
+?>

Added: 3.x/trunk/limb/web_agent/src/request/lmbSocketWebAgentRequest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/src/request/lmbSocketWebAgentRequest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/src/request/lmbSocketWebAgentRequest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,236 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/lmbAbstractWebAgentRequest.class.php');
+lmb_require(dirname(__FILE__).'/../lmbWebServerResponse.class.php');
+lmb_require(dirname(__FILE__).'/../lmbWebAgentCookie.class.php');
+lmb_require(dirname(__FILE__).'/../lmbWebAgentHeaders.class.php');
+lmb_require(dirname(__FILE__).'/../lmbWebServerCookiesCollection.class.php');
+
+/**
+ * Web request with sockets
+ *
+ * @package web_agent
+ * @version $Id: lmbSocketWebAgentRequest.class.php 89 2007-10-12 15:28:50Z CatMan $
+ */
+class lmbSocketWebAgentRequest extends lmbAbstractWebAgentRequest {
+  protected $request_data = '';
+  protected $request_headers = null;
+  protected $request_method = '';
+  protected $parsed_url = null;
+  protected $response_data = array();
+  protected $response_headers = null;
+
+  protected function prepareRequestData($url, $method)
+  {
+    $this->setRequestMethod($method);
+    $this->setRequestUrl($url);
+    $this->initRequestData();
+    $this->prepareCookies();
+    $this->prepareUserAgent();
+    $this->prepareAcceptCharset();
+    $this->prepareHeaders();
+    $this->prepareContent();
+    $this->assembleRequestData();
+  }
+
+  protected function setRequestMethod($method)
+  {
+    $this->request_method = strtoupper($method);
+  }
+
+  protected function setRequestUrl($url)
+  {
+    $this->parsed_url = parse_url($url);
+  }
+
+  protected function getRequestHostWithPort()
+  {
+    $host = $this->getRequestHost();
+    $port = $this->getRequestPort();
+    if($port != 80) $host .= ':'.$port;
+    return $host;
+  }
+
+  protected function getRequestPort()
+  {
+  	return isset($this->parsed_url['port']) ? $this->parsed_url['port'] : 80;
+  }
+
+  protected function getRequestHost()
+  {
+  	return $this->parsed_url['host'];
+  }
+
+  protected function getRequestPath()
+  {
+    $path = '/';
+    if(isset($this->parsed_url['path']))
+      $path = $this->parsed_url['path'];
+    if(isset($this->parsed_url['query']))
+      $path .= '?'.$this->parsed_url['query'];
+    return $path;
+  }
+
+  protected function initRequestData()
+  {
+  	$this->request_headers = new lmbWebAgentHeaders();
+    $this->request_headers->setRaw(
+      $this->request_method.' '.$this->getRequestPath().' HTTP/1.1'
+    );
+    $this->addHeader('host', $this->getRequestHostWithPort());
+    $this->addHeader('connection', 'close');
+  }
+
+  protected function addHeader($name, $value)
+  {
+  	$this->request_headers->set($name, $value);
+  }
+
+  protected function prepareCookies()
+  {
+    if($this->cookies->hasCookies())
+      $this->addHeader('Cookie', $this->cookies->export());
+  }
+
+  protected function prepareUserAgent()
+  {
+    if($this->user_agent)
+      $this->addHeader('User-Agent', $this->user_agent);
+  }
+
+  protected function prepareAcceptCharset()
+  {
+    if($this->accept_charset)
+      $this->addHeader('Accept-Charset', $this->accept_charset);
+  }
+
+  protected function prepareHeaders()
+  {
+    $this->headers->copyTo($this->request_headers);
+  }
+
+  protected function prepareContent()
+  {
+    if($this->content)
+      $this->addHeader('Content-length', strlen($this->content));
+  }
+
+  protected function assembleRequestData()
+  {
+    $this->request_data = $this->request_headers->exportHeaders()."\r\n".$this->content;
+  }
+
+  function doRequest($url, $method = 'GET')
+  {
+    $this->prepareRequestData($url, $method);
+    //echo '<pre>', $this->request_data, '</pre>';
+    if($this->readData())
+    {
+      $headers =  $this->readHeaders();
+      $status = $this->readStatus();
+      $mediatype = $this->readMediaType();
+      $charset = $this->readCharset();
+      $cookies = $this->readCookies();
+      $content = $this->readContent();
+      /*ini_set('xdebug.var_display_max_depth', 4 );
+      var_dump(
+        $status,
+        $mediatype,
+        $charset,
+        $cookies,
+        $headers
+       );
+      echo $content;*/
+      return new lmbWebServerResponse(
+        $content,
+        $status,
+        $mediatype,
+        $charset,
+        $cookies,
+        $headers
+      );
+    }
+    else
+    {
+      return new lmbWebServerResponse('', 400, '', '', new lmbWebServerCookiesCollection(), new lmbWebAgentHeaders());
+    }
+  }
+
+  protected function readData()
+  {
+    if($fp = fsockopen($this->getRequestHost(), $this->getRequestPort()))
+    {
+      fwrite($fp, $this->request_data);
+      $this->response_data = array();
+      while(!feof($fp))
+      	$this->response_data[] = fgets($fp);
+      fclose($fp);
+      return true;
+    }
+    else
+      return false;
+  }
+
+  protected function readHeaders()
+  {
+    $this->response_headers = new lmbWebAgentHeaders();
+  	while($header = trim(array_shift($this->response_data))) {
+      $this->response_headers->parse($header);
+  	}
+    return $this->response_headers;
+  }
+
+  protected function readStatus()
+  {
+    $first = $this->response_headers->getFirst();
+    if(!$first) return 400;
+    if(!preg_match('#^HTTP/\S+[ ]+([0-9]+)#', $first, $a))
+      return false;
+    return $a[1];
+  }
+
+  protected function getContentTypeHeader()
+  {
+    return strtolower($this->response_headers->get('content-type'));
+  }
+
+  protected function readMediaType()
+  {
+    $content_type = $this->getContentTypeHeader();
+    if(!$content_type)
+      return 'text/html';
+    return substr($content_type, 0, strpos($content_type, ';'));
+  }
+
+  protected function readCharset()
+  {
+    $content_type = $this->getContentTypeHeader();
+    if(!$content_type)
+      return 'utf-8';
+    return substr($content_type, strpos($content_type, 'charset=') + 8);
+  }
+
+  protected function readCookies()
+  {
+    $cookies = new lmbWebServerCookiesCollection();
+    $n = 0;
+    while(($value = $this->response_headers->get('set-cookie', $n ++)) !== null)
+    {
+    	$cookies->add(new lmbWebServerCookie($value));
+    }
+    return $cookies;
+  }
+
+  protected function readContent()
+  {
+    return implode('', $this->response_data);
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/.setup.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/.setup.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/.setup.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,58 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/web_agent/src/request/lmbAbstractWebAgentRequest.class.php');
+lmb_require('limb/web_agent/src/lmbWebServerResponse.class.php');
+lmb_require('limb/web_agent/src/lmbWebAgentCookies.class.php');
+lmb_require('limb/web_agent/src/lmbWebServerCookiesCollection.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: .setup.php 43 2007-10-05 15:33:11Z CatMan $
+ */
+class lmbFakeWebAgentRequest extends lmbAbstractWebAgentRequest {
+
+  public $response_content = '';
+  public $response_status = 200;
+  public $response_mediatype = '';
+  public $response_charset = '';
+  public $response_headers;
+  public $response_cookies;
+
+  public $request_cookies;
+  public $request_content = '';
+  public $request_url = '';
+  public $request_accept_charset = '';
+
+  function __construct()
+  {
+    parent::__construct();
+    $this->response_headers = new lmbWebAgentHeaders();
+    $this->response_cookies = new lmbWebServerCookiesCollection();
+  }
+
+  function doRequest($url, $method = 'GET')
+  {
+    $this->request_cookies = clone $this->cookies;
+    $this->request_content = $this->content;
+    $this->request_accept_charset = $this->accept_charset;
+    $this->request_url = $url;
+    return new lmbWebServerResponse(
+      $this->response_content,
+      $this->response_status,
+      $this->response_mediatype,
+      $this->response_charset,
+      $this->response_cookies,
+      $this->response_headers
+    );
+  }
+
+}
+
+?>

Added: 3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetAgentTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetAgentTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetAgentTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,64 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package web_agent
+ */
+lmb_require('limb/web_agent/src/agent/liveinternet/lmbLiveInternetAgent.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbLiveInternetAgentTest.class.php 89 2007-10-12 15:28:50Z CatMan $
+ */
+class lmbLiveInternetAgentTest extends UnitTestCase {
+  protected $agent;
+  protected $request;
+
+  function setUp()
+  {
+    $this->request = new lmbFakeWebAgentRequest();
+  	$this->agent = new lmbLiveInternetAgent('test.ru', $this->request);
+  }
+
+  function testGetProject()
+  {
+    $this->assertEqual($this->agent->getProject(), 'test.ru');
+  }
+
+  function testGetValues()
+  {
+    $arr = array(
+      'test' => 'val',
+      'test1' => 'val1',
+      'id' => array(9,7,5,0));
+    $vals = $this->agent->getValues();
+    $vals->import($arr);
+
+    $this->assertEqual($vals->buildQuery(), 'test=val;test1=val1;id=9;id=7;id=5;id=0');
+  }
+
+  function testRequestStatPage()
+  {
+  	$this->agent->requestStatPage('visitors.html');
+
+    $this->assertEqual($this->request->request_url, 'http://www.liveinternet.ru/stat/test.ru/visitors.html');
+  }
+
+  function testAuth()
+  {
+    $this->request->response_cookies->add(new lmbWebServerCookie('sid=zxc'));
+  	$this->agent->auth('***');
+
+    $this->assertEqual($this->request->request_url, 'http://www.liveinternet.ru/stat/test.ru/');
+    $this->assertEqual($this->request->request_content,
+      http_build_query(array('url' => 'http://test.ru', 'password' => '***', 'ok' => ' ok ')));
+    $this->assertEqual($this->agent->getCookies()->get(0)->value, 'zxc');
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetValuesTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetValuesTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/agent/liveinternet/lmbLiveInternetValuesTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,30 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/web_agent/src/agent/liveinternet/lmbLiveInternetValues.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbLiveInternetValuesTest.class.php 43 2007-10-05 15:33:11Z CatMan $
+ */
+class lmbLiveInternetValuesTest extends UnitTestCase {
+
+  function testBuildQuery()
+  {
+    $arr = array(
+      'test' => 'val',
+      'test1' => 'val1',
+      'id' => array(9,7,5,0));
+    $vals = new lmbLiveInternetValues($arr);
+
+    $this->assertEqual($vals->buildQuery(), 'test=val;test1=val1;id=9;id=7;id=5;id=0');
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentCookiesTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentCookiesTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentCookiesTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,77 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/web_agent/src/lmbWebAgentCookies.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbWebAgentCookiesTest.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebAgentCookiesTest extends UnitTestCase {
+
+  function testGetSetCookie()
+  {
+    $cookies = new lmbWebAgentCookies(array('test1' => 'val1', 'test2' => 'val2'));
+
+    $this->assertEqual($cookies->get('test1'), 'val1');
+    $this->assertEqual($cookies->get('test2'), 'val2');
+
+    $cookies->set('cookie3', 'val3');
+    $this->assertEqual($cookies->get('cookie3'), 'val3');
+  }
+
+  function testHasCookie()
+  {
+    $cookies = new lmbWebAgentCookies(array('test1' => 'val1', 'test2' => 'val2'));
+
+    $this->assertTrue($cookies->has('test1'));
+    $this->assertFalse($cookies->has('cookie3'));
+  }
+
+  function testIteration()
+  {
+    $cookies = new lmbWebAgentCookies(array('test1' => 'val1', 'test2' => 'val2'));
+
+    $n = 1;
+    foreach($cookies as $name => $val)
+    {
+      $this->assertEqual($name, 'test'.$n);
+      $this->assertEqual($val, 'val'.$n);
+      $n ++;
+    }
+    $this->assertEqual($n, 3);
+  }
+
+  function testClean()
+  {
+    $cookies = new lmbWebAgentCookies(array('test1' => 'val1', 'test2' => 'val2'));
+
+    $cookies->clean();
+    $this->assertFalse($cookies->has('test1'));
+    $this->assertFalse($cookies->has('test2'));
+  }
+
+  function testExport()
+  {
+    $cookies = new lmbWebAgentCookies(array('test1' => 'val1', 'test2' => 'val2'));
+
+    $this->assertEqual($cookies->export(), 'test1=val1; test2=val2');
+  }
+
+
+  function testHasCookies()
+  {
+    $cookies = new lmbWebAgentCookies();
+
+    $this->assertFalse($cookies->hasCookies());
+    $cookies->set('cookie3', 'val3');
+    $this->assertTrue($cookies->hasCookies());
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentHeadersTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentHeadersTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentHeadersTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,155 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/web_agent/src/lmbWebAgentHeaders.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbWebAgentHeadersTest.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebAgentHeadersTest extends UnitTestCase {
+
+  protected function _getHeaders()
+  {
+    return new lmbWebAgentHeaders(array('GET / HTTP/1.1' => null, 'Host' => 'test.ru'));
+  }
+
+  function testGetSet()
+  {
+    $headers = $this->_getHeaders();
+
+    $this->assertEqual($headers->get('host'), 'test.ru');
+
+    $headers->set('User-Agent', 'TestAgent');
+    $this->assertEqual($headers->get('user-agent'), 'TestAgent');
+  }
+
+  function testSetRaw()
+  {
+    $headers = $this->_getHeaders();
+
+    $headers->setRaw('Content-MD5', 'zxc');
+    $this->assertEqual($headers->get('Content-MD5'), 'zxc');
+  }
+
+  function testGetFirst()
+  {
+    $headers = $this->_getHeaders();
+
+    $this->assertEqual($headers->getFirst(), 'GET / HTTP/1.1');
+  }
+
+  function testHasHeader()
+  {
+    $headers = $this->_getHeaders();
+
+    $this->assertTrue($headers->has('host'));
+    $this->assertFalse($headers->has('user-agent'));
+  }
+
+  function testClean()
+  {
+    $headers = $this->_getHeaders();
+
+    $this->assertTrue($headers->has('host'));
+    $headers->clean();
+    $this->assertFalse($headers->has('host'));
+  }
+
+  function testCountHeaders()
+  {
+    $headers = $this->_getHeaders();
+
+    $this->assertEqual($headers->countHeaders('host'), 1);
+    $this->assertEqual($headers->countHeaders('user-agent'), 0);
+
+    $headers->set('Set-Cookie', 'sid=1');
+    $headers->set('Set-Cookie', 'sid2=2');
+    $this->assertEqual($headers->countHeaders('set-cookie'), 2);
+  }
+
+  function testGetByNum()
+  {
+    $headers = $this->_getHeaders();
+
+    $this->assertEqual($headers->get('host', 0), 'test.ru');
+    $this->assertNull($headers->get('host', 1));
+
+    $this->assertNull($headers->get('user-agent', 0));
+
+    $headers->set('Set-Cookie', 'sid=1');
+    $headers->set('Set-Cookie', 'sid2=2');
+    $this->assertEqual($headers->get('set-cookie', 0), 'sid=1');
+    $this->assertEqual($headers->get('set-cookie', 1), 'sid2=2');
+    $this->assertNull($headers->get('set-cookie', 3));
+  }
+
+  function testParse()
+  {
+  	$headers = new lmbWebAgentHeaders();
+
+    $this->assertTrue($headers->parse("GET / HTTP/1.1\r\n"));
+    $this->assertTrue($headers->parse('Host: test.ru'));
+    $this->assertTrue($headers->parse('Set-Cookie: sid=1'));
+    $this->assertTrue($headers->parse('Set-Cookie: sid2=2'));
+    $this->assertFalse($headers->parse("\r\n"));
+
+    $this->assertEqual($headers->getFirst(), 'GET / HTTP/1.1');
+    $this->assertEqual($headers->get('host'), 'test.ru');
+    $this->assertEqual($headers->get('set-cookie', 0), 'sid=1');
+    $this->assertEqual($headers->get('set-cookie', 1), 'sid2=2');
+  }
+
+  function testExportHeader()
+  {
+    $headers = $this->_getHeaders();
+
+    $headers->set('Set-Cookie', 'sid=1');
+    $headers->set('Set-Cookie', 'sid2=2');
+    $this->assertEqual($headers->exportHeader('host'), 'Host: test.ru');
+    $this->assertEqual($headers->exportHeader('set-cookie', 0), 'Set-Cookie: sid=1');
+    $this->assertEqual($headers->exportHeader('set-cookie', 1), 'Set-Cookie: sid2=2');
+    $this->assertNull($headers->exportHeader('set-cookie', 3));
+  }
+
+  function testExportHeaders()
+  {
+    $headers = $this->_getHeaders();
+    $headers->set('Set-Cookie', 'sid=1');
+    $headers->set('Set-Cookie', 'sid2=2');
+
+    $str = $headers->exportHeaders();
+    $this->assertEqual($str, "GET / HTTP/1.1\r\nHost: test.ru\r\nSet-Cookie: sid=1\r\nSet-Cookie: sid2=2\r\n");
+  }
+
+  function testRemove()
+  {
+    $headers = $this->_getHeaders();
+    $headers->set('User-Agent', 'TestAgent');
+
+    $this->assertTrue($headers->has('host'));
+    $headers->remove('host');
+    $this->assertFalse($headers->has('host'));
+    $this->assertEqual($headers->get('user-agent'), 'TestAgent');
+  }
+
+  function testCopyTo()
+  {
+    $headers = $this->_getHeaders();
+    $headers->set('Set-Cookie', 'sid=1');
+    $headers->set('Set-Cookie', 'sid2=2');
+
+    $headers_dest = new lmbWebAgentHeaders();
+    $headers->copyTo($headers_dest);
+    $str = $headers_dest->exportHeaders();
+    $this->assertEqual($str, "GET / HTTP/1.1\r\nHost: test.ru\r\nSet-Cookie: sid=1\r\nSet-Cookie: sid2=2\r\n");
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,109 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package web_agent
+ */
+lmb_require('limb/web_agent/src/lmbWebAgent.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbWebAgentTest.class.php 43 2007-10-05 15:33:11Z CatMan $
+ */
+class lmbWebAgentTest extends UnitTestCase {
+
+  function testSavePageCookie()
+  {
+    $request = new lmbFakeWebAgentRequest();
+    $agent = new lmbWebAgent($request);
+    $request->response_cookies->add(new lmbWebServerCookie('sid=12345'));
+
+    $agent->doRequest('http://test.ru');
+    $agent->doRequest('http://test.ru');
+    $this->assertEqual($request->request_cookies->get('sid'), '12345');
+    $agent->doRequest('http://test2.ru');
+    $this->assertFalse($request->request_cookies->has('sid'));
+  }
+
+  function testSaveDomainCookie()
+  {
+    $request = new lmbFakeWebAgentRequest();
+    $agent = new lmbWebAgent($request);
+    $request->response_cookies->add(new lmbWebServerCookie('sid=12345; domain=.test.ru'));
+
+    $agent->doRequest('http://test.ru');
+    $agent->doRequest('http://test.ru');
+    $this->assertEqual($request->request_cookies->get('sid'), '12345');
+    $agent->doRequest('http://sub.test.ru');
+    $this->assertEqual($request->request_cookies->get('sid'), '12345');
+    $agent->doRequest('http://test2.ru');
+    $this->assertFalse($request->request_cookies->has('sid'));
+  }
+
+  function testSavePathCookie()
+  {
+    $request = new lmbFakeWebAgentRequest();
+    $agent = new lmbWebAgent($request);
+    $request->response_cookies->add(new lmbWebServerCookie('sid=12345; path=/test/'));
+
+    $agent->doRequest('http://test.ru');
+    $agent->doRequest('http://test.ru/test/index.php');
+    $this->assertEqual($request->request_cookies->get('sid'), '12345');
+    $agent->doRequest('http://test.ru');
+    $this->assertFalse($request->request_cookies->has('sid'));
+  }
+
+  function testResponseContent()
+  {
+    $request = new lmbFakeWebAgentRequest();
+    $agent = new lmbWebAgent($request);
+    $request->response_content = 'test content';
+
+    $agent->doRequest('http://test.ru');
+    $this->assertEqual($agent->getContent(), 'test content');
+  }
+
+  function testSendValues()
+  {
+    $request = new lmbFakeWebAgentRequest();
+    $agent = new lmbWebAgent($request);
+    $values = $agent->getValues();
+    $values->setName1('value1');
+    $values->setName2('value2');
+    $http_vals = http_build_query(array('name1' => 'value1', 'name2' => 'value2'));
+
+    $agent->doRequest('http://test.ru');
+    $this->assertTrue(strpos($request->request_url, $http_vals));
+    $agent->doRequest('http://test.ru', 'POST');
+    $this->assertEqual($request->request_content, $http_vals);
+  }
+
+  function testAcceptCharset()
+  {
+    $request = new lmbFakeWebAgentRequest();
+    $agent = new lmbWebAgent($request);
+    $agent->setAcceptCharset('utf-8');
+
+    $agent->doRequest('http://test.ru');
+    $this->assertEqual($request->request_accept_charset, 'utf-8');
+  }
+
+  function testRedirect()
+  {
+    $request = new lmbFakeWebAgentRequest();
+    $agent = new lmbWebAgent($request);
+    $agent->getValues()->setTest('1');
+    $request->response_headers->set('location', 'http://redirect.ru');
+
+    $agent->doRequest('http://test.ru');
+    $this->assertEqual($request->request_url, 'http://redirect.ru');
+    $this->assertEqual($agent->getValues()->getTest(), '1');
+  }
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentValuesTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentValuesTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/lmbWebAgentValuesTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,27 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/web_agent/src/lmbWebAgentValues.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbWebAgentValuesTest.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebAgentValuesTest extends UnitTestCase {
+
+  function testBuildQuery()
+  {
+    $arr = array('test' => 'val', 'test1' => 'val1');
+  	$vals = new lmbWebAgentValues($arr);
+
+    $this->assertEqual($vals->buildQuery(), http_build_query($arr));
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookieTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookieTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookieTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,31 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/web_agent/src/lmbWebServerCookie.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbWebServerCookieTest.class.php 39 2007-10-03 21:08:36Z CatMan $
+ */
+class lmbWebServerCookieTest extends UnitTestCase {
+
+  function testCookie()
+  {
+    $cookie = new lmbWebServerCookie('sid=xzxsd; expires=date; path=/; domain=.test.ru; secure');
+
+    $this->assertEqual($cookie->name, 'sid');
+    $this->assertEqual($cookie->value, 'xzxsd');
+    $this->assertEqual($cookie->expires, 'date');
+    $this->assertEqual($cookie->path, '/');
+    $this->assertEqual($cookie->domain, '.test.ru');
+    $this->assertTrue($cookie->secure);
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookiesCollectionTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookiesCollectionTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerCookiesCollectionTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,122 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/web_agent/src/lmbWebServerCookiesCollection.class.php');
+
+/**
+ * @package web_agent
+ * @version $Id: lmbWebServerCookiesCollectionTest.class.php 43 2007-10-05 15:33:11Z CatMan $
+ */
+class lmbWebServerCookiesCollectionTest extends UnitTestCase {
+
+  function testAdd()
+  {
+    $collect = new lmbWebServerCookiesCollection();
+    $cookies = array(
+      new lmbWebServerCookie('sid=sid1; expires=date; path=/; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid=sid2; expires=date; path=/; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid=sid2; expires=date; path=/sub; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid2=sid2; expires=date; path=/; domain=.test.ru; secure')
+    );
+    foreach($cookies as $cookie)
+    {
+      $collect->add($cookie);
+    }
+    $it = $collect->getIterator();
+    $it->rewind();
+    $cookie = $it->current();
+    $this->assertIdentical($cookies[1], $cookie);
+    $it->next();
+    $cookie = $it->current();
+    $this->assertIdentical($cookies[2], $cookie);
+    $it->next();
+    $cookie = $it->current();
+    $this->assertIdentical($cookies[3], $cookie);
+  }
+
+  function testSearchCookie()
+  {
+    $collect = new lmbWebServerCookiesCollection();
+    $cookies = array(
+      new lmbWebServerCookie('sid=sid1; expires=date; path=/; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid=sid2; expires=date; path=/sub; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid2=sid2; expires=date; path=/; domain=.test.ru; secure')
+    );
+    foreach($cookies as $cookie)
+    {
+      $collect->add($cookie);
+    }
+
+    $this->assertEqual($collect->search('sid', '/sub', '.test.ru'), 1);
+    $this->assertFalse($collect->search('sid', '/sub', '.test1.ru'));
+    $this->assertFalse($collect->search('sid1'));
+  }
+
+  function testGet()
+  {
+    $collect = new lmbWebServerCookiesCollection();
+    $cookies = array(
+      new lmbWebServerCookie('sid=sid1; expires=date; path=/; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid=sid2; expires=date; path=/sub; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid2=sid3; expires=date; path=/; domain=.test.ru; secure')
+    );
+    foreach($cookies as $cookie)
+    {
+      $collect->add($cookie);
+    }
+
+    $this->assertEqual($collect->get(1)->value, 'sid2');
+    $this->assertFalse($collect->get(3));
+  }
+
+  function testCopyTo()
+  {
+    $collect = new lmbWebServerCookiesCollection();
+    $cookies = array(
+      new lmbWebServerCookie('sid=sid1; expires=date; path=/; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid=sid2; expires=date; path=/sub; domain=.test.ru; secure'),
+      new lmbWebServerCookie('sid2=sid2; expires=date; path=/; domain=.test.ru; secure')
+    );
+    foreach($cookies as $cookie)
+    {
+      $collect->add($cookie);
+    }
+    $collect2 = new lmbWebServerCookiesCollection();
+    $cookies2 = array(
+      new lmbWebServerCookie('sid=sid2; expires=date; path=/; domain=.test.ru; secure'),
+    );
+    foreach($cookies2 as $cookie)
+    {
+      $collect2->add($cookie);
+    }
+
+    $collect->copyTo($collect2);
+    $it = $collect2->getIterator();
+    $it->rewind();
+    $cookie = $it->current();
+    $this->assertClone($cookies[0], $cookie);
+    $this->assertEqual($cookie->name, 'sid');
+    $this->assertEqual($cookie->value, 'sid1');
+    $this->assertEqual($cookie->path, '/');
+    $it->next();
+    $cookie = $it->current();
+    $this->assertClone($cookies[1], $cookie);
+    $this->assertEqual($cookie->name, 'sid');
+    $this->assertEqual($cookie->value, 'sid2');
+    $this->assertEqual($cookie->path, '/sub');
+    $it->next();
+    $cookie = $it->current();
+    $this->assertClone($cookies[2], $cookie);
+    $this->assertEqual($cookie->name, 'sid2');
+    $this->assertEqual($cookie->value, 'sid2');
+    $this->assertEqual($cookie->path, '/');
+  }
+
+}
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerResponseTest.class.php
===================================================================
--- 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerResponseTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/web_agent/tests/cases/lmbWebServerResponseTest.class.php	2007-10-14 09:12:00 UTC (rev 6414)
@@ -0,0 +1,40 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package web_agent
+ */
+lmb_require('limb/web_agent/src/lmbWebServerResponse.class.php');
+lmb_require('limb/web_agent/src/lmbWebServerCookieCollection.class.php');
+
+/**
+ * Web server response
+ *
+ * @package web_agent
+ * @version $Id: lmbWebServerResponseTest.class.php 40 2007-10-04 15:52:39Z CatMan $
+ */
+class lmbWebServerResponseTest extends UnitTestCase {
+
+  function testGetResponseParams()
+  {
+    $cookies = new lmbWebServerCookiesCollection();
+    $headers = new lmbWebAgentHeaders(array('Location' => 'http://localhost'));
+    $response = new lmbWebServerResponse('content', 200, 'text/html', 'utf-8', $cookies, $headers);
+
+    $this->assertEqual($response->getContent(), 'content');
+    $this->assertEqual($response->getStatus(), 200);
+    $this->assertEqual($response->getMediaType(), 'text/html');
+    $this->assertEqual($response->getCharset(), 'utf-8');
+    $this->assertEqual($response->getHeaders()->get('location'), 'http://localhost');
+    $this->assertFalse($response->getHeaders()->get('p3p'));
+    $this->assertIdentical($response->getCookies(), $cookies);
+  }
+
+}
+?>
\ No newline at end of file



More information about the limb-svn mailing list