e09973630ab7499b614c4458f78febce3f89fd97
[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     *
38     * @return HTTP_Request2_Response
39     *
40     * @throws HTTP_Request2_Exception When the path is not mapped, or
41     *                                 the mapped file does not exist
42     */
43     public function send()
44     {
45         $host = $this->url->getHost();
46         if (strlen($host) == 3 && is_numeric($host)) {
47             $this->adapter->addResponse(
48                 "HTTP/1.0 $host dummy\n\nnothing"
49             );
50             return parent::send();
51         }
52
53         $path = $this->url->getPath();
54         if (!isset(self::$mapping[$path])) {
55             throw new HTTP_Request2_Exception(
56                 'No predefined response for path: ' . $path
57             );
58         }
59
60         $file = dirname(__FILE__) . '/../' . self::$mapping[$path];
61         if (!file_exists($file)) {
62             throw new HTTP_Request2_Exception(
63                 'Response file does not exist: ' . $file
64             );
65         }
66         
67         $this->adapter->addResponse(
68             "HTTP/1.0 200 OK\n"
69             . "\n"
70             . file_get_contents($file)
71         );
72
73         return parent::send();
74     }
75 }
76
77 ?>