FormDataTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 = 'test=value&empty=&int=1&float=2.3';
  63. $this->assertEquals($expected, $result);
  64. }
  65. /**
  66. * Test adding a part object.
  67. *
  68. * @return void
  69. */
  70. public function testAddPartObject()
  71. {
  72. $data = new FormData();
  73. $boundary = $data->boundary();
  74. $part = $data->newPart('test', 'value');
  75. $part->contentId('abc123');
  76. $data->add($part);
  77. $this->assertTrue($data->isMultipart());
  78. $this->assertFalse($data->hasFile());
  79. $this->assertCount(1, $data, 'Should have 1 part');
  80. $expected = [
  81. '--' . $boundary,
  82. 'Content-Disposition: form-data; name="test"',
  83. 'Content-ID: <abc123>',
  84. '',
  85. 'value',
  86. '--' . $boundary . '--',
  87. '',
  88. '',
  89. ];
  90. $this->assertEquals(implode("\r\n", $expected), (string)$data);
  91. }
  92. /**
  93. * Test adding parts that are arrays.
  94. *
  95. * @return void
  96. */
  97. public function testAddArray()
  98. {
  99. $data = new FormData();
  100. $data->add('Article', [
  101. 'title' => 'first post',
  102. 'published' => 'Y',
  103. 'tags' => ['blog', 'cakephp']
  104. ]);
  105. $result = (string)$data;
  106. $expected = 'Article%5Btitle%5D=first+post&Article%5Bpublished%5D=Y&' .
  107. 'Article%5Btags%5D%5B0%5D=blog&Article%5Btags%5D%5B1%5D=cakephp';
  108. $this->assertEquals($expected, $result);
  109. }
  110. /**
  111. * Test adding a part with a file in it.
  112. *
  113. * @return void
  114. */
  115. public function testAddArrayWithFile()
  116. {
  117. $errorLevel = error_reporting();
  118. error_reporting($errorLevel & ~E_USER_DEPRECATED);
  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 = [
  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. error_reporting($errorLevel);
  144. }
  145. /**
  146. * Test adding a part with a file in it.
  147. *
  148. * @return void
  149. */
  150. public function testAddFile()
  151. {
  152. $file = CORE_PATH . 'VERSION.txt';
  153. $contents = file_get_contents($file);
  154. $data = new FormData();
  155. $data->add('upload', fopen($file, 'r'));
  156. $boundary = $data->boundary();
  157. $result = (string)$data;
  158. $expected = [
  159. '--' . $boundary,
  160. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  161. 'Content-Type: text/plain; charset=us-ascii',
  162. '',
  163. $contents,
  164. '--' . $boundary . '--',
  165. '',
  166. ''
  167. ];
  168. $this->assertEquals(implode("\r\n", $expected), $result);
  169. }
  170. /**
  171. * Test adding a part with a filehandle.
  172. *
  173. * @return void
  174. */
  175. public function testAddFileHandle()
  176. {
  177. $file = CORE_PATH . 'VERSION.txt';
  178. $fh = fopen($file, 'r');
  179. $data = new FormData();
  180. $data->add('upload', $fh);
  181. $boundary = $data->boundary();
  182. $result = (string)$data;
  183. rewind($fh);
  184. $contents = stream_get_contents($fh);
  185. $expected = [
  186. '--' . $boundary,
  187. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  188. 'Content-Type: text/plain; charset=us-ascii',
  189. '',
  190. $contents,
  191. '--' . $boundary . '--',
  192. '',
  193. ''
  194. ];
  195. $this->assertEquals(implode("\r\n", $expected), $result);
  196. }
  197. }