1
0

RequestTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Request;
  4. class RequestTest extends TestCase
  5. {
  6. public function setUp()
  7. {
  8. /* Setup Routine */
  9. }
  10. public function tearDown()
  11. {
  12. /* Tear Down Routine */
  13. }
  14. public function reset()
  15. {
  16. $_SERVER = array();
  17. $_GET = array();
  18. $_POST = array();
  19. }
  20. /**
  21. * Returns 16 random hexadecimal characters.
  22. *
  23. * @access public
  24. * @return string
  25. */
  26. public function getRandomId()
  27. {
  28. // 8 binary bytes are 16 characters long in hex
  29. return bin2hex(random_bytes(8));
  30. }
  31. /**
  32. * Returns random query safe characters.
  33. *
  34. * @access public
  35. * @return string
  36. */
  37. public function getRandomQueryChars()
  38. {
  39. $queryChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=';
  40. $queryCharCount = strlen($queryChars) - 1;
  41. $resultLength = random_int(1, 10);
  42. $result = '';
  43. for ($i = 0; $i < $resultLength; ++$i) {
  44. $result .= $queryChars[random_int(0, $queryCharCount)];
  45. }
  46. return $result;
  47. }
  48. public function testView()
  49. {
  50. $this->reset();
  51. $_SERVER['REQUEST_METHOD'] = 'GET';
  52. $request = new Request;
  53. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  54. $this->assertEquals('view', $request->getOperation());
  55. }
  56. public function testRead()
  57. {
  58. $this->reset();
  59. $id = $this->getRandomId();
  60. $_SERVER['REQUEST_METHOD'] = 'GET';
  61. $_SERVER['QUERY_STRING'] = $id;
  62. $_GET[$id] = '';
  63. $request = new Request;
  64. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  65. $this->assertEquals($id, $request->getParam('pasteid'));
  66. $this->assertEquals('read', $request->getOperation());
  67. }
  68. public function testDelete()
  69. {
  70. $this->reset();
  71. $id = $this->getRandomId();
  72. $_SERVER['REQUEST_METHOD'] = 'GET';
  73. $_GET['pasteid'] = $id;
  74. $_GET['deletetoken'] = 'bar';
  75. $request = new Request;
  76. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  77. $this->assertEquals('delete', $request->getOperation());
  78. $this->assertEquals($id, $request->getParam('pasteid'));
  79. $this->assertEquals('bar', $request->getParam('deletetoken'));
  80. }
  81. public function testApiCreate()
  82. {
  83. $this->reset();
  84. $_SERVER['REQUEST_METHOD'] = 'PUT';
  85. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  86. $file = tempnam(sys_get_temp_dir(), 'FOO');
  87. file_put_contents($file, '{"ct":"foo"}');
  88. Request::setInputStream($file);
  89. $request = new Request;
  90. unlink($file);
  91. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  92. $this->assertEquals('create', $request->getOperation());
  93. $this->assertEquals('foo', $request->getParam('ct'));
  94. }
  95. public function testApiCreateAlternative()
  96. {
  97. $this->reset();
  98. $_SERVER['REQUEST_METHOD'] = 'POST';
  99. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  100. $file = tempnam(sys_get_temp_dir(), 'FOO');
  101. file_put_contents($file, '{"ct":"foo"}');
  102. Request::setInputStream($file);
  103. $request = new Request;
  104. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  105. $this->assertEquals('create', $request->getOperation());
  106. $this->assertEquals('foo', $request->getParam('ct'));
  107. }
  108. public function testApiRead()
  109. {
  110. $this->reset();
  111. $id = $this->getRandomId();
  112. $_SERVER['REQUEST_METHOD'] = 'GET';
  113. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  114. $_SERVER['QUERY_STRING'] = $id;
  115. $_GET[$id] = '';
  116. $request = new Request;
  117. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  118. $this->assertEquals($id, $request->getParam('pasteid'));
  119. $this->assertEquals('read', $request->getOperation());
  120. }
  121. public function testApiDelete()
  122. {
  123. $this->reset();
  124. $id = $this->getRandomId();
  125. $_SERVER['REQUEST_METHOD'] = 'POST';
  126. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  127. $_SERVER['QUERY_STRING'] = $id;
  128. $_GET = array($id => '');
  129. $file = tempnam(sys_get_temp_dir(), 'FOO');
  130. file_put_contents($file, '{"deletetoken":"bar"}');
  131. Request::setInputStream($file);
  132. $request = new Request;
  133. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  134. $this->assertEquals('delete', $request->getOperation());
  135. $this->assertEquals($id, $request->getParam('pasteid'));
  136. $this->assertEquals('bar', $request->getParam('deletetoken'));
  137. }
  138. public function testReadWithNegotiation()
  139. {
  140. $this->reset();
  141. $id = $this->getRandomId();
  142. $_SERVER['REQUEST_METHOD'] = 'GET';
  143. $_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';
  144. $_SERVER['QUERY_STRING'] = $id;
  145. $_GET[$id] = '';
  146. $request = new Request;
  147. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  148. $this->assertEquals($id, $request->getParam('pasteid'));
  149. $this->assertEquals('read', $request->getOperation());
  150. }
  151. public function testReadWithXhtmlNegotiation()
  152. {
  153. $this->reset();
  154. $id = $this->getRandomId();
  155. $_SERVER['REQUEST_METHOD'] = 'GET';
  156. $_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';
  157. $_SERVER['QUERY_STRING'] = $id;
  158. $_GET[$id] = '';
  159. $request = new Request;
  160. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  161. $this->assertEquals($id, $request->getParam('pasteid'));
  162. $this->assertEquals('read', $request->getOperation());
  163. }
  164. public function testApiReadWithNegotiation()
  165. {
  166. $this->reset();
  167. $id = $this->getRandomId();
  168. $_SERVER['REQUEST_METHOD'] = 'GET';
  169. $_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';
  170. $_SERVER['QUERY_STRING'] = $id;
  171. $_GET[$id] = '';
  172. $request = new Request;
  173. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  174. $this->assertEquals($id, $request->getParam('pasteid'));
  175. $this->assertEquals('read', $request->getOperation());
  176. }
  177. public function testReadWithFailedNegotiation()
  178. {
  179. $this->reset();
  180. $id = $this->getRandomId();
  181. $_SERVER['REQUEST_METHOD'] = 'GET';
  182. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  183. $_SERVER['QUERY_STRING'] = $id;
  184. $_GET[$id] = '';
  185. $request = new Request;
  186. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  187. $this->assertEquals($id, $request->getParam('pasteid'));
  188. $this->assertEquals('read', $request->getOperation());
  189. }
  190. public function testPasteIdExtraction()
  191. {
  192. $this->reset();
  193. $id = $this->getRandomId();
  194. $queryParams = array($id);
  195. $queryParamCount = random_int(1, 5);
  196. for ($i = 0; $i < $queryParamCount; ++$i) {
  197. array_push($queryParams, $this->getRandomQueryChars());
  198. }
  199. shuffle($queryParams);
  200. $_SERVER['REQUEST_METHOD'] = 'GET';
  201. $_SERVER['QUERY_STRING'] = implode('&', $queryParams);
  202. $_GET[$id] = '';
  203. $request = new Request;
  204. $this->assertEquals($id, $request->getParam('pasteid'));
  205. }
  206. }