FormDataTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. $this->assertEquals(implode("\r\n", $expected), (string)$data);
  110. }
  111. /**
  112. * Test adding parts that are arrays.
  113. *
  114. * @return void
  115. */
  116. public function testAddArray()
  117. {
  118. $data = new FormData();
  119. $data->add('Article', [
  120. 'title' => 'first post',
  121. 'published' => 'Y',
  122. 'tags' => ['blog', 'cakephp']
  123. ]);
  124. $result = (string)$data;
  125. $expected = 'Article%5Btitle%5D=first+post&Article%5Bpublished%5D=Y&' .
  126. 'Article%5Btags%5D%5B0%5D=blog&Article%5Btags%5D%5B1%5D=cakephp';
  127. $this->assertEquals($expected, $result);
  128. }
  129. /**
  130. * Test adding a part with a file in it.
  131. *
  132. * @return void
  133. */
  134. public function testAddFile()
  135. {
  136. $file = CORE_PATH . 'VERSION.txt';
  137. $contents = file_get_contents($file);
  138. $data = new FormData();
  139. $data->addFile('upload', fopen($file, 'r'));
  140. $boundary = $data->boundary();
  141. $result = (string)$data;
  142. $expected = [
  143. '--' . $boundary,
  144. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  145. 'Content-Type: text/plain; charset=us-ascii',
  146. '',
  147. $contents,
  148. '--' . $boundary . '--',
  149. '',
  150. ];
  151. $this->assertEquals(implode("\r\n", $expected), $result);
  152. }
  153. /**
  154. * Test adding a part with a filehandle.
  155. *
  156. * @return void
  157. */
  158. public function testAddFileHandle()
  159. {
  160. $file = CORE_PATH . 'VERSION.txt';
  161. $fh = fopen($file, 'r');
  162. $data = new FormData();
  163. $data->add('upload', $fh);
  164. $boundary = $data->boundary();
  165. $result = (string)$data;
  166. rewind($fh);
  167. $contents = stream_get_contents($fh);
  168. $expected = [
  169. '--' . $boundary,
  170. 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
  171. 'Content-Type: text/plain; charset=us-ascii',
  172. '',
  173. $contents,
  174. '--' . $boundary . '--',
  175. '',
  176. ];
  177. $this->assertEquals(implode("\r\n", $expected), $result);
  178. }
  179. /**
  180. * Test contentType method.
  181. *
  182. * @return void
  183. */
  184. public function testContentType()
  185. {
  186. $data = new FormData();
  187. $data->add('key', 'value');
  188. $result = $data->contentType();
  189. $expected = 'application/x-www-form-urlencoded';
  190. $this->assertEquals($expected, $result);
  191. $file = CORE_PATH . 'VERSION.txt';
  192. $data = new FormData();
  193. $data->addFile('upload', fopen($file, 'r'));
  194. $boundary = $data->boundary();
  195. $result = $data->contentType();
  196. $expected = 'multipart/form-data; boundary=' . $boundary;
  197. $this->assertEquals($expected, $result);
  198. }
  199. }