FormDataTest.php 6.2 KB

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