File: PEAR_test_mock_pearweb.php.inc

package info (click to toggle)
php-pear 1%3A1.10.1%2Bsubmodules%2Bnotgz-9%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,600 kB
  • sloc: php: 51,665; ansic: 38,629; xml: 32,572; yacc: 677; pascal: 452; makefile: 122; sh: 116
file content (135 lines) | stat: -rw-r--r-- 4,439 bytes parent folder | download | duplicates (5)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
class PEAR_test_mock_pearweb
{
    var $_config;
    var $_continue = false;

    function addHtmlConfig($address, $filename, $lastmodified = false)
    {
        $this->_config['html'][$address] = array(basename($filename), file_get_contents($filename),
            $lastmodified);
    }

    function addRESTConfig($address, $content, $content_type)
    {
        $this->_config['rest'][$address] = array($content, $content_type);
    }

    function receiveREST($address)
    {
        if (!isset($this->_config) || !is_array($this->_config)) {
            $ret = $this->tryParentREST($address);
            if (!$this->_continue) {
                die('No REST config for URL ' . $address);
            }

            if (!$ret) {
                return $this->do304($address);
            }

            return $this->do200() .
            "ETag: 12345678910\n" .
            "Last-Modified: 12345678910\n" .
            'Content-Type: ' . $ret[2]['content-type'] . "\n" .
            'Content-Length: ' . $ret[2]['content-length'] . "\n\n" .
            $ret[0];
        }

        if (!isset($this->_config['rest'][$address])) {
            $ret = $this->tryParentREST($address);
            if (!$this->_continue) {
                die('No REST config for URL ' . $address);
            }

            if (!$ret) {
                return $this->do304($address);
            }

            return $this->do200() .
            "ETag: 12345678910\n" .
            "Last-Modified: 12345678910\n" .
            'Content-Type: ' . $ret[2]['content-type'] . "\n" .
            'Content-Length: ' . $ret[2]['content-length'] . "\n\n" .
            $ret[0];
        } else {
            if (!$this->_config['rest'][$address][0]) {
                $this->_restCalls[] = array($address, '404');
                return $this->do404($address);
            }
            if ($this->_config['rest'][$address][0] === true) {
                $this->_restCalls[] = array($address, '304');
                return $this->do304($address);
            }
            $this->_restCalls[] = array($address, '200');
            return $this->do200() .
                "ETag: 12345678910\n" .
                "Last-Modified: 12345678910\n" .
                'Content-Type: ' . $this->_config['rest'][$address][1] . "\n" .
                'Content-Length: ' . strlen($this->_config['rest'][$address][0]) . "\n\n" .
                $this->_config['rest'][$address][0];
        }
    }

    function getRESTCalls()
    {
        $ret = $this->_restCalls;
        $this->_restCalls = array();
        return $ret;
    }

    function tryParentREST($address)
    {
        require_once 'PEAR/REST.php';
        $a = new PEAR_Config;
        $rest = new PEAR_REST($a, array());
        PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
        $ret = $rest->downloadHttp($address, false);
        PEAR::popErrorHandling();
        if (!PEAR::isError($ret)) {
            if (!$ret) {
                echo '$pearweb->addRESTConfig("' . $address . "\", true, 'text/xml');\n";
            } else {
                echo '$pearweb->addRESTConfig("' . $address . '", ' . var_export($ret[0], true)
                    . ", 'text/xml');\n";
            }
            return $ret;
        } else {
                echo '$pearweb->addRESTConfig("' . $address . "\", false, false);\n";
            return false;
        }
    }

    function receiveHttp($address)
    {
        if (!isset($this->_config) || !is_array($this->_config)) {
            return $this->do404($address);
        }
        if (!isset($this->_config['html'][$address])) {
            return $this->do404($address);
        } elseif (isset($this->_config['html'][$address][2])) {
            return $this->do200() .
                $this->_config['html'][$address][2] .
                'Content-Length: ' . strlen($this->_config['html'][$address][1]) . "\n\n" .
                $this->_config['html'][$address][1];
        } else {
            return $this->do200() .
                'Content-Length: ' . strlen($this->_config['html'][$address][1]) . "\n\n" .
                $this->_config['html'][$address][1];
        }
    }

    function do200()
    {
        return "HTTP/1.1 200 \n";
    }

    function do404($address)
    {
        return 'HTTP/1.1 404 ' . $address . ' Is not valid';
    }

    function do304($address)
    {
        return 'HTTP/1.1 304 ' . $address . ' Unmodified';
    }
}