FormDataTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. $file = CORE_PATH . 'VERSION.txt';
  131. $contents = file_get_contents($file);
  132. $data = new FormData();
  133. $data->add('Article', [
  134. 'title' => 'first post',
  135. 'thumbnail' => '@' . $file
  136. ]);
  137. $boundary = $data->boundary();
  138. $result = (string)$data;
  139. $expected = [
  140. '--' . $boundary,
  141. 'Content-Disposition: form-data; name="Article[title]"',
  142. '',
  143. 'first post',
  144. '--' . $boundary,
  145. 'Content-Disposition: form-data; name="Article[thumbnail]"; filename="VERSION.txt"',
  146. 'Content-Type: text/plain; charset=us-ascii',
  147. '',
  148. $contents,
  149. '--' . $boundary . '--',
  150. '',
  151. '',
  152. ];
  153. $this->assertEquals(implode("\r\n", $expected), $result);
  154. }
  155. /**
  156. * Test adding a part with a file in it.
  157. *
  158. * @return void
  159. */
  160. public function testAddFile()
  161. {
  162. $file = CORE_PATH . 'VERSION.txt';
  163. $contents = file_get_contents($file);
  164. $data = new FormData();
  165. $data->add('upload', '@' . $file);
  166. $boundary = $data->boundary();
  167. $result = (string)$data;
  168. $expected = [
  169. '--' . $boundary,
  170. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  171. 'Content-Type: text/plain; charset=us-ascii',
  172. '',
  173. $contents,
  174. '--' . $boundary . '--',
  175. '',
  176. ''
  177. ];
  178. $this->assertEquals(implode("\r\n", $expected), $result);
  179. }
  180. /**
  181. * Test adding a part with a filehandle.
  182. *
  183. * @return void
  184. */
  185. public function testAddFileHandle()
  186. {
  187. $file = CORE_PATH . 'VERSION.txt';
  188. $fh = fopen($file, 'r');
  189. $data = new FormData();
  190. $data->add('upload', $fh);
  191. $boundary = $data->boundary();
  192. $result = (string)$data;
  193. rewind($fh);
  194. $contents = stream_get_contents($fh);
  195. $expected = [
  196. '--' . $boundary,
  197. 'Content-Disposition: form-data; name="upload"',
  198. 'Content-Type: application/octet-stream',
  199. '',
  200. $contents,
  201. '--' . $boundary . '--',
  202. '',
  203. ''
  204. ];
  205. $this->assertEquals(implode("\r\n", $expected), $result);
  206. }
  207. }