add possibility to use dummy router - verrrry useful for testing offline :)
authorChristian Weiske <cweiske@cweiske.de>
Thu, 16 Dec 2010 07:44:43 +0000 (08:44 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 16 Dec 2010 07:44:43 +0000 (08:44 +0100)
Wrt3g.php
Wrt3g/DummyRequest.php [new file with mode: 0644]
scripts/linksys-wrt3g.php

index 9910b8e2d65a2e89a4fd524c82792aad8bdad062..c71ae1904a5a7c258dc313669112bc7f07ada47e 100644 (file)
--- 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 (file)
index 0000000..97f3217
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+require_once 'HTTP/Request2.php';
+require_once 'HTTP/Request2/Adapter/Mock.php';
+
+class Wrt3g_DummyRequest extends HTTP_Request2
+{
+    /**
+     * Maps request paths (i.e. "/index.php") to files containing the HTML body
+     *
+     * @var array
+     */
+    public static $mapping = array(
+        '/Status_NoAuth.asp' => '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
index 7d87cd1decf30104be0ec3728cc7745c71b0bd39..4d64b5ac4be2c31abfb48d19f0bf314640c3baa5 100755 (executable)
@@ -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) {