2015-09-27 13:37:17 +00:00
|
|
|
<?php
|
2016-07-21 15:09:48 +00:00
|
|
|
|
2016-08-09 09:54:42 +00:00
|
|
|
use PrivateBin\Request;
|
2016-07-21 15:09:48 +00:00
|
|
|
|
2016-08-09 09:54:42 +00:00
|
|
|
class RequestTest extends PHPUnit_Framework_TestCase
|
2015-09-27 13:37:17 +00:00
|
|
|
{
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
$_SERVER = array();
|
2016-10-29 08:24:08 +00:00
|
|
|
$_GET = array();
|
|
|
|
$_POST = array();
|
2015-09-27 13:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testView()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$request = new Request;
|
2015-09-27 13:37:17 +00:00
|
|
|
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
|
|
|
|
$this->assertEquals('view', $request->getOperation());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRead()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['QUERY_STRING'] = 'foo';
|
|
|
|
$request = new Request;
|
2015-09-27 13:37:17 +00:00
|
|
|
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('read', $request->getOperation());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDelete()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_GET['pasteid'] = 'foo';
|
|
|
|
$_GET['deletetoken'] = 'bar';
|
|
|
|
$request = new Request;
|
2015-09-27 13:37:17 +00:00
|
|
|
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
|
|
|
|
$this->assertEquals('delete', $request->getOperation());
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('bar', $request->getParam('deletetoken'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiCreate()
|
|
|
|
{
|
|
|
|
$this->reset();
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
2015-09-27 13:37:17 +00:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
2016-10-29 08:24:08 +00:00
|
|
|
$file = tempnam(sys_get_temp_dir(), 'FOO');
|
2015-09-27 18:34:39 +00:00
|
|
|
file_put_contents($file, 'data=foo');
|
2016-08-09 09:54:42 +00:00
|
|
|
Request::setInputStream($file);
|
|
|
|
$request = new Request;
|
2017-03-24 22:42:11 +00:00
|
|
|
unlink($file);
|
2015-09-27 13:37:17 +00:00
|
|
|
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
|
|
|
|
$this->assertEquals('create', $request->getOperation());
|
|
|
|
$this->assertEquals('foo', $request->getParam('data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiCreateAlternative()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
|
|
|
|
$_POST['attachment'] = 'foo';
|
|
|
|
$request = new Request;
|
2015-09-27 13:37:17 +00:00
|
|
|
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
|
|
|
|
$this->assertEquals('create', $request->getOperation());
|
|
|
|
$this->assertEquals('foo', $request->getParam('attachment'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiRead()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
|
|
|
|
$_SERVER['QUERY_STRING'] = 'foo';
|
|
|
|
$request = new Request;
|
2015-09-27 13:37:17 +00:00
|
|
|
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('read', $request->getOperation());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiDelete()
|
|
|
|
{
|
|
|
|
$this->reset();
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2015-09-27 13:37:17 +00:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['QUERY_STRING'] = 'foo';
|
|
|
|
$_POST['deletetoken'] = 'bar';
|
|
|
|
$request = new Request;
|
2015-09-27 13:37:17 +00:00
|
|
|
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
|
|
|
|
$this->assertEquals('delete', $request->getOperation());
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('bar', $request->getParam('deletetoken'));
|
|
|
|
}
|
2016-04-08 21:29:44 +00:00
|
|
|
|
|
|
|
public function testReadWithNegotiation()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/html,text/html; charset=UTF-8,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8, text/csv,application/json';
|
|
|
|
$_SERVER['QUERY_STRING'] = 'foo';
|
|
|
|
$request = new Request;
|
2016-04-08 21:29:44 +00:00
|
|
|
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('read', $request->getOperation());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadWithXhtmlNegotiation()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/xhtml+xml,text/html,text/html; charset=UTF-8, application/xml;q=0.9,*/*;q=0.8, text/csv,application/json';
|
|
|
|
$_SERVER['QUERY_STRING'] = 'foo';
|
|
|
|
$request = new Request;
|
2016-04-08 21:29:44 +00:00
|
|
|
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('read', $request->getOperation());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiReadWithNegotiation()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, application/json, text/html,text/html; charset=UTF-8,application/xhtml+xml, */*;q=0.8';
|
|
|
|
$_SERVER['QUERY_STRING'] = 'foo';
|
|
|
|
$request = new Request;
|
2016-04-08 21:29:44 +00:00
|
|
|
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('read', $request->getOperation());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadWithFailedNegotiation()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-10-29 08:24:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
|
|
|
|
$_SERVER['QUERY_STRING'] = 'foo';
|
|
|
|
$request = new Request;
|
2016-04-08 21:29:44 +00:00
|
|
|
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
|
|
|
|
$this->assertEquals('foo', $request->getParam('pasteid'));
|
|
|
|
$this->assertEquals('read', $request->getOperation());
|
|
|
|
}
|
2016-07-11 12:15:20 +00:00
|
|
|
}
|