request.php 5.6 KB

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