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
|
<?php
require_once __DIR__.'/../../Base.php';
use Kanboard\Core\Http\Request;
class RequestTest extends Base
{
public function testGetStringParam()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEquals('', $request->getStringParam('myvar'));
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEquals('default', $request->getStringParam('myvar', 'default'));
$request = new Request($this->container, array(), array('myvar' => 'myvalue'), array(), array(), array());
$this->assertEquals('myvalue', $request->getStringParam('myvar'));
}
public function testGetIntegerParam()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEquals(0, $request->getIntegerParam('myvar'));
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEquals(5, $request->getIntegerParam('myvar', 5));
$request = new Request($this->container, array(), array('myvar' => 'myvalue'), array(), array(), array());
$this->assertEquals(0, $request->getIntegerParam('myvar'));
$request = new Request($this->container, array(), array('myvar' => '123'), array(), array(), array());
$this->assertEquals(123, $request->getIntegerParam('myvar'));
}
public function testGetValues()
{
$request = new Request($this->container, array(), array(), array('myvar' => 'myvalue'), array(), array());
$this->assertEmpty($request->getValue('myvar'));
$request = new Request($this->container, array(), array(), array('myvar' => 'myvalue', 'csrf_token' => $this->container['token']->getCSRFToken()), array(), array());
$this->assertEquals('myvalue', $request->getValue('myvar'));
$request = new Request($this->container, array(), array(), array('myvar' => 'myvalue', 'csrf_token' => $this->container['token']->getCSRFToken()), array(), array());
$this->assertEquals(array('myvar' => 'myvalue'), $request->getValues());
}
public function testGetFileContent()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEmpty($request->getFileContent('myfile'));
$filename = tempnam(sys_get_temp_dir(), 'UnitTest');
file_put_contents($filename, 'something');
$request = new Request($this->container, array(), array(), array(), array('myfile' => array('tmp_name' => $filename)), array());
$this->assertEquals('something', $request->getFileContent('myfile'));
unlink($filename);
}
public function testGetFilePath()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEmpty($request->getFilePath('myfile'));
$request = new Request($this->container, array(), array(), array(), array('myfile' => array('tmp_name' => 'somewhere')), array());
$this->assertEquals('somewhere', $request->getFilePath('myfile'));
}
public function testIsPost()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertFalse($request->isPost());
$request = new Request($this->container, array('REQUEST_METHOD' => 'POST'), array(), array(), array(), array());
$this->assertTrue($request->isPost());
}
public function testIsAjax()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertFalse($request->isAjax());
$request = new Request($this->container, array('HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'), array(), array(), array(), array());
$this->assertTrue($request->isAjax());
}
public function testIsHTTPS()
{
$request = new Request($this->container, array(), array(), array(), array());
$this->assertFalse($request->isHTTPS());
$request = new Request($this->container, array('HTTPS' => ''), array(), array(), array(), array());
$this->assertFalse($request->isHTTPS());
$request = new Request($this->container, array('HTTPS' => 'off'), array(), array(), array(), array());
$this->assertFalse($request->isHTTPS());
$request = new Request($this->container, array('HTTPS' => 'on'), array(), array(), array(), array());
$this->assertTrue($request->isHTTPS());
$request = new Request($this->container, array('HTTPS' => '1'), array(), array(), array(), array());
$this->assertTrue($request->isHTTPS());
$request = new Request($this->container, array('HTTP_X_FORWARDED_PROTO' => 'https'), array(), array(), array(), array());
$this->assertTrue($request->isHTTPS());
$request = new Request($this->container, array('HTTP_X_FORWARDED_PROTO' => 'http'), array(), array(), array(), array());
$this->assertFalse($request->isHTTPS());
}
public function testGetCookie()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEmpty($request->getCookie('mycookie'));
$request = new Request($this->container, array(), array(), array(), array(), array('mycookie' => 'miam'));
$this->assertEquals('miam', $request->getCookie('mycookie'));
}
public function testGetHeader()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEmpty($request->getHeader('X-Forwarded-For'));
$request = new Request($this->container, array('HTTP_X_FORWARDED_FOR' => 'test'), array(), array(), array(), array());
$this->assertEquals('test', $request->getHeader('X-Forwarded-For'));
}
public function testGetRemoteUser()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEmpty($request->getRemoteUser());
$request = new Request($this->container, array(REVERSE_PROXY_USER_HEADER => 'test'), array(), array(), array(), array());
$this->assertEquals('test', $request->getRemoteUser());
}
public function testGetQueryString()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEmpty($request->getQueryString());
$request = new Request($this->container, array('QUERY_STRING' => 'k=v'), array(), array(), array(), array());
$this->assertEquals('k=v', $request->getQueryString());
}
public function testGetUri()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEmpty($request->getUri());
$request = new Request($this->container, array('REQUEST_URI' => '/blah'), array(), array(), array(), array());
$this->assertEquals('/blah', $request->getUri());
}
public function testGetUserAgent()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEquals('Unknown', $request->getUserAgent());
$request = new Request($this->container, array('HTTP_USER_AGENT' => 'My browser'), array(), array(), array(), array());
$this->assertEquals('My browser', $request->getUserAgent());
}
public function testGetIpAddress()
{
$request = new Request($this->container, array(), array(), array(), array(), array());
$this->assertEquals('Unknown', $request->getIpAddress());
$request = new Request($this->container, array('HTTP_X_REAL_IP' => '192.168.1.1,127.0.0.1'), array(), array(), array(), array());
$this->assertEquals('192.168.1.1', $request->getIpAddress());
$request = new Request($this->container, array('HTTP_X_FORWARDED_FOR' => '192.168.0.1,127.0.0.1'), array(), array(), array(), array());
$this->assertEquals('192.168.0.1', $request->getIpAddress());
$request = new Request($this->container, array('REMOTE_ADDR' => '192.168.0.1'), array(), array(), array(), array());
$this->assertEquals('192.168.0.1', $request->getIpAddress());
$request = new Request($this->container, array('REMOTE_ADDR' => ''), array(), array(), array(), array());
$this->assertEquals('Unknown', $request->getIpAddress());
}
}
|