Passed
Push — 4 ( 3224c9...7533ac )
by Guy
09:03 queued 10s
created

HTTPRequestTest::testHasSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace SilverStripe\Control\Tests;
4
5
use ReflectionMethod;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Control\Middleware\TrustedProxyMiddleware;
8
use SilverStripe\Control\Session;
9
use SilverStripe\Dev\SapphireTest;
10
11
class HTTPRequestTest extends SapphireTest
12
{
13
    protected static $fixture_file = null;
14
15
    public function testMatch()
16
    {
17
        $request = new HTTPRequest("GET", "admin/crm/add");
18
19
        /* When a rule matches, but has no variables, array("_matched" => true) is returned. */
20
        $this->assertEquals(array("_matched" => true), $request->match('admin/crm', true));
21
22
        /* Becasue we shifted admin/crm off the stack, just "add" should be remaining */
23
        $this->assertEquals("add", $request->remaining());
24
25
        $this->assertEquals(array("_matched" => true), $request->match('add', true));
26
    }
27
28
    public function testHttpMethodOverrides()
29
    {
30
        $request = new HTTPRequest(
31
            'GET',
32
            'admin/crm'
33
        );
34
        $this->assertTrue(
35
            $request->isGET(),
36
            'GET with no method override'
37
        );
38
39
        $request = new HTTPRequest(
40
            'POST',
41
            'admin/crm'
42
        );
43
        $this->assertTrue(
44
            $request->isPOST(),
45
            'POST with no method override'
46
        );
47
48
        $request = new HTTPRequest(
49
            'GET',
50
            'admin/crm',
51
            array('_method' => 'DELETE')
52
        );
53
        $this->assertTrue(
54
            $request->isGET(),
55
            'GET with invalid POST method override'
56
        );
57
58
        $request = new HTTPRequest(
59
            'POST',
60
            'admin/crm',
61
            array(),
62
            array('_method' => 'DELETE')
63
        );
64
        $this->assertTrue(
65
            $request->isDELETE(),
66
            'POST with valid method override to DELETE'
67
        );
68
69
        $request = new HTTPRequest(
70
            'POST',
71
            'admin/crm',
72
            array(),
73
            array('_method' => 'put')
74
        );
75
        $this->assertTrue(
76
            $request->isPUT(),
77
            'POST with valid method override to PUT'
78
        );
79
80
        $request = new HTTPRequest(
81
            'POST',
82
            'admin/crm',
83
            array(),
84
            array('_method' => 'head')
85
        );
86
        $this->assertTrue(
87
            $request->isHEAD(),
88
            'POST with valid method override to HEAD '
89
        );
90
91
        $request = new HTTPRequest(
92
            'POST',
93
            'admin/crm',
94
            array(),
95
            array('_method' => 'head')
96
        );
97
        $this->assertTrue(
98
            $request->isHEAD(),
99
            'POST with valid method override to HEAD'
100
        );
101
102
        $request = new HTTPRequest(
103
            'POST',
104
            'admin/crm',
105
            array('_method' => 'head')
106
        );
107
        $this->assertTrue(
108
            $request->isPOST(),
109
            'POST with invalid method override by GET parameters to HEAD'
110
        );
111
    }
112
113
    public function testRequestVars()
114
    {
115
        $getVars = array(
116
            'first' => 'a',
117
            'second' => 'b',
118
        );
119
        $postVars = array(
120
            'third' => 'c',
121
            'fourth' => 'd',
122
        );
123
        $requestVars = array(
124
            'first' => 'a',
125
            'second' => 'b',
126
            'third' => 'c',
127
            'fourth' => 'd',
128
        );
129
        $request = new HTTPRequest(
130
            'POST',
131
            'admin/crm',
132
            $getVars,
133
            $postVars
134
        );
135
        $this->assertEquals(
136
            $requestVars,
137
            $request->requestVars(),
138
            'GET parameters should supplement POST parameters'
139
        );
140
141
        $getVars = array(
142
            'first' => 'a',
143
            'second' => 'b',
144
        );
145
        $postVars = array(
146
            'first' => 'c',
147
            'third' => 'd',
148
        );
149
        $requestVars = array(
150
            'first' => 'c',
151
            'second' => 'b',
152
            'third' => 'd',
153
        );
154
        $request = new HTTPRequest(
155
            'POST',
156
            'admin/crm',
157
            $getVars,
158
            $postVars
159
        );
160
        $this->assertEquals(
161
            $requestVars,
162
            $request->requestVars(),
163
            'POST parameters should override GET parameters'
164
        );
165
166
        $getVars = array(
167
            'first' => array(
168
                'first' => 'a',
169
            ),
170
            'second' => array(
171
                'second' => 'b',
172
            ),
173
        );
174
        $postVars = array(
175
            'first' => array(
176
                'first' => 'c',
177
            ),
178
            'third' => array(
179
                'third' => 'd',
180
            ),
181
        );
182
        $requestVars = array(
183
            'first' => array(
184
                'first' => 'c',
185
            ),
186
            'second' => array(
187
                'second' => 'b',
188
            ),
189
            'third' => array(
190
                'third' => 'd',
191
            ),
192
        );
193
        $request = new HTTPRequest(
194
            'POST',
195
            'admin/crm',
196
            $getVars,
197
            $postVars
198
        );
199
        $this->assertEquals(
200
            $requestVars,
201
            $request->requestVars(),
202
            'Nested POST parameters should override GET parameters'
203
        );
204
205
        $getVars = array(
206
            'first' => array(
207
                'first' => 'a',
208
            ),
209
            'second' => array(
210
                'second' => 'b',
211
            ),
212
        );
213
        $postVars = array(
214
            'first' => array(
215
                'second' => 'c',
216
            ),
217
            'third' => array(
218
                'third' => 'd',
219
            ),
220
        );
221
        $requestVars = array(
222
            'first' => array(
223
                'first' => 'a',
224
                'second' => 'c',
225
            ),
226
            'second' => array(
227
                'second' => 'b',
228
            ),
229
            'third' => array(
230
                'third' => 'd',
231
            ),
232
        );
233
        $request = new HTTPRequest(
234
            'POST',
235
            'admin/crm',
236
            $getVars,
237
            $postVars
238
        );
239
        $this->assertEquals(
240
            $requestVars,
241
            $request->requestVars(),
242
            'Nested GET parameters should supplement POST parameters'
243
        );
244
    }
245
246
    public function testIsAjax()
247
    {
248
        $req = new HTTPRequest('GET', '/', array('ajax' => 0));
249
        $this->assertFalse($req->isAjax());
250
251
        $req = new HTTPRequest('GET', '/', array('ajax' => 1));
252
        $this->assertTrue($req->isAjax());
253
254
        $req = new HTTPRequest('GET', '/');
255
        $req->addHeader('X-Requested-With', 'XMLHttpRequest');
256
        $this->assertTrue($req->isAjax());
257
    }
258
259
    public function testGetURL()
260
    {
261
        $req = new HTTPRequest('GET', '/');
262
        $this->assertEquals('', $req->getURL());
263
264
        $req = new HTTPRequest('GET', '/assets/somefile.gif');
265
        $this->assertEquals('assets/somefile.gif', $req->getURL());
266
267
        $req = new HTTPRequest('GET', '/home?test=1');
268
        $this->assertEquals('home?test=1', $req->getURL(true));
269
        $this->assertEquals('home', $req->getURL());
270
    }
271
272
    public function testSetIPFromHeaderValue()
273
    {
274
        $req = new TrustedProxyMiddleware();
275
        $reflectionMethod = new ReflectionMethod($req, 'getIPFromHeaderValue');
276
        $reflectionMethod->setAccessible(true);
277
278
        $headers = array(
279
            '80.79.208.21, 149.126.76.1, 10.51.0.68' => '80.79.208.21',
280
            '52.19.19.103, 10.51.0.49' => '52.19.19.103',
281
            '10.51.0.49, 52.19.19.103' => '52.19.19.103',
282
            '10.51.0.49' => '10.51.0.49',
283
            '127.0.0.1, 10.51.0.49' => '127.0.0.1',
284
        );
285
286
        foreach ($headers as $header => $ip) {
287
            $this->assertEquals($ip, $reflectionMethod->invoke($req, $header));
288
        }
289
    }
290
291
    public function testHasSession()
292
    {
293
        $request = new HTTPRequest('GET', '/');
294
        $this->assertFalse($request->hasSession());
295
296
        $request->setSession($this->createMock(Session::class));
297
        $this->assertTrue($request->hasSession());
298
    }
299
}
300