FormDataTest.php 5.8 KB

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