RequestTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http\Client;
  16. use Cake\Http\Client\Request;
  17. use Cake\TestSuite\TestCase;
  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->assertSame('http://example.com', (string)$request->getUri());
  37. $this->assertStringContainsString($request->getMethod(), 'POST');
  38. $this->assertSame('application/json', $request->getHeaderLine('Content-Type'));
  39. $this->assertSame(json_encode($data), $request->getBody()->__toString());
  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->assertSame($request->getMethod(), $method);
  52. $this->assertSame('http://example.com', (string)$request->getUri());
  53. $this->assertSame('application/json', $request->getHeaderLine('Content-Type'));
  54. $this->assertSame(json_encode($data), $request->getBody()->__toString());
  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->assertSame('http://example.com', (string)$request->getUri());
  87. $this->assertSame('POST', $request->getMethod());
  88. $this->assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
  89. $this->assertSame('a=b&c=d', $request->getBody()->__toString());
  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->assertSame('http://example.com', (string)$request->getUri());
  105. $this->assertSame('POST', $request->getMethod());
  106. $this->assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
  107. $this->assertSame('a=b&c%5B0%5D=foo&c%5B1%5D=bar', $request->getBody()->__toString());
  108. }
  109. /**
  110. * test body method.
  111. *
  112. * @return void
  113. */
  114. public function testBody()
  115. {
  116. $data = '{"json":"data"}';
  117. $request = new Request('', Request::METHOD_GET, [], $data);
  118. $this->assertSame($data, $request->getBody()->__toString());
  119. }
  120. /**
  121. * test body method with array payload
  122. *
  123. * @return void
  124. */
  125. public function testBodyArray()
  126. {
  127. $data = [
  128. 'a' => 'b',
  129. 'c' => 'd',
  130. 'e' => ['f', 'g'],
  131. ];
  132. $request = new Request('', Request::METHOD_GET, [], $data);
  133. $this->assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('content-type'));
  134. $this->assertSame(
  135. 'a=b&c=d&e%5B0%5D=f&e%5B1%5D=g',
  136. $request->getBody()->__toString(),
  137. 'Body should be serialized'
  138. );
  139. }
  140. /**
  141. * Test that body() modifies the PSR7 stream
  142. *
  143. * @return void
  144. */
  145. public function testBodyInteroperability()
  146. {
  147. $request = new Request();
  148. $this->assertSame('', $request->getBody()->__toString());
  149. $data = '{"json":"data"}';
  150. $request = new Request('', Request::METHOD_GET, [], $data);
  151. $this->assertSame($data, $request->getBody()->__toString());
  152. }
  153. /**
  154. * Test the default headers
  155. *
  156. * @return void
  157. */
  158. public function testDefaultHeaders()
  159. {
  160. $request = new Request();
  161. $this->assertSame('CakePHP', $request->getHeaderLine('User-Agent'));
  162. $this->assertSame('close', $request->getHeaderLine('Connection'));
  163. }
  164. }