RequestTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, 'data=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('data'));
  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. $_POST['attachment'] = 'foo';
  100. $request = new Request;
  101. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  102. $this->assertEquals('create', $request->getOperation());
  103. $this->assertEquals('foo', $request->getParam('attachment'));
  104. }
  105. public function testApiRead()
  106. {
  107. $this->reset();
  108. $id = $this->getRandomId();
  109. $_SERVER['REQUEST_METHOD'] = 'GET';
  110. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  111. $_SERVER['QUERY_STRING'] = $id;
  112. $_GET[$id] = '';
  113. $request = new Request;
  114. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  115. $this->assertEquals($id, $request->getParam('pasteid'));
  116. $this->assertEquals('read', $request->getOperation());
  117. }
  118. public function testApiDelete()
  119. {
  120. $this->reset();
  121. $id = $this->getRandomId();
  122. $_SERVER['REQUEST_METHOD'] = 'POST';
  123. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  124. $_SERVER['QUERY_STRING'] = $id;
  125. $_GET = [$id => ''];
  126. $_POST['deletetoken'] = 'bar';
  127. $request = new Request;
  128. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  129. $this->assertEquals('delete', $request->getOperation());
  130. $this->assertEquals($id, $request->getParam('pasteid'));
  131. $this->assertEquals('bar', $request->getParam('deletetoken'));
  132. }
  133. public function testReadWithNegotiation()
  134. {
  135. $this->reset();
  136. $id = $this->getRandomId();
  137. $_SERVER['REQUEST_METHOD'] = 'GET';
  138. $_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';
  139. $_SERVER['QUERY_STRING'] = $id;
  140. $_GET[$id] = '';
  141. $request = new Request;
  142. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  143. $this->assertEquals($id, $request->getParam('pasteid'));
  144. $this->assertEquals('read', $request->getOperation());
  145. }
  146. public function testReadWithXhtmlNegotiation()
  147. {
  148. $this->reset();
  149. $id = $this->getRandomId();
  150. $_SERVER['REQUEST_METHOD'] = 'GET';
  151. $_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';
  152. $_SERVER['QUERY_STRING'] = $id;
  153. $_GET[$id] = '';
  154. $request = new Request;
  155. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  156. $this->assertEquals($id, $request->getParam('pasteid'));
  157. $this->assertEquals('read', $request->getOperation());
  158. }
  159. public function testApiReadWithNegotiation()
  160. {
  161. $this->reset();
  162. $id = $this->getRandomId();
  163. $_SERVER['REQUEST_METHOD'] = 'GET';
  164. $_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';
  165. $_SERVER['QUERY_STRING'] = $id;
  166. $_GET[$id] = '';
  167. $request = new Request;
  168. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  169. $this->assertEquals($id, $request->getParam('pasteid'));
  170. $this->assertEquals('read', $request->getOperation());
  171. }
  172. public function testReadWithFailedNegotiation()
  173. {
  174. $this->reset();
  175. $id = $this->getRandomId();
  176. $_SERVER['REQUEST_METHOD'] = 'GET';
  177. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  178. $_SERVER['QUERY_STRING'] = $id;
  179. $_GET[$id] = '';
  180. $request = new Request;
  181. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  182. $this->assertEquals($id, $request->getParam('pasteid'));
  183. $this->assertEquals('read', $request->getOperation());
  184. }
  185. public function testPasteIdExtraction()
  186. {
  187. $this->reset();
  188. $id = $this->getRandomId();
  189. $queryParams = array($id);
  190. $queryParamCount = random_int(1, 5);
  191. for ($i = 0; $i < $queryParamCount; ++$i) {
  192. array_push($queryParams, $this->getRandomQueryChars());
  193. }
  194. shuffle($queryParams);
  195. $_SERVER['REQUEST_METHOD'] = 'GET';
  196. $_SERVER['QUERY_STRING'] = implode('&', $queryParams);
  197. $_GET[$id] = '';
  198. $request = new Request;
  199. $this->assertEquals($id, $request->getParam('pasteid'));
  200. }
  201. }