FormDataTest.php 5.9 KB

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