1
0

RequestTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 testReadWithNegotiation()
  138. {
  139. $this->reset();
  140. $id = $this->getRandomId();
  141. $_SERVER['REQUEST_METHOD'] = 'GET';
  142. $_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';
  143. $_SERVER['QUERY_STRING'] = $id;
  144. $_GET[$id] = '';
  145. $request = new Request;
  146. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  147. $this->assertEquals($id, $request->getParam('pasteid'));
  148. $this->assertEquals('read', $request->getOperation());
  149. }
  150. public function testReadWithXhtmlNegotiation()
  151. {
  152. $this->reset();
  153. $id = $this->getRandomId();
  154. $_SERVER['REQUEST_METHOD'] = 'GET';
  155. $_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';
  156. $_SERVER['QUERY_STRING'] = $id;
  157. $_GET[$id] = '';
  158. $request = new Request;
  159. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  160. $this->assertEquals($id, $request->getParam('pasteid'));
  161. $this->assertEquals('read', $request->getOperation());
  162. }
  163. public function testApiReadWithNegotiation()
  164. {
  165. $this->reset();
  166. $id = $this->getRandomId();
  167. $_SERVER['REQUEST_METHOD'] = 'GET';
  168. $_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';
  169. $_SERVER['QUERY_STRING'] = $id;
  170. $_GET[$id] = '';
  171. $request = new Request;
  172. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  173. $this->assertEquals($id, $request->getParam('pasteid'));
  174. $this->assertEquals('read', $request->getOperation());
  175. }
  176. public function testReadWithFailedNegotiation()
  177. {
  178. $this->reset();
  179. $id = $this->getRandomId();
  180. $_SERVER['REQUEST_METHOD'] = 'GET';
  181. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  182. $_SERVER['QUERY_STRING'] = $id;
  183. $_GET[$id] = '';
  184. $request = new Request;
  185. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  186. $this->assertEquals($id, $request->getParam('pasteid'));
  187. $this->assertEquals('read', $request->getOperation());
  188. }
  189. public function testPasteIdExtraction()
  190. {
  191. $this->reset();
  192. $id = $this->getRandomId();
  193. $queryParams = array($id);
  194. $queryParamCount = random_int(1, 5);
  195. for ($i = 0; $i < $queryParamCount; ++$i) {
  196. array_push($queryParams, $this->getRandomQueryChars());
  197. }
  198. shuffle($queryParams);
  199. $_SERVER['REQUEST_METHOD'] = 'GET';
  200. $_SERVER['QUERY_STRING'] = implode('&', $queryParams);
  201. $_GET[$id] = '';
  202. $request = new Request;
  203. $this->assertEquals($id, $request->getParam('pasteid'));
  204. }
  205. }