FormDataTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\FormData;
  17. use Cake\TestSuite\TestCase;
  18. use Laminas\Diactoros\UploadedFile;
  19. /**
  20. * Test case for FormData.
  21. */
  22. class FormDataTest extends TestCase
  23. {
  24. /**
  25. * Test getting the boundary.
  26. */
  27. public function testBoundary(): void
  28. {
  29. $data = new FormData();
  30. $result = $data->boundary();
  31. $this->assertMatchesRegularExpression('/^[a-f0-9]{32}$/', $result);
  32. $result2 = $data->boundary();
  33. $this->assertSame($result, $result2);
  34. }
  35. /**
  36. * test adding parts returns this.
  37. */
  38. public function testAddReturnThis(): void
  39. {
  40. $data = new FormData();
  41. $return = $data->add('test', 'value');
  42. $this->assertSame($data, $return);
  43. }
  44. /**
  45. * Test adding parts that are simple.
  46. */
  47. public function testAddSimple(): void
  48. {
  49. $data = new FormData();
  50. $data->add('test', 'value')
  51. ->add('empty', '')
  52. ->add('int', 1)
  53. ->add('float', 2.3)
  54. ->add('password', '@secret');
  55. $this->assertCount(5, $data);
  56. $result = (string)$data;
  57. $expected = 'test=value&empty=&int=1&float=2.3&password=%40secret';
  58. $this->assertSame($expected, $result);
  59. }
  60. /**
  61. * Test addMany method.
  62. */
  63. public function testAddMany(): void
  64. {
  65. $data = new FormData();
  66. $array = [
  67. 'key' => 'value',
  68. 'empty' => '',
  69. 'int' => '1',
  70. 'float' => '2.3',
  71. ];
  72. $data->addMany($array);
  73. $this->assertCount(4, $data);
  74. $result = (string)$data;
  75. $expected = 'key=value&empty=&int=1&float=2.3';
  76. $this->assertSame($expected, $result);
  77. }
  78. /**
  79. * Test adding a part object.
  80. */
  81. public function testAddPartObject(): void
  82. {
  83. $data = new FormData();
  84. $boundary = $data->boundary();
  85. $part = $data->newPart('test', 'value');
  86. $part->contentId('abc123');
  87. $data->add($part);
  88. $this->assertTrue($data->isMultipart());
  89. $this->assertFalse($data->hasFile());
  90. $this->assertCount(1, $data, 'Should have 1 part');
  91. $expected = [
  92. '--' . $boundary,
  93. 'Content-Disposition: form-data; name="test"',
  94. 'Content-ID: <abc123>',
  95. '',
  96. 'value',
  97. '--' . $boundary . '--',
  98. '',
  99. ];
  100. $this->assertSame(implode("\r\n", $expected), (string)$data);
  101. }
  102. /**
  103. * Test adding parts that are arrays.
  104. */
  105. public function testAddArray(): void
  106. {
  107. $data = new FormData();
  108. $data->add('Article', [
  109. 'title' => 'first post',
  110. 'published' => 'Y',
  111. 'tags' => ['blog', 'cakephp'],
  112. ]);
  113. $result = (string)$data;
  114. $expected = 'Article%5Btitle%5D=first+post&Article%5Bpublished%5D=Y&' .
  115. 'Article%5Btags%5D%5B0%5D=blog&Article%5Btags%5D%5B1%5D=cakephp';
  116. $this->assertSame($expected, $result);
  117. }
  118. /**
  119. * Test adding a part with a file in it.
  120. */
  121. public function testAddFile(): void
  122. {
  123. $file = CORE_PATH . 'VERSION.txt';
  124. $contents = file_get_contents($file);
  125. $data = new FormData();
  126. $data->addFile('upload', fopen($file, 'r'));
  127. $boundary = $data->boundary();
  128. $result = (string)$data;
  129. $expected = [
  130. '--' . $boundary,
  131. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  132. 'Content-Type: text/plain; charset=us-ascii',
  133. '',
  134. $contents,
  135. '--' . $boundary . '--',
  136. '',
  137. ];
  138. $this->assertSame(implode("\r\n", $expected), $result);
  139. }
  140. /**
  141. * Test adding a part with a filehandle.
  142. */
  143. public function testAddFileHandle(): void
  144. {
  145. $file = CORE_PATH . 'VERSION.txt';
  146. $fh = fopen($file, 'r');
  147. $data = new FormData();
  148. $data->add('upload', $fh);
  149. $boundary = $data->boundary();
  150. $result = (string)$data;
  151. rewind($fh);
  152. $contents = stream_get_contents($fh);
  153. $expected = [
  154. '--' . $boundary,
  155. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  156. 'Content-Type: text/plain; charset=us-ascii',
  157. '',
  158. $contents,
  159. '--' . $boundary . '--',
  160. '',
  161. ];
  162. $this->assertSame(implode("\r\n", $expected), $result);
  163. }
  164. /**
  165. * Test adding a part with a UploadedFileInterface instance.
  166. */
  167. public function testAddFileUploadedFile(): void
  168. {
  169. $file = new UploadedFile(
  170. CORE_PATH . 'VERSION.txt',
  171. filesize(CORE_PATH . 'VERSION.txt'),
  172. 0,
  173. 'VERSION.txt',
  174. 'text/plain'
  175. );
  176. $data = new FormData();
  177. $data->add('upload', $file);
  178. $boundary = $data->boundary();
  179. $result = (string)$data;
  180. $expected = [
  181. '--' . $boundary,
  182. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  183. 'Content-Type: text/plain',
  184. '',
  185. (string)$file->getStream(),
  186. '--' . $boundary . '--',
  187. '',
  188. ];
  189. $this->assertSame(implode("\r\n", $expected), $result);
  190. }
  191. /**
  192. * Test contentType method.
  193. */
  194. public function testContentType(): void
  195. {
  196. $data = new FormData();
  197. $data->add('key', 'value');
  198. $result = $data->contentType();
  199. $expected = 'application/x-www-form-urlencoded';
  200. $this->assertSame($expected, $result);
  201. $file = CORE_PATH . 'VERSION.txt';
  202. $data = new FormData();
  203. $data->addFile('upload', fopen($file, 'r'));
  204. $boundary = $data->boundary();
  205. $result = $data->contentType();
  206. $expected = 'multipart/form-data; boundary=' . $boundary;
  207. $this->assertSame($expected, $result);
  208. }
  209. }