FormDataTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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->assertCount(1, $data, 'Should have 1 part');
  78. $expected = [
  79. '--' . $boundary,
  80. 'Content-Disposition: form-data; name="test"',
  81. 'Content-ID: <abc123>',
  82. '',
  83. 'value',
  84. '--' . $boundary . '--',
  85. '',
  86. '',
  87. ];
  88. $this->assertEquals(implode("\r\n", $expected), (string)$data);
  89. }
  90. /**
  91. * Test adding parts that are arrays.
  92. *
  93. * @return void
  94. */
  95. public function testAddArray()
  96. {
  97. $data = new FormData();
  98. $data->add('Article', [
  99. 'title' => 'first post',
  100. 'published' => 'Y',
  101. 'tags' => ['blog', 'cakephp']
  102. ]);
  103. $result = (string)$data;
  104. $expected = 'Article%5Btitle%5D=first+post&Article%5Bpublished%5D=Y&' .
  105. 'Article%5Btags%5D%5B0%5D=blog&Article%5Btags%5D%5B1%5D=cakephp';
  106. $this->assertEquals($expected, $result);
  107. }
  108. /**
  109. * Test adding a part with a file in it.
  110. *
  111. * @return void
  112. */
  113. public function testAddArrayWithFile()
  114. {
  115. $errorLevel = error_reporting();
  116. error_reporting($errorLevel & ~E_USER_DEPRECATED);
  117. $file = CORE_PATH . 'VERSION.txt';
  118. $contents = file_get_contents($file);
  119. $data = new FormData();
  120. $data->add('Article', [
  121. 'title' => 'first post',
  122. 'thumbnail' => '@' . $file
  123. ]);
  124. $boundary = $data->boundary();
  125. $result = (string)$data;
  126. $expected = [
  127. '--' . $boundary,
  128. 'Content-Disposition: form-data; name="Article[title]"',
  129. '',
  130. 'first post',
  131. '--' . $boundary,
  132. 'Content-Disposition: form-data; name="Article[thumbnail]"; filename="VERSION.txt"',
  133. 'Content-Type: text/plain; charset=us-ascii',
  134. '',
  135. $contents,
  136. '--' . $boundary . '--',
  137. '',
  138. '',
  139. ];
  140. $this->assertEquals(implode("\r\n", $expected), $result);
  141. error_reporting($errorLevel);
  142. }
  143. /**
  144. * Test adding a part with a file in it.
  145. *
  146. * @return void
  147. */
  148. public function testAddFile()
  149. {
  150. $file = CORE_PATH . 'VERSION.txt';
  151. $contents = file_get_contents($file);
  152. $data = new FormData();
  153. $data->add('upload', fopen($file, 'r'));
  154. $boundary = $data->boundary();
  155. $result = (string)$data;
  156. $expected = [
  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. {
  175. $file = CORE_PATH . 'VERSION.txt';
  176. $fh = fopen($file, 'r');
  177. $data = new FormData();
  178. $data->add('upload', $fh);
  179. $boundary = $data->boundary();
  180. $result = (string)$data;
  181. rewind($fh);
  182. $contents = stream_get_contents($fh);
  183. $expected = [
  184. '--' . $boundary,
  185. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  186. 'Content-Type: text/plain; charset=us-ascii',
  187. '',
  188. $contents,
  189. '--' . $boundary . '--',
  190. '',
  191. ''
  192. ];
  193. $this->assertEquals(implode("\r\n", $expected), $result);
  194. }
  195. }