2008-08-09 05:54:55 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Control\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use ReflectionMethod;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
class HTTPRequestTest extends SapphireTest {
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMatch() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest("GET", "admin/crm/add");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/* When a rule matches, but has no variables, array("_matched" => true) is returned. */
|
|
|
|
$this->assertEquals(array("_matched" => true), $request->match('admin/crm', true));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/* Becasue we shifted admin/crm off the stack, just "add" should be remaining */
|
|
|
|
$this->assertEquals("add", $request->remaining());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->assertEquals(array("_matched" => true), $request->match('add', true));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
public function testHttpMethodOverrides() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'GET',
|
|
|
|
'admin/crm'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isGET(),
|
|
|
|
'GET with no method override'
|
|
|
|
);
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'POST',
|
|
|
|
'admin/crm'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isPOST(),
|
|
|
|
'POST with no method override'
|
|
|
|
);
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'GET',
|
|
|
|
'admin/crm',
|
|
|
|
array('_method' => 'DELETE')
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isGET(),
|
|
|
|
'GET with invalid POST method override'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
array(),
|
|
|
|
array('_method' => 'DELETE')
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isDELETE(),
|
|
|
|
'POST with valid method override to DELETE'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
array(),
|
|
|
|
array('_method' => 'put')
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isPUT(),
|
|
|
|
'POST with valid method override to PUT'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
array(),
|
|
|
|
array('_method' => 'head')
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isHEAD(),
|
|
|
|
'POST with valid method override to HEAD '
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
array(),
|
|
|
|
array('_method' => 'head')
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isHEAD(),
|
|
|
|
'POST with valid method override to HEAD'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
array('_method' => 'head')
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$request->isPOST(),
|
|
|
|
'POST with invalid method override by GET parameters to HEAD'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
public function testRequestVars() {
|
|
|
|
$getVars = array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
);
|
|
|
|
$postVars = array(
|
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
|
|
|
);
|
|
|
|
$requestVars = array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
|
|
|
);
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2011-12-22 23:04:44 +01:00
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
$getVars,
|
|
|
|
$postVars
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$requestVars,
|
|
|
|
$request->requestVars(),
|
|
|
|
'GET parameters should supplement POST parameters'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$getVars = array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
);
|
|
|
|
$postVars = array(
|
|
|
|
'first' => 'c',
|
|
|
|
'third' => 'd',
|
|
|
|
);
|
|
|
|
$requestVars = array(
|
|
|
|
'first' => 'c',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'd',
|
|
|
|
);
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2011-12-22 23:04:44 +01:00
|
|
|
'POST',
|
|
|
|
'admin/crm',
|
|
|
|
$getVars,
|
|
|
|
$postVars
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$requestVars,
|
|
|
|
$request->requestVars(),
|
|
|
|
'POST parameters should override GET parameters'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$getVars = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'a',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$postVars = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'c',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$requestVars = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'c',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2011-12-22 23:04:44 +01:00
|
|
|
'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
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$getVars = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'a',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$postVars = array(
|
|
|
|
'first' => array(
|
|
|
|
'second' => 'c',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$requestVars = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'c',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest(
|
2011-12-22 23:04:44 +01:00
|
|
|
'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
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIsAjax() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest('GET', '/', array('ajax' => 0));
|
2012-04-05 14:44:42 +02:00
|
|
|
$this->assertFalse($req->isAjax());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest('GET', '/', array('ajax' => 1));
|
2012-04-05 14:44:42 +02:00
|
|
|
$this->assertTrue($req->isAjax());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest('GET', '/');
|
2012-04-05 14:44:42 +02:00
|
|
|
$req->addHeader('X-Requested-With', 'XMLHttpRequest');
|
|
|
|
$this->assertTrue($req->isAjax());
|
|
|
|
}
|
2012-06-29 12:02:30 +02:00
|
|
|
|
|
|
|
public function testGetURL() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest('GET', '/');
|
2012-06-29 12:02:30 +02:00
|
|
|
$this->assertEquals('', $req->getURL());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest('GET', '/assets/somefile.gif');
|
2012-06-29 12:02:30 +02:00
|
|
|
$this->assertEquals('assets/somefile.gif', $req->getURL());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest('GET', '/home?test=1');
|
2012-06-29 12:02:30 +02:00
|
|
|
$this->assertEquals('home?test=1', $req->getURL(true));
|
|
|
|
$this->assertEquals('home', $req->getURL());
|
|
|
|
}
|
2016-03-01 13:56:34 +01:00
|
|
|
|
|
|
|
public function testGetIPFromHeaderValue() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest('GET', '/');
|
2016-03-01 13:56:34 +01:00
|
|
|
$reflectionMethod = new ReflectionMethod($req, 'getIPFromHeaderValue');
|
|
|
|
$reflectionMethod->setAccessible(true);
|
|
|
|
|
|
|
|
$headers = array(
|
|
|
|
'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',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($headers as $header => $ip) {
|
|
|
|
$this->assertEquals($ip, $reflectionMethod->invoke($req, $header));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-12-22 23:04:44 +01:00
|
|
|
}
|