FormDataTest.php 4.7 KB

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