request.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. class requestTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_request;
  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. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  59. $this->assertEquals('create', $request->getOperation());
  60. $this->assertEquals('foo', $request->getParam('data'));
  61. }
  62. public function testApiCreateAlternative()
  63. {
  64. $this->reset();
  65. $_SERVER['REQUEST_METHOD'] = 'POST';
  66. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  67. $_POST['attachment'] = 'foo';
  68. $request = new request;
  69. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  70. $this->assertEquals('create', $request->getOperation());
  71. $this->assertEquals('foo', $request->getParam('attachment'));
  72. }
  73. public function testApiRead()
  74. {
  75. $this->reset();
  76. $_SERVER['REQUEST_METHOD'] = 'GET';
  77. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  78. $_SERVER['QUERY_STRING'] = 'foo';
  79. $request = new request;
  80. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  81. $this->assertEquals('foo', $request->getParam('pasteid'));
  82. $this->assertEquals('read', $request->getOperation());
  83. }
  84. public function testApiDelete()
  85. {
  86. $this->reset();
  87. $_SERVER['REQUEST_METHOD'] = 'POST';
  88. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  89. $_SERVER['QUERY_STRING'] = 'foo';
  90. $_POST['deletetoken'] = 'bar';
  91. $request = new request;
  92. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  93. $this->assertEquals('delete', $request->getOperation());
  94. $this->assertEquals('foo', $request->getParam('pasteid'));
  95. $this->assertEquals('bar', $request->getParam('deletetoken'));
  96. }
  97. public function testReadWithNegotiation()
  98. {
  99. $this->reset();
  100. $_SERVER['REQUEST_METHOD'] = 'GET';
  101. $_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';
  102. $_SERVER['QUERY_STRING'] = 'foo';
  103. $request = new request;
  104. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  105. $this->assertEquals('foo', $request->getParam('pasteid'));
  106. $this->assertEquals('read', $request->getOperation());
  107. }
  108. public function testReadWithXhtmlNegotiation()
  109. {
  110. $this->reset();
  111. $_SERVER['REQUEST_METHOD'] = 'GET';
  112. $_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';
  113. $_SERVER['QUERY_STRING'] = 'foo';
  114. $request = new request;
  115. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  116. $this->assertEquals('foo', $request->getParam('pasteid'));
  117. $this->assertEquals('read', $request->getOperation());
  118. }
  119. public function testApiReadWithNegotiation()
  120. {
  121. $this->reset();
  122. $_SERVER['REQUEST_METHOD'] = 'GET';
  123. $_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';
  124. $_SERVER['QUERY_STRING'] = 'foo';
  125. $request = new request;
  126. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  127. $this->assertEquals('foo', $request->getParam('pasteid'));
  128. $this->assertEquals('read', $request->getOperation());
  129. }
  130. public function testReadWithFailedNegotiation()
  131. {
  132. $this->reset();
  133. $_SERVER['REQUEST_METHOD'] = 'GET';
  134. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  135. $_SERVER['QUERY_STRING'] = 'foo';
  136. $request = new request;
  137. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  138. $this->assertEquals('foo', $request->getParam('pasteid'));
  139. $this->assertEquals('read', $request->getOperation());
  140. }
  141. }