RequestTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\AppFramework\Http;
  9. global $data;
  10. class RequestTest extends \PHPUnit_Framework_TestCase {
  11. public function setUp() {
  12. require_once __DIR__ . '/requeststream.php';
  13. if (in_array('fakeinput', stream_get_wrappers())) {
  14. stream_wrapper_unregister('fakeinput');
  15. }
  16. stream_wrapper_register('fakeinput', 'RequestStream');
  17. }
  18. public function tearDown() {
  19. stream_wrapper_unregister('fakeinput');
  20. }
  21. public function testRequestAccessors() {
  22. $vars = array(
  23. 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
  24. 'method' => 'GET',
  25. );
  26. $request = new Request($vars);
  27. // Countable
  28. $this->assertEquals(2, count($request));
  29. // Array access
  30. $this->assertEquals('Joey', $request['nickname']);
  31. // "Magic" accessors
  32. $this->assertEquals('Joey', $request->{'nickname'});
  33. $this->assertTrue(isset($request['nickname']));
  34. $this->assertTrue(isset($request->{'nickname'}));
  35. $this->assertEquals(false, isset($request->{'flickname'}));
  36. // Only testing 'get', but same approach for post, files etc.
  37. $this->assertEquals('Joey', $request->get['nickname']);
  38. // Always returns null if variable not set.
  39. $this->assertEquals(null, $request->{'flickname'});
  40. }
  41. // urlParams has precedence over POST which has precedence over GET
  42. public function testPrecedence() {
  43. $vars = array(
  44. 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
  45. 'post' => array('name' => 'Jane Doe', 'nickname' => 'Janey'),
  46. 'urlParams' => array('user' => 'jw', 'name' => 'Johnny Weissmüller'),
  47. );
  48. $request = new Request($vars);
  49. $this->assertEquals(3, count($request));
  50. $this->assertEquals('Janey', $request->{'nickname'});
  51. $this->assertEquals('Johnny Weissmüller', $request->{'name'});
  52. }
  53. /**
  54. * @expectedException RuntimeException
  55. */
  56. public function testImmutableArrayAccess() {
  57. $vars = array(
  58. 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
  59. );
  60. $request = new Request($vars);
  61. $request['nickname'] = 'Janey';
  62. }
  63. /**
  64. * @expectedException RuntimeException
  65. */
  66. public function testImmutableMagicAccess() {
  67. $vars = array(
  68. 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
  69. );
  70. $request = new Request($vars);
  71. $request->{'nickname'} = 'Janey';
  72. }
  73. /**
  74. * @expectedException LogicException
  75. */
  76. public function testGetTheMethodRight() {
  77. $vars = array(
  78. 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
  79. 'method' => 'GET',
  80. );
  81. $request = new Request($vars);
  82. $result = $request->post;
  83. }
  84. public function testTheMethodIsRight() {
  85. $vars = array(
  86. 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
  87. 'method' => 'GET',
  88. );
  89. $request = new Request($vars);
  90. $this->assertEquals('GET', $request->method);
  91. $result = $request->get;
  92. $this->assertEquals('John Q. Public', $result['name']);
  93. $this->assertEquals('Joey', $result['nickname']);
  94. }
  95. public function testJsonPost() {
  96. global $data;
  97. $data = '{"name": "John Q. Public", "nickname": "Joey"}';
  98. $vars = array(
  99. 'method' => 'POST',
  100. 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
  101. );
  102. $request = new Request($vars);
  103. $this->assertEquals('POST', $request->method);
  104. $result = $request->post;
  105. $this->assertEquals('John Q. Public', $result['name']);
  106. $this->assertEquals('Joey', $result['nickname']);
  107. $this->assertEquals('Joey', $request->params['nickname']);
  108. $this->assertEquals('Joey', $request['nickname']);
  109. }
  110. public function testPatch() {
  111. global $data;
  112. $data = http_build_query(array('name' => 'John Q. Public', 'nickname' => 'Joey'), '', '&');
  113. $vars = array(
  114. 'method' => 'PATCH',
  115. 'server' => array('CONTENT_TYPE' => 'application/x-www-form-urlencoded'),
  116. );
  117. $request = new Request($vars);
  118. $this->assertEquals('PATCH', $request->method);
  119. $result = $request->patch;
  120. $this->assertEquals('John Q. Public', $result['name']);
  121. $this->assertEquals('Joey', $result['nickname']);
  122. }
  123. public function testJsonPatchAndPut() {
  124. global $data;
  125. // PUT content
  126. $data = '{"name": "John Q. Public", "nickname": "Joey"}';
  127. $vars = array(
  128. 'method' => 'PUT',
  129. 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
  130. );
  131. $request = new Request($vars);
  132. $this->assertEquals('PUT', $request->method);
  133. $result = $request->put;
  134. $this->assertEquals('John Q. Public', $result['name']);
  135. $this->assertEquals('Joey', $result['nickname']);
  136. // PATCH content
  137. $data = '{"name": "John Q. Public", "nickname": null}';
  138. $vars = array(
  139. 'method' => 'PATCH',
  140. 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
  141. );
  142. $request = new Request($vars);
  143. $this->assertEquals('PATCH', $request->method);
  144. $result = $request->patch;
  145. $this->assertEquals('John Q. Public', $result['name']);
  146. $this->assertEquals(null, $result['nickname']);
  147. }
  148. public function testPutStream() {
  149. global $data;
  150. $data = file_get_contents(__DIR__ . '/../../../data/testimage.png');
  151. $vars = array(
  152. 'put' => $data,
  153. 'method' => 'PUT',
  154. 'server' => array('CONTENT_TYPE' => 'image/png'),
  155. );
  156. $request = new Request($vars);
  157. $this->assertEquals('PUT', $request->method);
  158. $resource = $request->put;
  159. $contents = stream_get_contents($resource);
  160. $this->assertEquals($data, $contents);
  161. try {
  162. $resource = $request->put;
  163. } catch(\LogicException $e) {
  164. return;
  165. }
  166. $this->fail('Expected LogicException.');
  167. }
  168. }