ignore releases dir
[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         '/index_wstatus2.asp' => 'tests/index_wstatus2-connected.htm'
16     );
17
18
19
20     /**
21      * Sets the mock connection adapter
22      *
23      * @param string|Net_Url2 Request URL
24      * @param string          Request method
25      * @param array           Configuration for this Request instance
26      */
27     public function __construct(
28         $url = null, $method = self::METHOD_GET, array $config = array()
29     ) {
30         parent::__construct($url, $method, $config);
31         $this->setAdapter(new HTTP_Request2_Adapter_Mock());
32     }
33
34
35
36    /**
37     * Sends the request and returns the response.
38     * When the host is three-letter numeric string, it
39     * is used as HTTP response status code. No "proper" content
40     * is returned in that case.
41     *
42     * @return HTTP_Request2_Response
43     *
44     * @throws HTTP_Request2_Exception When the path is not mapped, or
45     *                                 the mapped file does not exist
46     */
47     public function send()
48     {
49         $host = $this->url->getHost();
50         if (strlen($host) == 3 && is_numeric($host)) {
51             $this->adapter->addResponse(
52                 "HTTP/1.0 $host dummy\n\nnothing"
53             );
54             return parent::send();
55         }
56
57         $path = $this->url->getPath();
58         if (!isset(self::$mapping[$path])) {
59             throw new HTTP_Request2_Exception(
60                 'No predefined response for path: ' . $path
61             );
62         }
63
64         $file = dirname(__FILE__) . '/../' . self::$mapping[$path];
65         if (!file_exists($file)) {
66             throw new HTTP_Request2_Exception(
67                 'Response file does not exist: ' . $file
68             );
69         }
70         
71         $this->adapter->addResponse(
72             "HTTP/1.0 200 OK\n"
73             . "\n"
74             . file_get_contents($file)
75         );
76
77         return parent::send();
78     }
79 }
80
81 ?>