update readme
[linksys-wrt3g-tools.git] / Wrt3g / DummyRequest.php
1 <?php
2 require_once 'HTTP/Request2.php';
3 require_once 'HTTP/Request2/Adapter/Mock.php';
4
5 class Wrt3g_DummyRequest extends HTTP_Request2
6 {
7     /**
8      * Maps request paths (i.e. "/index.php") to files containing the HTML body
9      *
10      * @var array
11      */
12     public static $mapping = array(
13         '/Status_NoAuth.asp'  => 'tests/status_noauth-connected.htm',
14         '/index_wstatus1.asp' => 'tests/index_wstatus1-connected-excellent.htm'
15     );
16
17
18
19     /**
20      * Sets the mock connection adapter
21      *
22      * @param string|Net_Url2 Request URL
23      * @param string          Request method
24      * @param array           Configuration for this Request instance
25      */
26     public function __construct(
27         $url = null, $method = self::METHOD_GET, array $config = array()
28     ) {
29         parent::__construct($url, $method, $config);
30         $this->setAdapter(new HTTP_Request2_Adapter_Mock());
31     }
32
33
34
35    /**
36     * Sends the request and returns the response.
37     * When the host is three-letter numeric string, it
38     * is used as HTTP response status code. No "proper" content
39     * is returned in that case.
40     *
41     * @return HTTP_Request2_Response
42     *
43     * @throws HTTP_Request2_Exception When the path is not mapped, or
44     *                                 the mapped file does not exist
45     */
46     public function send()
47     {
48         $host = $this->url->getHost();
49         if (strlen($host) == 3 && is_numeric($host)) {
50             $this->adapter->addResponse(
51                 "HTTP/1.0 $host dummy\n\nnothing"
52             );
53             return parent::send();
54         }
55
56         $path = $this->url->getPath();
57         if (!isset(self::$mapping[$path])) {
58             throw new HTTP_Request2_Exception(
59                 'No predefined response for path: ' . $path
60             );
61         }
62
63         $file = dirname(__FILE__) . '/../' . self::$mapping[$path];
64         if (!file_exists($file)) {
65             throw new HTTP_Request2_Exception(
66                 'Response file does not exist: ' . $file
67             );
68         }
69         
70         $this->adapter->addResponse(
71             "HTTP/1.0 200 OK\n"
72             . "\n"
73             . file_get_contents($file)
74         );
75
76         return parent::send();
77     }
78 }
79
80 ?>