RequestTest.php 5.6 KB

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