FormDataTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http\Client;
  15. use Cake\Http\Client\FormData;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * Test case for FormData.
  19. */
  20. class FormDataTest extends TestCase
  21. {
  22. /**
  23. * Test getting the boundary.
  24. *
  25. * @return void
  26. */
  27. public function testBoundary()
  28. {
  29. $data = new FormData();
  30. $result = $data->boundary();
  31. $this->assertRegExp('/^[a-f0-9]{32}$/', $result);
  32. $result2 = $data->boundary();
  33. $this->assertEquals($result, $result2);
  34. }
  35. /**
  36. * test adding parts returns this.
  37. *
  38. * @return void
  39. */
  40. public function testAddReturnThis()
  41. {
  42. $data = new FormData();
  43. $return = $data->add('test', 'value');
  44. $this->assertSame($data, $return);
  45. }
  46. /**
  47. * Test adding parts that are simple.
  48. *
  49. * @return void
  50. */
  51. public function testAddSimple()
  52. {
  53. $data = new FormData();
  54. $data->add('test', 'value')
  55. ->add('empty', '')
  56. ->add('int', 1)
  57. ->add('float', 2.3)
  58. ->add('password', '@secret');
  59. $this->assertCount(5, $data);
  60. $boundary = $data->boundary();
  61. $result = (string)$data;
  62. $expected = 'test=value&empty=&int=1&float=2.3&password=%40secret';
  63. $this->assertEquals($expected, $result);
  64. }
  65. /**
  66. * Test addMany method.
  67. *
  68. * @return void
  69. */
  70. public function testAddMany()
  71. {
  72. $data = new FormData();
  73. $array = [
  74. 'key' => 'value',
  75. 'empty' => '',
  76. 'int' => '1',
  77. 'float' => '2.3'
  78. ];
  79. $data->addMany($array);
  80. $this->assertCount(4, $data);
  81. $result = (string)$data;
  82. $expected = 'key=value&empty=&int=1&float=2.3';
  83. $this->assertEquals($expected, $result);
  84. }
  85. /**
  86. * Test adding a part object.
  87. *
  88. * @return void
  89. */
  90. public function testAddPartObject()
  91. {
  92. $data = new FormData();
  93. $boundary = $data->boundary();
  94. $part = $data->newPart('test', 'value');
  95. $part->contentId('abc123');
  96. $data->add($part);
  97. $this->assertTrue($data->isMultipart());
  98. $this->assertFalse($data->hasFile());
  99. $this->assertCount(1, $data, 'Should have 1 part');
  100. $expected = [
  101. '--' . $boundary,
  102. 'Content-Disposition: form-data; name="test"',
  103. 'Content-ID: <abc123>',
  104. '',
  105. 'value',
  106. '--' . $boundary . '--',
  107. '',
  108. '',
  109. ];
  110. $this->assertEquals(implode("\r\n", $expected), (string)$data);
  111. }
  112. /**
  113. * Test adding parts that are arrays.
  114. *
  115. * @return void
  116. */
  117. public function testAddArray()
  118. {
  119. $data = new FormData();
  120. $data->add('Article', [
  121. 'title' => 'first post',
  122. 'published' => 'Y',
  123. 'tags' => ['blog', 'cakephp']
  124. ]);
  125. $result = (string)$data;
  126. $expected = 'Article%5Btitle%5D=first+post&Article%5Bpublished%5D=Y&' .
  127. 'Article%5Btags%5D%5B0%5D=blog&Article%5Btags%5D%5B1%5D=cakephp';
  128. $this->assertEquals($expected, $result);
  129. }
  130. /**
  131. * Test adding a part with a file in it.
  132. *
  133. * @return void
  134. */
  135. public function testAddFile()
  136. {
  137. $file = CORE_PATH . 'VERSION.txt';
  138. $contents = file_get_contents($file);
  139. $data = new FormData();
  140. $data->addFile('upload', fopen($file, 'r'));
  141. $boundary = $data->boundary();
  142. $result = (string)$data;
  143. $expected = [
  144. '--' . $boundary,
  145. 'Content-Disposition: form-data; name="upload"; 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 filehandle.
  157. *
  158. * @return void
  159. */
  160. public function testAddFileHandle()
  161. {
  162. $file = CORE_PATH . 'VERSION.txt';
  163. $fh = fopen($file, 'r');
  164. $data = new FormData();
  165. $data->add('upload', $fh);
  166. $boundary = $data->boundary();
  167. $result = (string)$data;
  168. rewind($fh);
  169. $contents = stream_get_contents($fh);
  170. $expected = [
  171. '--' . $boundary,
  172. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  173. 'Content-Type: text/plain; charset=us-ascii',
  174. '',
  175. $contents,
  176. '--' . $boundary . '--',
  177. '',
  178. ''
  179. ];
  180. $this->assertEquals(implode("\r\n", $expected), $result);
  181. }
  182. /**
  183. * Test contentType method.
  184. *
  185. * @return void
  186. */
  187. public function testContentType()
  188. {
  189. $data = new FormData();
  190. $data->add('key', 'value');
  191. $result = $data->contentType();
  192. $expected = 'application/x-www-form-urlencoded';
  193. $this->assertEquals($expected, $result);
  194. $file = CORE_PATH . 'VERSION.txt';
  195. $data = new FormData();
  196. $data->addFile('upload', fopen($file, 'r'));
  197. $boundary = $data->boundary();
  198. $result = $data->contentType();
  199. $expected = 'multipart/form-data; boundary="' . $boundary . '"';
  200. $this->assertEquals($expected, $result);
  201. }
  202. }