FormDataTest.php 6.5 KB

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