FormDataTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Network\Http;
  15. use Cake\Network\Http\FormData;
  16. use Cake\Network\Http\FormData\Part;
  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. $this->assertCount(4, $data);
  60. $boundary = $data->boundary();
  61. $result = (string)$data;
  62. $expected = [
  63. '--' . $boundary,
  64. 'Content-Disposition: form-data; name="test"',
  65. '',
  66. 'value',
  67. '--' . $boundary,
  68. 'Content-Disposition: form-data; name="empty"',
  69. '',
  70. '',
  71. '--' . $boundary,
  72. 'Content-Disposition: form-data; name="int"',
  73. '',
  74. '1',
  75. '--' . $boundary,
  76. 'Content-Disposition: form-data; name="float"',
  77. '',
  78. '2.3',
  79. '--' . $boundary . '--',
  80. '',
  81. '',
  82. ];
  83. $this->assertEquals(implode("\r\n", $expected), $result);
  84. }
  85. /**
  86. * Test adding parts that are arrays.
  87. *
  88. * @return void
  89. */
  90. public function testAddArray()
  91. {
  92. $data = new FormData();
  93. $data->add('Article', [
  94. 'title' => 'first post',
  95. 'published' => 'Y',
  96. 'tags' => ['blog', 'cakephp']
  97. ]);
  98. $boundary = $data->boundary();
  99. $result = (string)$data;
  100. $expected = [
  101. '--' . $boundary,
  102. 'Content-Disposition: form-data; name="Article[title]"',
  103. '',
  104. 'first post',
  105. '--' . $boundary,
  106. 'Content-Disposition: form-data; name="Article[published]"',
  107. '',
  108. 'Y',
  109. '--' . $boundary,
  110. 'Content-Disposition: form-data; name="Article[tags][0]"',
  111. '',
  112. 'blog',
  113. '--' . $boundary,
  114. 'Content-Disposition: form-data; name="Article[tags][1]"',
  115. '',
  116. 'cakephp',
  117. '--' . $boundary . '--',
  118. '',
  119. '',
  120. ];
  121. $this->assertEquals(implode("\r\n", $expected), $result);
  122. }
  123. /**
  124. * Test adding a part with a file in it.
  125. *
  126. * @return void
  127. */
  128. public function testAddArrayWithFile()
  129. {
  130. $errorLevel = error_reporting();
  131. error_reporting($errorLevel & ~E_USER_DEPRECATED);
  132. $file = CORE_PATH . 'VERSION.txt';
  133. $contents = file_get_contents($file);
  134. $data = new FormData();
  135. $data->add('Article', [
  136. 'title' => 'first post',
  137. 'thumbnail' => '@' . $file
  138. ]);
  139. $boundary = $data->boundary();
  140. $result = (string)$data;
  141. $expected = [
  142. '--' . $boundary,
  143. 'Content-Disposition: form-data; name="Article[title]"',
  144. '',
  145. 'first post',
  146. '--' . $boundary,
  147. 'Content-Disposition: form-data; name="Article[thumbnail]"; filename="VERSION.txt"',
  148. 'Content-Type: text/plain; charset=us-ascii',
  149. '',
  150. $contents,
  151. '--' . $boundary . '--',
  152. '',
  153. '',
  154. ];
  155. $this->assertEquals(implode("\r\n", $expected), $result);
  156. error_reporting($errorLevel);
  157. }
  158. /**
  159. * Test adding a part with a file in it.
  160. *
  161. * @return void
  162. */
  163. public function testAddFile()
  164. {
  165. $file = CORE_PATH . 'VERSION.txt';
  166. $contents = file_get_contents($file);
  167. $data = new FormData();
  168. $data->add('upload', fopen($file, 'r'));
  169. $boundary = $data->boundary();
  170. $result = (string)$data;
  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 adding a part with a filehandle.
  185. *
  186. * @return void
  187. */
  188. public function testAddFileHandle()
  189. {
  190. $file = CORE_PATH . 'VERSION.txt';
  191. $fh = fopen($file, 'r');
  192. $data = new FormData();
  193. $data->add('upload', $fh);
  194. $boundary = $data->boundary();
  195. $result = (string)$data;
  196. rewind($fh);
  197. $contents = stream_get_contents($fh);
  198. $expected = [
  199. '--' . $boundary,
  200. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  201. 'Content-Type: text/plain; charset=us-ascii',
  202. '',
  203. $contents,
  204. '--' . $boundary . '--',
  205. '',
  206. ''
  207. ];
  208. $this->assertEquals(implode("\r\n", $expected), $result);
  209. }
  210. }