Completed
Push — 4.5 ( 0ed340...1726cd )
by Garion
20:10 queued 11:52
created

HTTPRequestTest::detectMethodDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 16
rs 9.8666
c 1
b 0
f 0
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
65
        $this->assertTrue(
66
            $request->isPOST(),
67
            '_method override is no longer honored'
68
        );
69
70
        $this->assertFalse(
71
            $request->isDELETE(),
72
            'DELETE _method override is not honored'
73
        );
74
75
        $request = new HTTPRequest(
76
            'POST',
77
            'admin/crm',
78
            array(),
79
            array('_method' => 'put')
80
        );
81
        $this->assertFalse(
82
            $request->isPUT(),
83
            'PUT _method override is not honored'
84
        );
85
86
        $request = new HTTPRequest(
87
            'POST',
88
            'admin/crm',
89
            array(),
90
            array('_method' => 'head')
91
        );
92
        $this->assertFalse(
93
            $request->isHEAD(),
94
            'HEAD _method override is not honored'
95
        );
96
    }
97
98
    public function detectMethodDataProvider()
99
    {
100
        return [
101
            'Plain POST request' => ['POST', [], 'POST'],
102
            'Plain GET request' => ['GET', [], 'GET'],
103
            'Plain DELETE request' => ['DELETE', [], 'DELETE'],
104
            'Plain PUT request' => ['PUT', [], 'PUT'],
105
            'Plain HEAD request' => ['HEAD', [], 'HEAD'],
106
107
            'Request with GET method override' => ['POST', ['_method' => 'GET'], 'GET'],
108
            'Request with HEAD method override' => ['POST', ['_method' => 'HEAD'], 'HEAD'],
109
            'Request with DELETE method override' => ['POST', ['_method' => 'DELETE'], 'DELETE'],
110
            'Request with PUT method override' => ['POST', ['_method' => 'PUT'], 'PUT'],
111
            'Request with POST method override' => ['POST', ['_method' => 'POST'], 'POST'],
112
113
            'Request with mixed case method override' => ['POST', ['_method' => 'gEt'], 'GET']
114
        ];
115
    }
116
117
    /**
118
     * @dataProvider detectMethodDataProvider
119
     */
120
    public function testDetectMethod($realMethod, $post, $expected)
121
    {
122
        $actual = HTTPRequest::detect_method($realMethod, $post);
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Control\HTTPRequest::detect_method() has been deprecated: 4.4.7 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

122
        $actual = /** @scrutinizer ignore-deprecated */ HTTPRequest::detect_method($realMethod, $post);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
123
        $this->assertEquals($expected, $actual);
124
    }
125
126
    /**
127
     * @expectedException PHPUnit_Framework_Error
128
     */
129
    public function testBadDetectMethod()
130
    {
131
        HTTPRequest::detect_method('POST', ['_method' => 'Boom']);
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Control\HTTPRequest::detect_method() has been deprecated: 4.4.7 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

131
        /** @scrutinizer ignore-deprecated */ HTTPRequest::detect_method('POST', ['_method' => 'Boom']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
132
    }
133
134
    public function setHttpMethodDataProvider()
135
    {
136
        return [
137
            'POST request' => ['POST','POST'],
138
            'GET request' => ['GET', 'GET'],
139
            'DELETE request' => ['DELETE', 'DELETE'],
140
            'PUT request' => ['PUT', 'PUT'],
141
            'HEAD request' => ['HEAD', 'HEAD'],
142
            'Mixed case POST' => ['gEt', 'GET'],
143
        ];
144
    }
145
146
    /**
147
     * @dataProvider setHttpMethodDataProvider
148
     */
149
    public function testSetHttpMethod($method, $expected)
150
    {
151
        $request = new HTTPRequest('GET', '/hello');
152
        $returnedRequest  = $request->setHttpMethod($method);
153
        $this->assertEquals($expected, $request->httpMethod());
154
        $this->assertEquals($request, $returnedRequest);
155
    }
156
157
    /**
158
     * @expectedException PHPUnit_Framework_Error
159
     */
160
    public function testBadSetHttpMethod()
161
    {
162
        $request = new HTTPRequest('GET', '/hello');
163
        $request->setHttpMethod('boom');
164
    }
165
166
    public function testRequestVars()
167
    {
168
        $getVars = array(
169
            'first' => 'a',
170
            'second' => 'b',
171
        );
172
        $postVars = array(
173
            'third' => 'c',
174
            'fourth' => 'd',
175
        );
176
        $requestVars = array(
177
            'first' => 'a',
178
            'second' => 'b',
179
            'third' => 'c',
180
            'fourth' => 'd',
181
        );
182
        $request = new HTTPRequest(
183
            'POST',
184
            'admin/crm',
185
            $getVars,
186
            $postVars
187
        );
188
        $this->assertEquals(
189
            $requestVars,
190
            $request->requestVars(),
191
            'GET parameters should supplement POST parameters'
192
        );
193
194
        $getVars = array(
195
            'first' => 'a',
196
            'second' => 'b',
197
        );
198
        $postVars = array(
199
            'first' => 'c',
200
            'third' => 'd',
201
        );
202
        $requestVars = array(
203
            'first' => 'c',
204
            'second' => 'b',
205
            'third' => 'd',
206
        );
207
        $request = new HTTPRequest(
208
            'POST',
209
            'admin/crm',
210
            $getVars,
211
            $postVars
212
        );
213
        $this->assertEquals(
214
            $requestVars,
215
            $request->requestVars(),
216
            'POST parameters should override GET parameters'
217
        );
218
219
        $getVars = array(
220
            'first' => array(
221
                'first' => 'a',
222
            ),
223
            'second' => array(
224
                'second' => 'b',
225
            ),
226
        );
227
        $postVars = array(
228
            'first' => array(
229
                'first' => 'c',
230
            ),
231
            'third' => array(
232
                'third' => 'd',
233
            ),
234
        );
235
        $requestVars = array(
236
            'first' => array(
237
                'first' => 'c',
238
            ),
239
            'second' => array(
240
                'second' => 'b',
241
            ),
242
            'third' => array(
243
                'third' => 'd',
244
            ),
245
        );
246
        $request = new HTTPRequest(
247
            'POST',
248
            'admin/crm',
249
            $getVars,
250
            $postVars
251
        );
252
        $this->assertEquals(
253
            $requestVars,
254
            $request->requestVars(),
255
            'Nested POST parameters should override GET parameters'
256
        );
257
258
        $getVars = array(
259
            'first' => array(
260
                'first' => 'a',
261
            ),
262
            'second' => array(
263
                'second' => 'b',
264
            ),
265
        );
266
        $postVars = array(
267
            'first' => array(
268
                'second' => 'c',
269
            ),
270
            'third' => array(
271
                'third' => 'd',
272
            ),
273
        );
274
        $requestVars = array(
275
            'first' => array(
276
                'first' => 'a',
277
                'second' => 'c',
278
            ),
279
            'second' => array(
280
                'second' => 'b',
281
            ),
282
            'third' => array(
283
                'third' => 'd',
284
            ),
285
        );
286
        $request = new HTTPRequest(
287
            'POST',
288
            'admin/crm',
289
            $getVars,
290
            $postVars
291
        );
292
        $this->assertEquals(
293
            $requestVars,
294
            $request->requestVars(),
295
            'Nested GET parameters should supplement POST parameters'
296
        );
297
    }
298
299
    public function testIsAjax()
300
    {
301
        $req = new HTTPRequest('GET', '/', array('ajax' => 0));
302
        $this->assertFalse($req->isAjax());
303
304
        $req = new HTTPRequest('GET', '/', array('ajax' => 1));
305
        $this->assertTrue($req->isAjax());
306
307
        $req = new HTTPRequest('GET', '/');
308
        $req->addHeader('X-Requested-With', 'XMLHttpRequest');
309
        $this->assertTrue($req->isAjax());
310
    }
311
312
    public function testGetURL()
313
    {
314
        $req = new HTTPRequest('GET', '/');
315
        $this->assertEquals('', $req->getURL());
316
317
        $req = new HTTPRequest('GET', '/assets/somefile.gif');
318
        $this->assertEquals('assets/somefile.gif', $req->getURL());
319
320
        $req = new HTTPRequest('GET', '/home?test=1');
321
        $this->assertEquals('home?test=1', $req->getURL(true));
322
        $this->assertEquals('home', $req->getURL());
323
    }
324
325
    public function testSetIPFromHeaderValue()
326
    {
327
        $req = new TrustedProxyMiddleware();
328
        $reflectionMethod = new ReflectionMethod($req, 'getIPFromHeaderValue');
329
        $reflectionMethod->setAccessible(true);
330
331
        $headers = array(
332
            '80.79.208.21, 149.126.76.1, 10.51.0.68' => '80.79.208.21',
333
            '52.19.19.103, 10.51.0.49' => '52.19.19.103',
334
            '10.51.0.49, 52.19.19.103' => '52.19.19.103',
335
            '10.51.0.49' => '10.51.0.49',
336
            '127.0.0.1, 10.51.0.49' => '127.0.0.1',
337
        );
338
339
        foreach ($headers as $header => $ip) {
340
            $this->assertEquals($ip, $reflectionMethod->invoke($req, $header));
341
        }
342
    }
343
344
    public function testHasSession()
345
    {
346
        $request = new HTTPRequest('GET', '/');
347
        $this->assertFalse($request->hasSession());
348
349
        $request->setSession($this->createMock(Session::class));
350
        $this->assertTrue($request->hasSession());
351
    }
352
}
353