RequestTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. public function testConstructorStringData(): void
  27. {
  28. $headers = [
  29. 'Content-Type' => 'application/json',
  30. 'Authorization' => 'Bearer valid-token',
  31. ];
  32. $data = ['a' => 'b', 'c' => 'd'];
  33. $request = new Request('http://example.com', 'POST', $headers, json_encode($data));
  34. $this->assertSame('http://example.com', (string)$request->getUri());
  35. $this->assertStringContainsString($request->getMethod(), 'POST');
  36. $this->assertSame('application/json', $request->getHeaderLine('Content-Type'));
  37. $this->assertSame(json_encode($data), $request->getBody()->__toString());
  38. }
  39. /**
  40. * @param array $headers The HTTP headers to set.
  41. * @param array|string|null $data The request body to use.
  42. * @param string $method The HTTP method to use.
  43. * @dataProvider additionProvider
  44. */
  45. public function testMethods(array $headers, $data, $method): void
  46. {
  47. $request = new Request('http://example.com', $method, $headers, json_encode($data));
  48. $this->assertSame($request->getMethod(), $method);
  49. $this->assertSame('http://example.com', (string)$request->getUri());
  50. $this->assertSame('application/json', $request->getHeaderLine('Content-Type'));
  51. $this->assertSame(json_encode($data), $request->getBody()->__toString());
  52. }
  53. /**
  54. * @dataProvider additionProvider
  55. */
  56. public static function additionProvider(): array
  57. {
  58. $headers = [
  59. 'Content-Type' => 'application/json',
  60. 'Authorization' => 'Bearer valid-token',
  61. ];
  62. $data = ['a' => 'b', 'c' => 'd'];
  63. return [
  64. [$headers, $data, Request::METHOD_POST],
  65. [$headers, $data, Request::METHOD_GET],
  66. [$headers, $data, Request::METHOD_PUT],
  67. [$headers, $data, Request::METHOD_DELETE],
  68. ];
  69. }
  70. /**
  71. * test array data, header and constructor
  72. */
  73. public function testConstructorArrayData(): void
  74. {
  75. $headers = [
  76. 'Content-Type' => 'application/json',
  77. 'Authorization' => 'Bearer valid-token',
  78. ];
  79. $data = ['a' => 'b', 'c' => 'd'];
  80. $request = new Request('http://example.com', 'POST', $headers, $data);
  81. $this->assertSame('http://example.com', (string)$request->getUri());
  82. $this->assertSame('POST', $request->getMethod());
  83. $this->assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
  84. $this->assertSame('a=b&c=d', $request->getBody()->__toString());
  85. }
  86. /**
  87. * test nested array data for encoding of brackets, header and constructor
  88. */
  89. public function testConstructorArrayNestedData(): void
  90. {
  91. $headers = [
  92. 'Content-Type' => 'application/json',
  93. 'Authorization' => 'Bearer valid-token',
  94. ];
  95. $data = ['a' => 'b', 'c' => ['foo', 'bar']];
  96. $request = new Request('http://example.com', 'POST', $headers, $data);
  97. $this->assertSame('http://example.com', (string)$request->getUri());
  98. $this->assertSame('POST', $request->getMethod());
  99. $this->assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
  100. $this->assertSame('a=b&c%5B0%5D=foo&c%5B1%5D=bar', $request->getBody()->__toString());
  101. }
  102. /**
  103. * test body method.
  104. */
  105. public function testBody(): void
  106. {
  107. $data = '{"json":"data"}';
  108. $request = new Request('', Request::METHOD_GET, [], $data);
  109. $this->assertSame($data, $request->getBody()->__toString());
  110. }
  111. /**
  112. * test body method with array payload
  113. */
  114. public function testBodyArray(): void
  115. {
  116. $data = [
  117. 'a' => 'b',
  118. 'c' => 'd',
  119. 'e' => ['f', 'g'],
  120. ];
  121. $request = new Request('', Request::METHOD_GET, [], $data);
  122. $this->assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('content-type'));
  123. $this->assertSame(
  124. 'a=b&c=d&e%5B0%5D=f&e%5B1%5D=g',
  125. $request->getBody()->__toString(),
  126. 'Body should be serialized'
  127. );
  128. }
  129. /**
  130. * Test that body() modifies the PSR7 stream
  131. */
  132. public function testBodyInteroperability(): void
  133. {
  134. $request = new Request();
  135. $this->assertSame('', $request->getBody()->__toString());
  136. $data = '{"json":"data"}';
  137. $request = new Request('', Request::METHOD_GET, [], $data);
  138. $this->assertSame($data, $request->getBody()->__toString());
  139. }
  140. /**
  141. * Test the default headers
  142. */
  143. public function testDefaultHeaders(): void
  144. {
  145. $request = new Request();
  146. $this->assertSame('CakePHP', $request->getHeaderLine('User-Agent'));
  147. $this->assertSame('close', $request->getHeaderLine('Connection'));
  148. }
  149. }