colly_wyx
2018-04-27 74adf3a72663f151dc2c1b87ecb4ea4b0e080a50
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
 * PhpUnderControl_PhalApiRequest_Test
 *
 * 针对 ../PhalApi/Request.php PhalApi_Request 类的PHPUnit单元测试
 *
 * @author: dogstar 20141004
 */
 
require_once dirname(__FILE__) . '/test_env.php';
 
if (!class_exists('PhalApi_Request')) {
    require dirname(__FILE__) . '/../PhalApi/Request.php';
}
 
class PhpUnderControl_PhalApiRequest_Test extends PHPUnit_Framework_TestCase
{
    public $coreRequest;
 
    protected function setUp()
    {
        parent::setUp();
 
        $data = array('year' => '2014', 'version' => '1.0.0');
        $this->coreRequest = new PhalApi_Request($data);
    }
 
    protected function tearDown()
    {
    }
 
 
    /**
     * @group testGet
     */ 
    public function testGet()
    {
        $key = 'year';
        $default = '2015';
 
        $rs = $this->coreRequest->get($key, $default);
 
        $this->assertSame('2014', $rs);
    }
 
    /**
     * @group testGetByRule
     */ 
    public function testGetByRule()
    {
        $rule = array('name' => 'version', 'type' => 'string', 'default' => '0.0.0');
 
        $rs = $this->coreRequest->getByRule($rule);
 
        $this->assertEquals('1.0.0', $rs);
    }
 
    /**
     * @expectedException PhalApi_Exception_BadRequest
     */
    public function testGetByComplexRule()
    {
        $rule = array('name' => 'year', 'type' => 'int', 'min' => '2000', 'max' => '2013');
 
        $rs = $this->coreRequest->getByRule($rule);
 
        $this->assertSame(2013, $rs);
    }
 
    /**
     * @group testGetAll
     */ 
    public function testGetAll()
    {
        $rs = $this->coreRequest->getAll();
        $this->assertEquals(array('year' => '2014', 'version' => '1.0.0'), $rs);
    }
 
    public function testConstructWithREQUEST()
    {
        $request = new PhalApi_Request();
    }
 
    /**
     * @expectedException PhalApi_Exception_InternalServerError
     */
    public function testIllegalRule()
    {
        $this->coreRequest->getByRule(array());
    }
 
    /**
     * @expectedException PhalApi_Exception_BadRequest
     */
    public function testGetRequireVal()
    {
        $this->coreRequest->getByRule(array('name' => 'requireVal', 'require' => true));
    }
 
    public function testGetHeader()
    {
        $_SERVER['HTTP_ACCEPT'] = 'application/text';
        $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf-8';
        //$_SERVER['PHP_AUTH_DIGEST'] = 'xxx';
 
        $request = new PhalApi_Request();
        $this->assertEquals('application/text', $request->getHeader('Accept'));
        $this->assertEquals('utf-8', $request->getHeader('Accept-Charset'));
        //$this->assertEquals('xxx', $request->getHeader('AUTHORIZATION'));
 
        $this->assertEquals('123', $request->getHeader('no-this-key', '123'));
        $this->assertSame(NULL, $request->getHeader('no-this-key'));
 
        unset($_SERVER['HTTP_ACCEPT']);
        unset($_SERVER['HTTP_ACCEPT_CHARSET']);
        unset($_SERVER['PHP_AUTH_DIGEST']);
    }
 
    public function testService() {
        $requests = new PhalApi_Request(array('service' => 'Demo.Go'));
 
        $this->assertEquals('Demo.Go',  $requests->getService());
        $this->assertEquals('Demo',     $requests->getServiceApi());
        $this->assertEquals('Go',       $requests->getServiceAction());
    }
 
    public function testServiceDefault() {
        $requests = new PhalApi_Request();
 
        $this->assertEquals('Default.Index',    $requests->getService());
        $this->assertEquals('Default',          $requests->getServiceApi());
        $this->assertEquals('Index',            $requests->getServiceAction());
    }
 
    public function testServiceEmpty() {
        $requests = new PhalApi_Request(array('service' => ''));
 
        $this->assertSame('', $requests->getService());
        $this->assertSame('', $requests->getServiceApi());
        $this->assertSame(NULL, $requests->getServiceAction());
    }
 
    public function testSource()
    {
        $_POST['pp'] = 'p_data';
        $_GET['gg'] = 'g_data';
        $_COOKIE['cc'] = 'c_data';
        $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf-8';
        $_SERVER['ss'] = 's_data';
        $_REQUEST['rr'] = 'r_data';
        $data = array('dd' => 'd_data');
 
        $requests = new PhalApi_Request($data);
 
        $postRs = $requests->getByRule(array('name' => 'pp', 'source' => 'post'));
        $this->assertEquals('p_data', $postRs);
 
        $getRs = $requests->getByRule(array('name' => 'gg', 'source' => 'get'));
        $this->assertEquals('g_data', $getRs);
 
        $cookieRs = $requests->getByRule(array('name' => 'cc', 'source' => 'cookie'));
        $this->assertEquals('c_data', $cookieRs);
 
        $headerRs = $requests->getByRule(array('name' => 'Accept-Charset', 'source' => 'header'));
        $this->assertEquals('utf-8', $headerRs);
 
        $serverRs = $requests->getByRule(array('name' => 'ss', 'source' => 'server'));
        $this->assertEquals('s_data', $serverRs);
 
        $requestRs = $requests->getByRule(array('name' => 'rr', 'source' => 'request'));
        $this->assertEquals('r_data', $requestRs);
 
        $dataRs = $requests->getByRule(array('name' => 'dd'));
        $this->assertEquals('d_data', $dataRs);
 
        unset($_POST['pp'], $_GET['gg'], $_COOKIE['cc'], $_SERVER['HTTP_ACCEPT_CHARSET'], $_SERVER['ss'], $_REQUEST['rr']);
    }
 
    /**
     * @expectedException PhalApi_Exception_InternalServerError
     * @expectedExceptionMessage no_this_source
     */
    public function testUnkonwSource()
    {
        $requests = new PhalApi_Request(array());
        $notFoundRs = $requests->getByRule(array('name' => 'dd', 'source' => 'no_this_source'));
    }
}