|
|
@@ -12,18 +12,6 @@ class RequestTest extends TestCase
|
|
|
$_POST = array();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Returns 16 random hexadecimal characters.
|
|
|
- *
|
|
|
- * @access public
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public function getRandomId()
|
|
|
- {
|
|
|
- // 8 binary bytes are 16 characters long in hex
|
|
|
- return bin2hex(random_bytes(8));
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Returns random query safe characters.
|
|
|
*
|
|
|
@@ -54,7 +42,25 @@ class RequestTest extends TestCase
|
|
|
public function testRead()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
+ $_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
+ $_SERVER['QUERY_STRING'] = $id;
|
|
|
+ $_GET[$id] = '';
|
|
|
+ $request = new Request;
|
|
|
+ $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
|
|
|
+ $this->assertEquals($id, $request->getParam('pasteid'));
|
|
|
+ $this->assertEquals('read', $request->getOperation());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * paste IDs are 8 bytes hex encoded strings, if unlucky, this turns into
|
|
|
+ * a numeric string that PHP will cast to an int, for example in array keys
|
|
|
+ * @see https://www.php.net/manual/en/language.types.array.php
|
|
|
+ */
|
|
|
+ public function testReadNumeric()
|
|
|
+ {
|
|
|
+ $this->reset();
|
|
|
+ $id = '1234567812345678';
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
$_SERVER['QUERY_STRING'] = $id;
|
|
|
$_GET[$id] = '';
|
|
|
@@ -67,7 +73,7 @@ class RequestTest extends TestCase
|
|
|
public function testDelete()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
$_GET['pasteid'] = $id;
|
|
|
$_GET['deletetoken'] = 'bar';
|
|
|
@@ -110,7 +116,7 @@ class RequestTest extends TestCase
|
|
|
public function testApiRead()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
|
|
|
$_SERVER['QUERY_STRING'] = $id;
|
|
|
@@ -124,7 +130,7 @@ class RequestTest extends TestCase
|
|
|
public function testApiDelete()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
|
|
$_SERVER['QUERY_STRING'] = $id;
|
|
|
@@ -155,7 +161,7 @@ class RequestTest extends TestCase
|
|
|
public function testReadWithNegotiation()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
$_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'] = $id;
|
|
|
@@ -169,7 +175,7 @@ class RequestTest extends TestCase
|
|
|
public function testReadWithXhtmlNegotiation()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
$_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'] = $id;
|
|
|
@@ -183,7 +189,7 @@ class RequestTest extends TestCase
|
|
|
public function testApiReadWithNegotiation()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
$_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'] = $id;
|
|
|
@@ -197,7 +203,7 @@ class RequestTest extends TestCase
|
|
|
public function testReadWithFailedNegotiation()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
|
|
|
$_SERVER['QUERY_STRING'] = $id;
|
|
|
@@ -211,7 +217,7 @@ class RequestTest extends TestCase
|
|
|
public function testPasteIdExtraction()
|
|
|
{
|
|
|
$this->reset();
|
|
|
- $id = $this->getRandomId();
|
|
|
+ $id = Helper::getRandomId();
|
|
|
$queryParams = array($id);
|
|
|
$queryParamCount = random_int(1, 5);
|
|
|
for ($i = 0; $i < $queryParamCount; ++$i) {
|