RequestTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $request = new Request;
  62. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  63. $this->assertEquals($id, $request->getParam('pasteid'));
  64. $this->assertEquals('read', $request->getOperation());
  65. }
  66. public function testDelete()
  67. {
  68. $this->reset();
  69. $id = $this->getRandomId();
  70. $_SERVER['REQUEST_METHOD'] = 'GET';
  71. $_GET['pasteid'] = $id;
  72. $_GET['deletetoken'] = 'bar';
  73. $request = new Request;
  74. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  75. $this->assertEquals('delete', $request->getOperation());
  76. $this->assertEquals($id, $request->getParam('pasteid'));
  77. $this->assertEquals('bar', $request->getParam('deletetoken'));
  78. }
  79. public function testApiCreate()
  80. {
  81. $this->reset();
  82. $_SERVER['REQUEST_METHOD'] = 'PUT';
  83. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  84. $file = tempnam(sys_get_temp_dir(), 'FOO');
  85. file_put_contents($file, 'data=foo');
  86. Request::setInputStream($file);
  87. $request = new Request;
  88. unlink($file);
  89. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  90. $this->assertEquals('create', $request->getOperation());
  91. $this->assertEquals('foo', $request->getParam('data'));
  92. }
  93. public function testApiCreateAlternative()
  94. {
  95. $this->reset();
  96. $_SERVER['REQUEST_METHOD'] = 'POST';
  97. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  98. $_POST['attachment'] = 'foo';
  99. $request = new Request;
  100. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  101. $this->assertEquals('create', $request->getOperation());
  102. $this->assertEquals('foo', $request->getParam('attachment'));
  103. }
  104. public function testApiRead()
  105. {
  106. $this->reset();
  107. $id = $this->getRandomId();
  108. $_SERVER['REQUEST_METHOD'] = 'GET';
  109. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  110. $_SERVER['QUERY_STRING'] = $id;
  111. $request = new Request;
  112. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  113. $this->assertEquals($id, $request->getParam('pasteid'));
  114. $this->assertEquals('read', $request->getOperation());
  115. }
  116. public function testApiDelete()
  117. {
  118. $this->reset();
  119. $id = $this->getRandomId();
  120. $_SERVER['REQUEST_METHOD'] = 'POST';
  121. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  122. $_SERVER['QUERY_STRING'] = $id;
  123. $_POST['deletetoken'] = 'bar';
  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 testReadWithNegotiation()
  131. {
  132. $this->reset();
  133. $id = $this->getRandomId();
  134. $_SERVER['REQUEST_METHOD'] = 'GET';
  135. $_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';
  136. $_SERVER['QUERY_STRING'] = $id;
  137. $request = new Request;
  138. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  139. $this->assertEquals($id, $request->getParam('pasteid'));
  140. $this->assertEquals('read', $request->getOperation());
  141. }
  142. public function testReadWithXhtmlNegotiation()
  143. {
  144. $this->reset();
  145. $id = $this->getRandomId();
  146. $_SERVER['REQUEST_METHOD'] = 'GET';
  147. $_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';
  148. $_SERVER['QUERY_STRING'] = $id;
  149. $request = new Request;
  150. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  151. $this->assertEquals($id, $request->getParam('pasteid'));
  152. $this->assertEquals('read', $request->getOperation());
  153. }
  154. public function testApiReadWithNegotiation()
  155. {
  156. $this->reset();
  157. $id = $this->getRandomId();
  158. $_SERVER['REQUEST_METHOD'] = 'GET';
  159. $_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';
  160. $_SERVER['QUERY_STRING'] = $id;
  161. $request = new Request;
  162. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  163. $this->assertEquals($id, $request->getParam('pasteid'));
  164. $this->assertEquals('read', $request->getOperation());
  165. }
  166. public function testReadWithFailedNegotiation()
  167. {
  168. $this->reset();
  169. $id = $this->getRandomId();
  170. $_SERVER['REQUEST_METHOD'] = 'GET';
  171. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  172. $_SERVER['QUERY_STRING'] = $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 testPasteIdExtraction()
  179. {
  180. $this->reset();
  181. $id = $this->getRandomId();
  182. $queryParams = array($id);
  183. $queryParamCount = random_int(1, 5);
  184. for ($i = 0; $i < $queryParamCount; ++$i) {
  185. array_push($queryParams, $this->getRandomQueryChars());
  186. }
  187. shuffle($queryParams);
  188. $_SERVER['REQUEST_METHOD'] = 'GET';
  189. $_SERVER['QUERY_STRING'] = implode('&', $queryParams);
  190. $request = new Request;
  191. $this->assertEquals($id, $request->getParam('pasteid'));
  192. }
  193. }