RequestTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. public function testView()
  20. {
  21. $this->reset();
  22. $_SERVER['REQUEST_METHOD'] = 'GET';
  23. $request = new Request;
  24. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  25. $this->assertEquals('view', $request->getOperation());
  26. }
  27. public function testRead()
  28. {
  29. $this->reset();
  30. $_SERVER['REQUEST_METHOD'] = 'GET';
  31. $_SERVER['QUERY_STRING'] = 'foo';
  32. $request = new Request;
  33. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  34. $this->assertEquals('foo', $request->getParam('pasteid'));
  35. $this->assertEquals('read', $request->getOperation());
  36. }
  37. public function testDelete()
  38. {
  39. $this->reset();
  40. $_SERVER['REQUEST_METHOD'] = 'GET';
  41. $_GET['pasteid'] = 'foo';
  42. $_GET['deletetoken'] = 'bar';
  43. $request = new Request;
  44. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  45. $this->assertEquals('delete', $request->getOperation());
  46. $this->assertEquals('foo', $request->getParam('pasteid'));
  47. $this->assertEquals('bar', $request->getParam('deletetoken'));
  48. }
  49. public function testApiCreate()
  50. {
  51. $this->reset();
  52. $_SERVER['REQUEST_METHOD'] = 'PUT';
  53. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  54. $file = tempnam(sys_get_temp_dir(), 'FOO');
  55. file_put_contents($file, 'data=foo');
  56. Request::setInputStream($file);
  57. $request = new Request;
  58. unlink($file);
  59. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  60. $this->assertEquals('create', $request->getOperation());
  61. $this->assertEquals('foo', $request->getParam('data'));
  62. }
  63. public function testApiCreateAlternative()
  64. {
  65. $this->reset();
  66. $_SERVER['REQUEST_METHOD'] = 'POST';
  67. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  68. $_POST['attachment'] = 'foo';
  69. $request = new Request;
  70. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  71. $this->assertEquals('create', $request->getOperation());
  72. $this->assertEquals('foo', $request->getParam('attachment'));
  73. }
  74. public function testApiRead()
  75. {
  76. $this->reset();
  77. $_SERVER['REQUEST_METHOD'] = 'GET';
  78. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  79. $_SERVER['QUERY_STRING'] = 'foo';
  80. $request = new Request;
  81. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  82. $this->assertEquals('foo', $request->getParam('pasteid'));
  83. $this->assertEquals('read', $request->getOperation());
  84. }
  85. public function testApiDelete()
  86. {
  87. $this->reset();
  88. $_SERVER['REQUEST_METHOD'] = 'POST';
  89. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  90. $_SERVER['QUERY_STRING'] = 'foo';
  91. $_POST['deletetoken'] = 'bar';
  92. $request = new Request;
  93. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  94. $this->assertEquals('delete', $request->getOperation());
  95. $this->assertEquals('foo', $request->getParam('pasteid'));
  96. $this->assertEquals('bar', $request->getParam('deletetoken'));
  97. }
  98. public function testReadWithNegotiation()
  99. {
  100. $this->reset();
  101. $_SERVER['REQUEST_METHOD'] = 'GET';
  102. $_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';
  103. $_SERVER['QUERY_STRING'] = 'foo';
  104. $request = new Request;
  105. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  106. $this->assertEquals('foo', $request->getParam('pasteid'));
  107. $this->assertEquals('read', $request->getOperation());
  108. }
  109. public function testReadWithXhtmlNegotiation()
  110. {
  111. $this->reset();
  112. $_SERVER['REQUEST_METHOD'] = 'GET';
  113. $_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';
  114. $_SERVER['QUERY_STRING'] = 'foo';
  115. $request = new Request;
  116. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  117. $this->assertEquals('foo', $request->getParam('pasteid'));
  118. $this->assertEquals('read', $request->getOperation());
  119. }
  120. public function testApiReadWithNegotiation()
  121. {
  122. $this->reset();
  123. $_SERVER['REQUEST_METHOD'] = 'GET';
  124. $_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';
  125. $_SERVER['QUERY_STRING'] = 'foo';
  126. $request = new Request;
  127. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  128. $this->assertEquals('foo', $request->getParam('pasteid'));
  129. $this->assertEquals('read', $request->getOperation());
  130. }
  131. public function testReadWithFailedNegotiation()
  132. {
  133. $this->reset();
  134. $_SERVER['REQUEST_METHOD'] = 'GET';
  135. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  136. $_SERVER['QUERY_STRING'] = 'foo';
  137. $request = new Request;
  138. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  139. $this->assertEquals('foo', $request->getParam('pasteid'));
  140. $this->assertEquals('read', $request->getOperation());
  141. }
  142. }