aboutsummaryrefslogtreecommitdiff
path: root/Wrt3g/DummyRequest.php
blob: 24fa511b2d5d5c810cfd2030ef028e9306da0206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?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',
        '/index_wstatus1.asp' => 'tests/index_wstatus1-connected-excellent.htm',
        '/index_wstatus2.asp' => 'tests/index_wstatus2-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.
    * When the host is three-letter numeric string, it
    * is used as HTTP response status code. No "proper" content
    * is returned in that case.
    *
    * @return HTTP_Request2_Response
    *
    * @throws HTTP_Request2_Exception When the path is not mapped, or
    *                                 the mapped file does not exist
    */
    public function send()
    {
        $host = $this->url->getHost();
        if (strlen($host) == 3 && is_numeric($host)) {
            $this->adapter->addResponse(
                "HTTP/1.0 $host dummy\n\nnothing"
            );
            return parent::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();
    }
}

?>