2008-08-09 05:54:55 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Control\Tests;
|
|
|
|
|
2019-08-02 01:29:23 +02:00
|
|
|
use ReflectionMethod;
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2017-06-25 05:12:29 +02:00
|
|
|
use SilverStripe\Control\Middleware\TrustedProxyMiddleware;
|
2019-08-02 01:29:23 +02:00
|
|
|
use SilverStripe\Control\Session;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class HTTPRequestTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testMatch()
|
|
|
|
{
|
|
|
|
$request = new HTTPRequest("GET", "admin/crm/add");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/* When a rule matches, but has no variables, array("_matched" => true) is returned. */
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(["_matched" => true], $request->match('admin/crm', true));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2021-12-13 09:05:33 +01:00
|
|
|
/* Because we shifted admin/crm off the stack, just "add" should be remaining */
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals("add", $request->remaining());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(["_matched" => true], $request->match('add', true));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-03-24 21:16:13 +01:00
|
|
|
/**
|
|
|
|
* @useDatabase false
|
|
|
|
*/
|
|
|
|
public function testWildCardMatch()
|
|
|
|
{
|
|
|
|
$request = new HTTPRequest('GET', 'admin/crm/test');
|
|
|
|
$this->assertEquals(['$1' => 'crm', '$2' => 'test'], $request->match('admin/$@', true));
|
|
|
|
$this->assertTrue($request->allParsed());
|
|
|
|
|
|
|
|
$request = new HTTPRequest('GET', 'admin/crm/test');
|
|
|
|
$this->assertEquals(['_matched' => true], $request->match('admin/$*', true));
|
|
|
|
$this->assertTrue($request->allParsed());
|
|
|
|
$this->assertEquals('crm/test', $request->remaining());
|
|
|
|
|
|
|
|
$request = new HTTPRequest('GET', 'admin/crm/test/part1/part2');
|
|
|
|
$this->assertEquals(['Action' => 'crm', '$1' => 'test', '$2' => 'part1', '$3' => 'part2'], $request->match('admin/$Action/$@', true));
|
|
|
|
$this->assertTrue($request->allParsed());
|
|
|
|
|
|
|
|
$request = new HTTPRequest('GET', 'admin/crm/test/part1/part2');
|
|
|
|
$this->assertEquals(['Action' => 'crm'], $request->match('admin/$Action/$*', true));
|
|
|
|
$this->assertTrue($request->allParsed());
|
|
|
|
$this->assertEquals('test/part1/part2', $request->remaining());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test just asserts a warning is given if there is more than one wildcard parameter. Note that this isn't an
|
|
|
|
* enforcement of an API and we an add new behaviour in the future to allow many wildcard params if we want to
|
|
|
|
*/
|
|
|
|
public function testWildCardWithFurtherParams()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectWarning();
|
2020-03-24 21:16:13 +01:00
|
|
|
$request = new HTTPRequest('GET', 'admin/crm/test');
|
|
|
|
// all parameters after the first wildcard parameter are ignored
|
|
|
|
$request->match('admin/$Action/$@/$Other/$*', true);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testHttpMethodOverrides()
|
|
|
|
{
|
|
|
|
$request = new HTTPRequest(
|
|
|
|
'GET',
|
|
|
|
'admin/crm'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isGET(),
|
|
|
|
'GET with no method override'
|
|
|
|
);
|
2008-10-06 16:58:01 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isPOST(),
|
|
|
|
'POST with no method override'
|
|
|
|
);
|
2008-10-06 16:58:01 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'GET',
|
|
|
|
'admin/crm',
|
2020-04-20 19:58:09 +02:00
|
|
|
['_method' => 'DELETE']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isGET(),
|
|
|
|
'GET with invalid POST method override'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
2020-04-20 19:58:09 +02:00
|
|
|
[],
|
|
|
|
['_method' => 'DELETE']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2020-05-12 00:58:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(
|
2020-05-12 00:58:30 +02:00
|
|
|
$request->isPOST(),
|
|
|
|
'_method override is no longer honored'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
2016-12-16 05:34:21 +01:00
|
|
|
$request->isDELETE(),
|
2020-05-12 00:58:30 +02:00
|
|
|
'DELETE _method override is not honored'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
2020-04-20 19:58:09 +02:00
|
|
|
[],
|
|
|
|
['_method' => 'put']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2020-05-12 00:58:30 +02:00
|
|
|
$this->assertFalse(
|
2016-12-16 05:34:21 +01:00
|
|
|
$request->isPUT(),
|
2020-05-12 00:58:30 +02:00
|
|
|
'PUT _method override is not honored'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
2020-04-20 19:58:09 +02:00
|
|
|
[],
|
|
|
|
['_method' => 'head']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2020-05-12 00:58:30 +02:00
|
|
|
$this->assertFalse(
|
2016-12-16 05:34:21 +01:00
|
|
|
$request->isHEAD(),
|
2020-05-12 00:58:30 +02:00
|
|
|
'HEAD _method override is not honored'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2020-05-12 00:58:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-05-12 00:58:30 +02:00
|
|
|
public function detectMethodDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'Plain POST request' => ['POST', [], 'POST'],
|
|
|
|
'Plain GET request' => ['GET', [], 'GET'],
|
|
|
|
'Plain DELETE request' => ['DELETE', [], 'DELETE'],
|
|
|
|
'Plain PUT request' => ['PUT', [], 'PUT'],
|
|
|
|
'Plain HEAD request' => ['HEAD', [], 'HEAD'],
|
|
|
|
|
|
|
|
'Request with GET method override' => ['POST', ['_method' => 'GET'], 'GET'],
|
|
|
|
'Request with HEAD method override' => ['POST', ['_method' => 'HEAD'], 'HEAD'],
|
|
|
|
'Request with DELETE method override' => ['POST', ['_method' => 'DELETE'], 'DELETE'],
|
|
|
|
'Request with PUT method override' => ['POST', ['_method' => 'PUT'], 'PUT'],
|
|
|
|
'Request with POST method override' => ['POST', ['_method' => 'POST'], 'POST'],
|
|
|
|
|
|
|
|
'Request with mixed case method override' => ['POST', ['_method' => 'gEt'], 'GET']
|
|
|
|
];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-05-12 00:58:30 +02:00
|
|
|
public function setHttpMethodDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'POST request' => ['POST','POST'],
|
|
|
|
'GET request' => ['GET', 'GET'],
|
|
|
|
'DELETE request' => ['DELETE', 'DELETE'],
|
|
|
|
'PUT request' => ['PUT', 'PUT'],
|
|
|
|
'HEAD request' => ['HEAD', 'HEAD'],
|
|
|
|
'Mixed case POST' => ['gEt', 'GET'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider setHttpMethodDataProvider
|
|
|
|
*/
|
|
|
|
public function testSetHttpMethod($method, $expected)
|
|
|
|
{
|
|
|
|
$request = new HTTPRequest('GET', '/hello');
|
|
|
|
$returnedRequest = $request->setHttpMethod($method);
|
|
|
|
$this->assertEquals($expected, $request->httpMethod());
|
|
|
|
$this->assertEquals($request, $returnedRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBadSetHttpMethod()
|
|
|
|
{
|
2020-09-25 18:30:52 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2020-05-12 00:58:30 +02:00
|
|
|
$request = new HTTPRequest('GET', '/hello');
|
|
|
|
$request->setHttpMethod('boom');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRequestVars()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$getVars = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$postVars = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$requestVars = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
$getVars,
|
|
|
|
$postVars
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$requestVars,
|
|
|
|
$request->requestVars(),
|
|
|
|
'GET parameters should supplement POST parameters'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$getVars = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$postVars = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$requestVars = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
$getVars,
|
|
|
|
$postVars
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$requestVars,
|
|
|
|
$request->requestVars(),
|
|
|
|
'POST parameters should override GET parameters'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$getVars = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$postVars = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$requestVars = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
$getVars,
|
|
|
|
$postVars
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$requestVars,
|
|
|
|
$request->requestVars(),
|
|
|
|
'Nested POST parameters should override GET parameters'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$getVars = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$postVars = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$requestVars = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
$getVars,
|
|
|
|
$postVars
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$requestVars,
|
|
|
|
$request->requestVars(),
|
|
|
|
'Nested GET parameters should supplement POST parameters'
|
|
|
|
);
|
|
|
|
}
|
2012-04-05 14:44:42 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testIsAjax()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$req = new HTTPRequest('GET', '/', ['ajax' => 0]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse($req->isAjax());
|
2012-04-05 14:44:42 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$req = new HTTPRequest('GET', '/', ['ajax' => 1]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($req->isAjax());
|
2012-04-05 14:44:42 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$req = new HTTPRequest('GET', '/');
|
|
|
|
$req->addHeader('X-Requested-With', 'XMLHttpRequest');
|
|
|
|
$this->assertTrue($req->isAjax());
|
|
|
|
}
|
2012-06-29 12:02:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetURL()
|
|
|
|
{
|
|
|
|
$req = new HTTPRequest('GET', '/');
|
|
|
|
$this->assertEquals('', $req->getURL());
|
2012-06-29 12:02:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$req = new HTTPRequest('GET', '/assets/somefile.gif');
|
|
|
|
$this->assertEquals('assets/somefile.gif', $req->getURL());
|
2012-06-29 12:02:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$req = new HTTPRequest('GET', '/home?test=1');
|
|
|
|
$this->assertEquals('home?test=1', $req->getURL(true));
|
|
|
|
$this->assertEquals('home', $req->getURL());
|
|
|
|
}
|
2016-03-01 13:56:34 +01:00
|
|
|
|
2017-06-25 05:12:29 +02:00
|
|
|
public function testSetIPFromHeaderValue()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-25 05:12:29 +02:00
|
|
|
$req = new TrustedProxyMiddleware();
|
2016-12-16 05:34:21 +01:00
|
|
|
$reflectionMethod = new ReflectionMethod($req, 'getIPFromHeaderValue');
|
|
|
|
$reflectionMethod->setAccessible(true);
|
2016-03-01 13:56:34 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$headers = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'80.79.208.21, 149.126.76.1, 10.51.0.68' => '80.79.208.21',
|
|
|
|
'52.19.19.103, 10.51.0.49' => '52.19.19.103',
|
|
|
|
'10.51.0.49, 52.19.19.103' => '52.19.19.103',
|
|
|
|
'10.51.0.49' => '10.51.0.49',
|
|
|
|
'127.0.0.1, 10.51.0.49' => '127.0.0.1',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-03-01 13:56:34 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
foreach ($headers as $header => $ip) {
|
|
|
|
$this->assertEquals($ip, $reflectionMethod->invoke($req, $header));
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 01:29:23 +02:00
|
|
|
|
|
|
|
public function testHasSession()
|
|
|
|
{
|
|
|
|
$request = new HTTPRequest('GET', '/');
|
|
|
|
$this->assertFalse($request->hasSession());
|
|
|
|
|
|
|
|
$request->setSession($this->createMock(Session::class));
|
|
|
|
$this->assertTrue($request->hasSession());
|
|
|
|
}
|
2011-12-22 23:04:44 +01:00
|
|
|
}
|