RequestTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 testApiReadWithToken()
  121. {
  122. $this->reset();
  123. $id = $this->getRandomId();
  124. $_SERVER['REQUEST_METHOD'] = 'GET';
  125. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  126. $_SERVER['QUERY_STRING'] = $id . '&token=foo';
  127. $_GET[$id] = '';
  128. $_GET['token'] = 'foo';
  129. $request = new Request;
  130. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  131. $this->assertEquals($id, $request->getParam('pasteid'));
  132. $this->assertEquals('foo', $request->getParam('token'));
  133. $this->assertEquals('read', $request->getOperation());
  134. }
  135. public function testApiDelete()
  136. {
  137. $this->reset();
  138. $id = $this->getRandomId();
  139. $_SERVER['REQUEST_METHOD'] = 'POST';
  140. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  141. $_SERVER['QUERY_STRING'] = $id;
  142. $_GET = array($id => '');
  143. $file = tempnam(sys_get_temp_dir(), 'FOO');
  144. file_put_contents($file, '{"deletetoken":"bar"}');
  145. Request::setInputStream($file);
  146. $request = new Request;
  147. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  148. $this->assertEquals('delete', $request->getOperation());
  149. $this->assertEquals($id, $request->getParam('pasteid'));
  150. $this->assertEquals('bar', $request->getParam('deletetoken'));
  151. }
  152. public function testReadWithNegotiation()
  153. {
  154. $this->reset();
  155. $id = $this->getRandomId();
  156. $_SERVER['REQUEST_METHOD'] = 'GET';
  157. $_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';
  158. $_SERVER['QUERY_STRING'] = $id;
  159. $_GET[$id] = '';
  160. $request = new Request;
  161. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  162. $this->assertEquals($id, $request->getParam('pasteid'));
  163. $this->assertEquals('read', $request->getOperation());
  164. }
  165. public function testReadWithXhtmlNegotiation()
  166. {
  167. $this->reset();
  168. $id = $this->getRandomId();
  169. $_SERVER['REQUEST_METHOD'] = 'GET';
  170. $_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';
  171. $_SERVER['QUERY_STRING'] = $id;
  172. $_GET[$id] = '';
  173. $request = new Request;
  174. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  175. $this->assertEquals($id, $request->getParam('pasteid'));
  176. $this->assertEquals('read', $request->getOperation());
  177. }
  178. public function testApiReadWithNegotiation()
  179. {
  180. $this->reset();
  181. $id = $this->getRandomId();
  182. $_SERVER['REQUEST_METHOD'] = 'GET';
  183. $_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';
  184. $_SERVER['QUERY_STRING'] = $id;
  185. $_GET[$id] = '';
  186. $request = new Request;
  187. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  188. $this->assertEquals($id, $request->getParam('pasteid'));
  189. $this->assertEquals('read', $request->getOperation());
  190. }
  191. public function testReadWithFailedNegotiation()
  192. {
  193. $this->reset();
  194. $id = $this->getRandomId();
  195. $_SERVER['REQUEST_METHOD'] = 'GET';
  196. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  197. $_SERVER['QUERY_STRING'] = $id;
  198. $_GET[$id] = '';
  199. $request = new Request;
  200. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  201. $this->assertEquals($id, $request->getParam('pasteid'));
  202. $this->assertEquals('read', $request->getOperation());
  203. }
  204. public function testPasteIdExtraction()
  205. {
  206. $this->reset();
  207. $id = $this->getRandomId();
  208. $queryParams = array($id);
  209. $queryParamCount = random_int(1, 5);
  210. for ($i = 0; $i < $queryParamCount; ++$i) {
  211. array_push($queryParams, $this->getRandomQueryChars());
  212. }
  213. shuffle($queryParams);
  214. $_SERVER['REQUEST_METHOD'] = 'GET';
  215. $_SERVER['QUERY_STRING'] = implode('&', $queryParams);
  216. $_GET[$id] = '';
  217. $request = new Request;
  218. $this->assertEquals($id, $request->getParam('pasteid'));
  219. }
  220. }