From afdec668e5cdaec7f9ea2266c2287c0acb82689b Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 16 Dec 2010 08:44:43 +0100 Subject: [PATCH] add possibility to use dummy router - verrrry useful for testing offline :) --- Wrt3g.php | 14 ++++++-- Wrt3g/DummyRequest.php | 68 +++++++++++++++++++++++++++++++++++++++ scripts/linksys-wrt3g.php | 22 ++++++++++++- 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 Wrt3g/DummyRequest.php diff --git a/Wrt3g.php b/Wrt3g.php index 9910b8e..c71ae19 100644 --- a/Wrt3g.php +++ b/Wrt3g.php @@ -56,6 +56,14 @@ class Wrt3g */ public $verbosity = 0; + /** + * Class to send HTTP Requests with. + * Needs to be compatible with HTTP_Request2 + * + * @var string + */ + public $requestClass = 'HTTP_Request2'; + /** @@ -99,7 +107,7 @@ class Wrt3g $url = $this->getAuthBaseUrl() . '/apply.cgi'; $this->log('Connecting to ' . $url); - $r = new HTTP_Request2(); + $r = new $this->requestClass(); $r->setMethod(HTTP_Request2::METHOD_POST); $r->setUrl($url); $r->addPostParameter('action', 'Reboot'); @@ -130,7 +138,7 @@ class Wrt3g /** * Connection status */ - $r = new HTTP_Request2(); + $r = new $this->requestClass(); $r->setMethod(HTTP_Request2::METHOD_GET); $r->setUrl($strUrlBase . '/index_wstatus2.asp'); $this->log('Connecting to ' . $strUrlBase . '/index_wstatus2.asp', 1); @@ -167,7 +175,7 @@ class Wrt3g $url = $strUrlBase . '/Status_NoAuth.asp'; $this->log('Connecting to ' . $url, 1); - $r = new HTTP_Request2(); + $r = new $this->requestClass(); $r->setMethod(HTTP_Request2::METHOD_GET); $r->setUrl($url); $resp = $r->send(); diff --git a/Wrt3g/DummyRequest.php b/Wrt3g/DummyRequest.php new file mode 100644 index 0000000..97f3217 --- /dev/null +++ b/Wrt3g/DummyRequest.php @@ -0,0 +1,68 @@ + 'tests/status_noauth-connected.htm' + ); + + + + /** + * Sets the mock connection adapter + * + * @param string|Net_Url2 Request URL + * @param string Request method + * @param array Configuration for this Request instance + */ + public function __construct( + $url = null, $method = self::METHOD_GET, array $config = array() + ) { + parent::__construct($url, $method, $config); + $this->setAdapter(new HTTP_Request2_Adapter_Mock()); + } + + + + /** + * Sends the request and returns the response + * + * @return HTTP_Request2_Response + * + * @throws HTTP_Request2_Exception When the path is not mapped, or + * the mapped file does not exist + */ + public function send() + { + $path = $this->url->getPath(); + if (!isset(self::$mapping[$path])) { + throw new HTTP_Request2_Exception( + 'No predefined response for path: ' . $path + ); + } + + $file = dirname(__FILE__) . '/../' . self::$mapping[$path]; + if (!file_exists($file)) { + throw new HTTP_Request2_Exception( + 'Response file does not exist: ' . $file + ); + } + + $this->adapter->addResponse( + "HTTP/1.0 200 OK\n" + . "\n" + . file_get_contents($file) + ); + + return parent::send(); + } +} + +?> \ No newline at end of file diff --git a/scripts/linksys-wrt3g.php b/scripts/linksys-wrt3g.php index 7d87cd1..4d64b5a 100755 --- a/scripts/linksys-wrt3g.php +++ b/scripts/linksys-wrt3g.php @@ -71,6 +71,14 @@ $parser->addOption( 'action' => 'Counter', ) ); +$parser->addOption( + 'dummy', + array( + 'long_name' => '--dummy', + 'description' => 'Use dummy router data, not real ones', + 'action' => 'StoreTrue', + ) +); $stCmd = $parser->addCommand( 'status', @@ -112,6 +120,12 @@ try { try { $router = new Wrt3g(); + if ($result->options['dummy']) { + require_once 'Wrt3g/DummyRequest.php'; + $router->requestClass = 'Wrt3g_DummyRequest'; + $router->log('Using dummy data', 1); + } else { + } $router->verbosity = $result->options['verbosity']; $router->host = $result->options['host']; $router->user = $result->options['user']; @@ -140,7 +154,13 @@ try { $arStatus = $router->getConnectionStatus(); } foreach ($arStatus as $key => $value) { - echo $key . ': ' . $value . "\n"; + echo $key . ': '; + if (is_array($value)) { + //session usage + echo var_export($value, true) . "\n"; + } else { + echo $value . "\n"; + } } } } catch (Exception $e) { -- 2.30.2