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