FormDataTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http\Client;
  15. use Cake\Http\Client\FormData;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * Test case for FormData.
  19. */
  20. class FormDataTest extends TestCase
  21. {
  22. /**
  23. * Test getting the boundary.
  24. *
  25. * @return void
  26. */
  27. public function testBoundary()
  28. {
  29. $data = new FormData();
  30. $result = $data->boundary();
  31. $this->assertRegExp('/^[a-f0-9]{32}$/', $result);
  32. $result2 = $data->boundary();
  33. $this->assertEquals($result, $result2);
  34. }
  35. /**
  36. * test adding parts returns this.
  37. *
  38. * @return void
  39. */
  40. public function testAddReturnThis()
  41. {
  42. $data = new FormData();
  43. $return = $data->add('test', 'value');
  44. $this->assertSame($data, $return);
  45. }
  46. /**
  47. * Test adding parts that are simple.
  48. *
  49. * @return void
  50. */
  51. public function testAddSimple()
  52. {
  53. $data = new FormData();
  54. $data->add('test', 'value')
  55. ->add('empty', '')
  56. ->add('int', 1)
  57. ->add('float', 2.3);
  58. $this->assertCount(4, $data);
  59. $boundary = $data->boundary();
  60. $result = (string)$data;
  61. $expected = 'test=value&empty=&int=1&float=2.3';
  62. $this->assertEquals($expected, $result);
  63. }
  64. /**
  65. * Test addMany method.
  66. *
  67. * @return void
  68. */
  69. public function testAddMany()
  70. {
  71. $data = new FormData();
  72. $array = [
  73. 'key' => 'value',
  74. 'empty' => '',
  75. 'int' => '1',
  76. 'float' => '2.3'
  77. ];
  78. $data->addMany($array);
  79. $this->assertCount(4, $data);
  80. $result = (string)$data;
  81. $expected = 'key=value&empty=&int=1&float=2.3';
  82. $this->assertEquals($expected, $result);
  83. }
  84. /**
  85. * Test adding a part object.
  86. *
  87. * @return void
  88. */
  89. public function testAddPartObject()
  90. {
  91. $data = new FormData();
  92. $boundary = $data->boundary();
  93. $part = $data->newPart('test', 'value');
  94. $part->contentId('abc123');
  95. $data->add($part);
  96. $this->assertTrue($data->isMultipart());
  97. $this->assertFalse($data->hasFile());
  98. $this->assertCount(1, $data, 'Should have 1 part');
  99. $expected = [
  100. '--' . $boundary,
  101. 'Content-Disposition: form-data; name="test"',
  102. 'Content-ID: <abc123>',
  103. '',
  104. 'value',
  105. '--' . $boundary . '--',
  106. '',
  107. '',
  108. ];
  109. $this->assertEquals(implode("\r\n", $expected), (string)$data);
  110. }
  111. /**
  112. * Test adding parts that are arrays.
  113. *
  114. * @return void
  115. */
  116. public function testAddArray()
  117. {
  118. $data = new FormData();
  119. $data->add('Article', [
  120. 'title' => 'first post',
  121. 'published' => 'Y',
  122. 'tags' => ['blog', 'cakephp']
  123. ]);
  124. $result = (string)$data;
  125. $expected = 'Article%5Btitle%5D=first+post&Article%5Bpublished%5D=Y&' .
  126. 'Article%5Btags%5D%5B0%5D=blog&Article%5Btags%5D%5B1%5D=cakephp';
  127. $this->assertEquals($expected, $result);
  128. }
  129. /**
  130. * Test adding a part with a file in it.
  131. *
  132. * @return void
  133. */
  134. public function testAddArrayWithFile()
  135. {
  136. $errorLevel = error_reporting();
  137. error_reporting($errorLevel & ~E_USER_DEPRECATED);
  138. $file = CORE_PATH . 'VERSION.txt';
  139. $contents = file_get_contents($file);
  140. $data = new FormData();
  141. $data->add('Article', [
  142. 'title' => 'first post',
  143. 'thumbnail' => '@' . $file
  144. ]);
  145. $boundary = $data->boundary();
  146. $result = (string)$data;
  147. $expected = [
  148. '--' . $boundary,
  149. 'Content-Disposition: form-data; name="Article[title]"',
  150. '',
  151. 'first post',
  152. '--' . $boundary,
  153. 'Content-Disposition: form-data; name="Article[thumbnail]"; filename="VERSION.txt"',
  154. 'Content-Type: text/plain; charset=us-ascii',
  155. '',
  156. $contents,
  157. '--' . $boundary . '--',
  158. '',
  159. '',
  160. ];
  161. $this->assertEquals(implode("\r\n", $expected), $result);
  162. error_reporting($errorLevel);
  163. }
  164. /**
  165. * Test adding a part with a file in it.
  166. *
  167. * @return void
  168. */
  169. public function testAddFile()
  170. {
  171. $file = CORE_PATH . 'VERSION.txt';
  172. $contents = file_get_contents($file);
  173. $data = new FormData();
  174. $data->addFile('upload', fopen($file, 'r'));
  175. $boundary = $data->boundary();
  176. $result = (string)$data;
  177. $expected = [
  178. '--' . $boundary,
  179. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  180. 'Content-Type: text/plain; charset=us-ascii',
  181. '',
  182. $contents,
  183. '--' . $boundary . '--',
  184. '',
  185. ''
  186. ];
  187. $this->assertEquals(implode("\r\n", $expected), $result);
  188. }
  189. /**
  190. * Test adding a part with a filehandle.
  191. *
  192. * @return void
  193. */
  194. public function testAddFileHandle()
  195. {
  196. $file = CORE_PATH . 'VERSION.txt';
  197. $fh = fopen($file, 'r');
  198. $data = new FormData();
  199. $data->add('upload', $fh);
  200. $boundary = $data->boundary();
  201. $result = (string)$data;
  202. rewind($fh);
  203. $contents = stream_get_contents($fh);
  204. $expected = [
  205. '--' . $boundary,
  206. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  207. 'Content-Type: text/plain; charset=us-ascii',
  208. '',
  209. $contents,
  210. '--' . $boundary . '--',
  211. '',
  212. ''
  213. ];
  214. $this->assertEquals(implode("\r\n", $expected), $result);
  215. }
  216. /**
  217. * Test contentType method.
  218. *
  219. * @return void
  220. */
  221. public function testContentType()
  222. {
  223. $data = new FormData();
  224. $data->add('key', 'value');
  225. $result = $data->contentType();
  226. $expected = 'application/x-www-form-urlencoded';
  227. $this->assertEquals($expected, $result);
  228. $file = CORE_PATH . 'VERSION.txt';
  229. $data = new FormData();
  230. $data->addFile('upload', fopen($file, 'r'));
  231. $boundary = $data->boundary();
  232. $result = $data->contentType();
  233. $expected = 'multipart/form-data; boundary="' . $boundary . '"';
  234. $this->assertEquals($expected, $result);
  235. }
  236. }