MultipartStreamTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Tests\Psr7;
  4. use GuzzleHttp\Psr7;
  5. use GuzzleHttp\Psr7\MultipartStream;
  6. use PHPUnit\Framework\TestCase;
  7. class MultipartStreamTest extends TestCase
  8. {
  9. public function testCreatesDefaultBoundary(): void
  10. {
  11. $b = new MultipartStream();
  12. self::assertNotEmpty($b->getBoundary());
  13. }
  14. public function testCanProvideBoundary(): void
  15. {
  16. $b = new MultipartStream([], 'foo');
  17. self::assertSame('foo', $b->getBoundary());
  18. }
  19. public function testIsNotWritable(): void
  20. {
  21. $b = new MultipartStream();
  22. self::assertFalse($b->isWritable());
  23. }
  24. public function testCanCreateEmptyStream(): void
  25. {
  26. $b = new MultipartStream();
  27. $boundary = $b->getBoundary();
  28. self::assertSame("--{$boundary}--\r\n", $b->getContents());
  29. self::assertSame(strlen($boundary) + 6, $b->getSize());
  30. }
  31. public function testValidatesFilesArrayElement(): void
  32. {
  33. $this->expectException(\InvalidArgumentException::class);
  34. new MultipartStream([['foo' => 'bar']]);
  35. }
  36. public function testEnsuresFileHasName(): void
  37. {
  38. $this->expectException(\InvalidArgumentException::class);
  39. new MultipartStream([['contents' => 'bar']]);
  40. }
  41. public function testSerializesFields(): void
  42. {
  43. $b = new MultipartStream([
  44. [
  45. 'name' => 'foo',
  46. 'contents' => 'bar',
  47. ],
  48. [
  49. 'name' => 'baz',
  50. 'contents' => 'bam',
  51. ],
  52. ], 'boundary');
  53. $expected = \implode('', [
  54. "--boundary\r\n",
  55. "Content-Disposition: form-data; name=\"foo\"\r\n",
  56. "Content-Length: 3\r\n",
  57. "\r\n",
  58. "bar\r\n",
  59. "--boundary\r\n",
  60. "Content-Disposition: form-data; name=\"baz\"\r\n",
  61. "Content-Length: 3\r\n",
  62. "\r\n",
  63. "bam\r\n",
  64. "--boundary--\r\n",
  65. ]);
  66. self::assertSame($expected, (string) $b);
  67. }
  68. public function testSerializesNonStringFields(): void
  69. {
  70. $b = new MultipartStream([
  71. [
  72. 'name' => 'int',
  73. 'contents' => (int) 1,
  74. ],
  75. [
  76. 'name' => 'bool',
  77. 'contents' => (bool) false,
  78. ],
  79. [
  80. 'name' => 'bool2',
  81. 'contents' => (bool) true,
  82. ],
  83. [
  84. 'name' => 'float',
  85. 'contents' => (float) 1.1,
  86. ],
  87. ], 'boundary');
  88. $expected = \implode('', [
  89. "--boundary\r\n",
  90. "Content-Disposition: form-data; name=\"int\"\r\n",
  91. "Content-Length: 1\r\n",
  92. "\r\n",
  93. "1\r\n",
  94. "--boundary\r\n",
  95. "Content-Disposition: form-data; name=\"bool\"\r\n",
  96. "\r\n",
  97. "\r\n",
  98. '--boundary',
  99. "\r\n",
  100. "Content-Disposition: form-data; name=\"bool2\"\r\n",
  101. "Content-Length: 1\r\n",
  102. "\r\n",
  103. "1\r\n",
  104. "--boundary\r\n",
  105. "Content-Disposition: form-data; name=\"float\"\r\n",
  106. "Content-Length: 3\r\n",
  107. "\r\n",
  108. "1.1\r\n",
  109. "--boundary--\r\n",
  110. '',
  111. ]);
  112. self::assertSame($expected, (string) $b);
  113. }
  114. public function testSerializesFiles(): void
  115. {
  116. $f1 = Psr7\FnStream::decorate(Psr7\Utils::streamFor('foo'), [
  117. 'getMetadata' => static function (): string {
  118. return '/foo/bar.txt';
  119. },
  120. ]);
  121. $f2 = Psr7\FnStream::decorate(Psr7\Utils::streamFor('baz'), [
  122. 'getMetadata' => static function (): string {
  123. return '/foo/baz.jpg';
  124. },
  125. ]);
  126. $f3 = Psr7\FnStream::decorate(Psr7\Utils::streamFor('bar'), [
  127. 'getMetadata' => static function (): string {
  128. return '/foo/bar.unknown';
  129. },
  130. ]);
  131. $b = new MultipartStream([
  132. [
  133. 'name' => 'foo',
  134. 'contents' => $f1,
  135. ],
  136. [
  137. 'name' => 'qux',
  138. 'contents' => $f2,
  139. ],
  140. [
  141. 'name' => 'qux',
  142. 'contents' => $f3,
  143. ],
  144. ], 'boundary');
  145. $expected = \implode('', [
  146. "--boundary\r\n",
  147. "Content-Disposition: form-data; name=\"foo\"; filename=\"bar.txt\"\r\n",
  148. "Content-Length: 3\r\n",
  149. "Content-Type: text/plain\r\n",
  150. "\r\n",
  151. "foo\r\n",
  152. "--boundary\r\n",
  153. "Content-Disposition: form-data; name=\"qux\"; filename=\"baz.jpg\"\r\n",
  154. "Content-Length: 3\r\n",
  155. "Content-Type: image/jpeg\r\n",
  156. "\r\n",
  157. "baz\r\n",
  158. "--boundary\r\n",
  159. "Content-Disposition: form-data; name=\"qux\"; filename=\"bar.unknown\"\r\n",
  160. "Content-Length: 3\r\n",
  161. "Content-Type: application/octet-stream\r\n",
  162. "\r\n",
  163. "bar\r\n",
  164. "--boundary--\r\n",
  165. ]);
  166. self::assertSame($expected, (string) $b);
  167. }
  168. public function testSerializesFilesWithMixedNewlines(): void
  169. {
  170. $content = "LF\nCRLF\r\nCR\r";
  171. $contentLength = \strlen($content);
  172. $f1 = Psr7\FnStream::decorate(Psr7\Utils::streamFor($content), [
  173. 'getMetadata' => static function (): string {
  174. return '/foo/newlines.txt';
  175. },
  176. ]);
  177. $b = new MultipartStream([
  178. [
  179. 'name' => 'newlines',
  180. 'contents' => $f1,
  181. ],
  182. ], 'boundary');
  183. $expected = \implode('', [
  184. "--boundary\r\n",
  185. "Content-Disposition: form-data; name=\"newlines\"; filename=\"newlines.txt\"\r\n",
  186. "Content-Length: {$contentLength}\r\n",
  187. "Content-Type: text/plain\r\n",
  188. "\r\n",
  189. "{$content}\r\n",
  190. "--boundary--\r\n",
  191. ]);
  192. // Do not perform newline normalization in the assertion! The `$content` must
  193. // be embedded as-is in the payload.
  194. self::assertSame($expected, (string) $b);
  195. }
  196. public function testSerializesFilesWithCustomHeaders(): void
  197. {
  198. $f1 = Psr7\FnStream::decorate(Psr7\Utils::streamFor('foo'), [
  199. 'getMetadata' => static function (): string {
  200. return '/foo/bar.txt';
  201. },
  202. ]);
  203. $b = new MultipartStream([
  204. [
  205. 'name' => 'foo',
  206. 'contents' => $f1,
  207. 'headers' => [
  208. 'x-foo' => 'bar',
  209. 'content-disposition' => 'custom',
  210. ],
  211. ],
  212. ], 'boundary');
  213. $expected = \implode('', [
  214. "--boundary\r\n",
  215. "x-foo: bar\r\n",
  216. "content-disposition: custom\r\n",
  217. "Content-Length: 3\r\n",
  218. "Content-Type: text/plain\r\n",
  219. "\r\n",
  220. "foo\r\n",
  221. "--boundary--\r\n",
  222. ]);
  223. self::assertSame($expected, (string) $b);
  224. }
  225. public function testSerializesFilesWithCustomHeadersAndMultipleValues(): void
  226. {
  227. $f1 = Psr7\FnStream::decorate(Psr7\Utils::streamFor('foo'), [
  228. 'getMetadata' => static function (): string {
  229. return '/foo/bar.txt';
  230. },
  231. ]);
  232. $f2 = Psr7\FnStream::decorate(Psr7\Utils::streamFor('baz'), [
  233. 'getMetadata' => static function (): string {
  234. return '/foo/baz.jpg';
  235. },
  236. ]);
  237. $b = new MultipartStream([
  238. [
  239. 'name' => 'foo',
  240. 'contents' => $f1,
  241. 'headers' => [
  242. 'x-foo' => 'bar',
  243. 'content-disposition' => 'custom',
  244. ],
  245. ],
  246. [
  247. 'name' => 'foo',
  248. 'contents' => $f2,
  249. 'headers' => ['cOntenT-Type' => 'custom'],
  250. ],
  251. ], 'boundary');
  252. $expected = \implode('', [
  253. "--boundary\r\n",
  254. "x-foo: bar\r\n",
  255. "content-disposition: custom\r\n",
  256. "Content-Length: 3\r\n",
  257. "Content-Type: text/plain\r\n",
  258. "\r\n",
  259. "foo\r\n",
  260. "--boundary\r\n",
  261. "cOntenT-Type: custom\r\n",
  262. "Content-Disposition: form-data; name=\"foo\"; filename=\"baz.jpg\"\r\n",
  263. "Content-Length: 3\r\n",
  264. "\r\n",
  265. "baz\r\n",
  266. "--boundary--\r\n",
  267. ]);
  268. self::assertSame($expected, (string) $b);
  269. }
  270. public function testCanCreateWithNoneMetadataStreamField(): void
  271. {
  272. $str = 'dummy text';
  273. $a = Psr7\Utils::streamFor(static function () use ($str): string {
  274. return $str;
  275. });
  276. $b = new Psr7\LimitStream($a, \strlen($str));
  277. $c = new MultipartStream([
  278. [
  279. 'name' => 'foo',
  280. 'contents' => $b,
  281. ],
  282. ], 'boundary');
  283. $expected = \implode('', [
  284. "--boundary\r\n",
  285. "Content-Disposition: form-data; name=\"foo\"\r\n",
  286. "\r\n",
  287. $str."\r\n",
  288. "--boundary--\r\n",
  289. ]);
  290. self::assertSame($expected, (string) $c);
  291. }
  292. }