RequestTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http\Client;
  15. use Cake\Http\Client\Request;
  16. use Cake\TestSuite\TestCase;
  17. use Zend\Diactoros\Uri;
  18. /**
  19. * HTTP request test.
  20. */
  21. class RequestTest extends TestCase
  22. {
  23. /**
  24. * test string ata, header and constructor
  25. *
  26. * @return void
  27. */
  28. public function testConstructorStringData()
  29. {
  30. $headers = [
  31. 'Content-Type' => 'application/json',
  32. 'Authorization' => 'Bearer valid-token',
  33. ];
  34. $data = ['a' => 'b', 'c' => 'd'];
  35. $request = new Request('http://example.com', 'POST', $headers, json_encode($data));
  36. $this->assertEquals('http://example.com', (string)$request->getUri());
  37. $this->assertContains($request->getMethod(), 'POST');
  38. $this->assertEquals('application/json', $request->getHeaderLine('Content-Type'));
  39. $this->assertEquals(json_encode($data), $request->body());
  40. }
  41. /**
  42. * @param array $headers The HTTP headers to set.
  43. * @param array|string|null $data The request body to use.
  44. * @param string $method The HTTP method to use.
  45. *
  46. * @dataProvider additionProvider
  47. */
  48. public function testMethods(array $headers, $data, $method)
  49. {
  50. $request = new Request('http://example.com', $method, $headers, json_encode($data));
  51. $this->assertEquals($request->getMethod(), $method);
  52. $this->assertEquals('http://example.com', (string)$request->getUri());
  53. $this->assertEquals('application/json', $request->getHeaderLine('Content-Type'));
  54. $this->assertEquals(json_encode($data), $request->body());
  55. }
  56. /**
  57. * @dataProvider additionProvider
  58. */
  59. public function additionProvider()
  60. {
  61. $headers = [
  62. 'Content-Type' => 'application/json',
  63. 'Authorization' => 'Bearer valid-token',
  64. ];
  65. $data = ['a' => 'b', 'c' => 'd'];
  66. return [
  67. [$headers, $data, Request::METHOD_POST],
  68. [$headers, $data, Request::METHOD_GET],
  69. [$headers, $data, Request::METHOD_PUT],
  70. [$headers, $data, Request::METHOD_DELETE],
  71. ];
  72. }
  73. /**
  74. * test array data, header and constructor
  75. *
  76. * @return void
  77. */
  78. public function testConstructorArrayData()
  79. {
  80. $headers = [
  81. 'Content-Type' => 'application/json',
  82. 'Authorization' => 'Bearer valid-token',
  83. ];
  84. $data = ['a' => 'b', 'c' => 'd'];
  85. $request = new Request('http://example.com', 'POST', $headers, $data);
  86. $this->assertEquals('http://example.com', (string)$request->getUri());
  87. $this->assertEquals('POST', $request->getMethod());
  88. $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
  89. $this->assertEquals('a=b&c=d', $request->body());
  90. }
  91. /**
  92. * test nested array data for encoding of brackets, header and constructor
  93. *
  94. * @return void
  95. */
  96. public function testConstructorArrayNestedData()
  97. {
  98. $headers = [
  99. 'Content-Type' => 'application/json',
  100. 'Authorization' => 'Bearer valid-token',
  101. ];
  102. $data = ['a' => 'b', 'c' => ['foo', 'bar']];
  103. $request = new Request('http://example.com', 'POST', $headers, $data);
  104. $this->assertEquals('http://example.com', (string)$request->getUri());
  105. $this->assertEquals('POST', $request->getMethod());
  106. $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
  107. $this->assertEquals('a=b&c%5B0%5D=foo&c%5B1%5D=bar', $request->body());
  108. }
  109. /**
  110. * test url method
  111. *
  112. * @group deprecated
  113. * @return void
  114. */
  115. public function testUrl()
  116. {
  117. $this->deprecated(function () {
  118. $request = new Request();
  119. $this->assertSame($request, $request->url('http://example.com'));
  120. $this->assertEquals('http://example.com', $request->url());
  121. });
  122. }
  123. /**
  124. * Test that url() modifies the PSR7 stream
  125. *
  126. * @group deprecated
  127. * @return void
  128. */
  129. public function testUrlInteroperability()
  130. {
  131. $this->deprecated(function () {
  132. $request = new Request();
  133. $request->url('http://example.com');
  134. $this->assertSame('http://example.com', $request->url());
  135. $this->assertSame('http://example.com', $request->getUri()->__toString());
  136. $uri = 'http://example.com/test';
  137. $request = new Request();
  138. $request = $request->withUri(new Uri($uri));
  139. $this->assertSame($uri, $request->url());
  140. $this->assertSame($uri, $request->getUri()->__toString());
  141. });
  142. }
  143. /**
  144. * test method method.
  145. *
  146. * @group deprecated
  147. * @return void
  148. */
  149. public function testMethod()
  150. {
  151. $this->deprecated(function () {
  152. $request = new Request();
  153. $this->assertSame($request, $request->method(Request::METHOD_GET));
  154. $this->assertEquals(Request::METHOD_GET, $request->method());
  155. });
  156. }
  157. /**
  158. * test method interoperability.
  159. *
  160. * @group deprecated
  161. * @return void
  162. */
  163. public function testMethodInteroperability()
  164. {
  165. $this->deprecated(function () {
  166. $request = new Request();
  167. $this->assertSame($request, $request->method(Request::METHOD_GET));
  168. $this->assertEquals(Request::METHOD_GET, $request->method());
  169. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  170. $request = $request->withMethod(Request::METHOD_GET);
  171. $this->assertEquals(Request::METHOD_GET, $request->method());
  172. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  173. });
  174. }
  175. /**
  176. * test invalid method.
  177. *
  178. * @group deprecated
  179. * @return void
  180. */
  181. public function testMethodInvalid()
  182. {
  183. $this->expectException(\Cake\Core\Exception\Exception::class);
  184. $this->deprecated(function () {
  185. $request = new Request();
  186. $request->method('set on fire');
  187. });
  188. }
  189. /**
  190. * test body method.
  191. *
  192. * @return void
  193. */
  194. public function testBody()
  195. {
  196. $data = '{"json":"data"}';
  197. $request = new Request();
  198. $this->assertSame($request, $request->body($data));
  199. $this->assertEquals($data, $request->body());
  200. }
  201. /**
  202. * test body method with array payload
  203. *
  204. * @group deprecated
  205. * @return void
  206. */
  207. public function testBodyArray()
  208. {
  209. $request = new Request();
  210. $data = [
  211. 'a' => 'b',
  212. 'c' => 'd',
  213. 'e' => ['f', 'g']
  214. ];
  215. $request->body($data);
  216. $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('content-type'));
  217. $this->assertEquals(
  218. 'a=b&c=d&e%5B0%5D=f&e%5B1%5D=g',
  219. $request->body(),
  220. 'Body should be serialized'
  221. );
  222. }
  223. /**
  224. * Test that body() modifies the PSR7 stream
  225. *
  226. * @return void
  227. */
  228. public function testBodyInteroperability()
  229. {
  230. $request = new Request();
  231. $this->assertSame('', $request->body());
  232. $data = '{"json":"data"}';
  233. $request = new Request();
  234. $request->body($data);
  235. $this->assertSame($data, $request->body());
  236. $this->assertSame($data, '' . $request->getBody());
  237. }
  238. /**
  239. * test header method.
  240. *
  241. * @group deprecated
  242. * @return void
  243. */
  244. public function testHeader()
  245. {
  246. $this->deprecated(function () {
  247. $request = new Request();
  248. $type = 'application/json';
  249. $result = $request->header('Content-Type', $type);
  250. $this->assertSame($result, $request, 'Should return self');
  251. $result = $request->header('content-type');
  252. $this->assertEquals($type, $result, 'lowercase does not work');
  253. $result = $request->header('ConTent-typE');
  254. $this->assertEquals($type, $result, 'Funny casing does not work');
  255. $result = $request->header([
  256. 'Connection' => 'close',
  257. 'user-agent' => 'CakePHP'
  258. ]);
  259. $this->assertSame($result, $request, 'Should return self');
  260. $this->assertEquals('close', $request->header('connection'));
  261. $this->assertEquals('CakePHP', $request->header('USER-AGENT'));
  262. $this->assertNull($request->header('not set'));
  263. });
  264. }
  265. /**
  266. * Test the default headers
  267. *
  268. * @return void
  269. */
  270. public function testDefaultHeaders()
  271. {
  272. $request = new Request();
  273. $this->assertEquals('CakePHP', $request->getHeaderLine('User-Agent'));
  274. $this->assertEquals('close', $request->getHeaderLine('Connection'));
  275. }
  276. /**
  277. * Test that header() and PSR7 methods play nice.
  278. *
  279. * @group deprecated
  280. * @return void
  281. */
  282. public function testHeaderMethodInteroperability()
  283. {
  284. $this->deprecated(function () {
  285. $request = new Request();
  286. $request->header('Content-Type', 'application/json');
  287. $this->assertEquals('application/json', $request->header('Content-Type'), 'Old getter should work');
  288. $this->assertEquals('application/json', $request->getHeaderLine('Content-Type'), 'getHeaderLine works');
  289. $this->assertEquals('application/json', $request->getHeaderLine('content-type'), 'getHeaderLine works');
  290. $this->assertEquals(['application/json'], $request->getHeader('Content-Type'), 'getHeader works');
  291. $this->assertEquals(['application/json'], $request->getHeader('content-type'), 'getHeader works');
  292. });
  293. }
  294. /**
  295. * test cookie method.
  296. *
  297. * @group deprecated
  298. * @return void
  299. */
  300. public function testCookie()
  301. {
  302. $this->deprecated(function () {
  303. $request = new Request();
  304. $result = $request->cookie('session', '123456');
  305. $this->assertSame($result, $request, 'Should return self');
  306. $this->assertNull($request->cookie('not set'));
  307. $result = $request->cookie('session');
  308. $this->assertEquals('123456', $result);
  309. });
  310. }
  311. /**
  312. * test version method.
  313. *
  314. * @group deprecated
  315. * @return void
  316. */
  317. public function testVersion()
  318. {
  319. $this->deprecated(function () {
  320. $request = new Request();
  321. $result = $request->version('1.0');
  322. $this->assertSame($request, $result, 'Should return self');
  323. $this->assertSame('1.0', $request->version());
  324. });
  325. }
  326. /**
  327. * test version Interoperable.
  328. *
  329. * @group deprecated
  330. * @return void
  331. */
  332. public function testVersionInteroperability()
  333. {
  334. $this->deprecated(function () {
  335. $request = new Request();
  336. $this->assertEquals('1.1', $request->version());
  337. $this->assertEquals('1.1', $request->getProtocolVersion());
  338. $request = $request->withProtocolVersion('1.0');
  339. $this->assertEquals('1.0', $request->version());
  340. $this->assertEquals('1.0', $request->getProtocolVersion());
  341. });
  342. }
  343. }