97f3217bd644b05954fe519c30b913d98690a1a1
[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     );
15
16
17
18     /**
19      * Sets the mock connection adapter
20      *
21      * @param string|Net_Url2 Request URL
22      * @param string          Request method
23      * @param array           Configuration for this Request instance
24      */
25     public function __construct(
26         $url = null, $method = self::METHOD_GET, array $config = array()
27     ) {
28         parent::__construct($url, $method, $config);
29         $this->setAdapter(new HTTP_Request2_Adapter_Mock());
30     }
31
32
33
34    /**
35     * Sends the request and returns the response
36     *
37     * @return HTTP_Request2_Response
38     *
39     * @throws HTTP_Request2_Exception When the path is not mapped, or
40     *                                 the mapped file does not exist
41     */
42     public function send()
43     {
44         $path = $this->url->getPath();
45         if (!isset(self::$mapping[$path])) {
46             throw new HTTP_Request2_Exception(
47                 'No predefined response for path: ' . $path
48             );
49         }
50
51         $file = dirname(__FILE__) . '/../' . self::$mapping[$path];
52         if (!file_exists($file)) {
53             throw new HTTP_Request2_Exception(
54                 'Response file does not exist: ' . $file
55             );
56         }
57         
58         $this->adapter->addResponse(
59             "HTTP/1.0 200 OK\n"
60             . "\n"
61             . file_get_contents($file)
62         );
63
64         return parent::send();
65     }
66 }
67
68 ?>