1
0

RequestTest.php 8.7 KB

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