RequestTest.php 8.8 KB

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