FormDataTest.php 4.6 KB

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