PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.7
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.7
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / psr7 / tests / MultipartStreamTest.php

MultipartStreamTest.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.5.7, at vendor/guzzlehttp/psr7/tests/MultipartStreamTest.php

243 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace GuzzleHttp\Tests\Psr7;
3
4 use GuzzleHttp\Psr7;
5 use GuzzleHttp\Psr7\MultipartStream;
6
7 class MultipartStreamTest extends BaseTest
8 {
9 public function testCreatesDefaultBoundary()
10 {
11 $b = new MultipartStream();
12 $this->assertNotEmpty($b->getBoundary());
13 }
14
15 public function testCanProvideBoundary()
16 {
17 $b = new MultipartStream([], 'foo');
18 $this->assertEquals('foo', $b->getBoundary());
19 }
20
21 public function testIsNotWritable()
22 {
23 $b = new MultipartStream();
24 $this->assertFalse($b->isWritable());
25 }
26
27 public function testCanCreateEmptyStream()
28 {
29 $b = new MultipartStream();
30 $boundary = $b->getBoundary();
31 $this->assertSame("--{$boundary}--\r\n", $b->getContents());
32 $this->assertSame(strlen($boundary) + 6, $b->getSize());
33 }
34
35 /**
36 * @expectedException \InvalidArgumentException
37 */
38 public function testValidatesFilesArrayElement()
39 {
40 new MultipartStream([['foo' => 'bar']]);
41 }
42
43 /**
44 * @expectedException \InvalidArgumentException
45 */
46 public function testEnsuresFileHasName()
47 {
48 new MultipartStream([['contents' => 'bar']]);
49 }
50
51 public function testSerializesFields()
52 {
53 $b = new MultipartStream([
54 [
55 'name' => 'foo',
56 'contents' => 'bar'
57 ],
58 [
59 'name' => 'baz',
60 'contents' => 'bam'
61 ]
62 ], 'boundary');
63 $this->assertEquals(
64 "--boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\nContent-Length: 3\r\n\r\n"
65 . "bar\r\n--boundary\r\nContent-Disposition: form-data; name=\"baz\"\r\nContent-Length: 3"
66 . "\r\n\r\nbam\r\n--boundary--\r\n", (string) $b);
67 }
68
69 public function testSerializesNonStringFields()
70 {
71 $b = new MultipartStream([
72 [
73 'name' => 'int',
74 'contents' => (int) 1
75 ],
76 [
77 'name' => 'bool',
78 'contents' => (boolean) false
79 ],
80 [
81 'name' => 'bool2',
82 'contents' => (boolean) true
83 ],
84 [
85 'name' => 'float',
86 'contents' => (float) 1.1
87 ]
88 ], 'boundary');
89 $this->assertEquals(
90 "--boundary\r\nContent-Disposition: form-data; name=\"int\"\r\nContent-Length: 1\r\n\r\n"
91 . "1\r\n--boundary\r\nContent-Disposition: form-data; name=\"bool\"\r\n\r\n\r\n--boundary"
92 . "\r\nContent-Disposition: form-data; name=\"bool2\"\r\nContent-Length: 1\r\n\r\n"
93 . "1\r\n--boundary\r\nContent-Disposition: form-data; name=\"float\"\r\nContent-Length: 3"
94 . "\r\n\r\n1.1\r\n--boundary--\r\n", (string) $b);
95 }
96
97 public function testSerializesFiles()
98 {
99 $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), [
100 'getMetadata' => function () {
101 return '/foo/bar.txt';
102 }
103 ]);
104
105 $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), [
106 'getMetadata' => function () {
107 return '/foo/baz.jpg';
108 }
109 ]);
110
111 $f3 = Psr7\FnStream::decorate(Psr7\stream_for('bar'), [
112 'getMetadata' => function () {
113 return '/foo/bar.gif';
114 }
115 ]);
116
117 $b = new MultipartStream([
118 [
119 'name' => 'foo',
120 'contents' => $f1
121 ],
122 [
123 'name' => 'qux',
124 'contents' => $f2
125 ],
126 [
127 'name' => 'qux',
128 'contents' => $f3
129 ],
130 ], 'boundary');
131
132 $expected = <<<EOT
133 --boundary
134 Content-Disposition: form-data; name="foo"; filename="bar.txt"
135 Content-Length: 3
136 Content-Type: text/plain
137
138 foo
139 --boundary
140 Content-Disposition: form-data; name="qux"; filename="baz.jpg"
141 Content-Length: 3
142 Content-Type: image/jpeg
143
144 baz
145 --boundary
146 Content-Disposition: form-data; name="qux"; filename="bar.gif"
147 Content-Length: 3
148 Content-Type: image/gif
149
150 bar
151 --boundary--
152
153 EOT;
154
155 $this->assertEquals($expected, str_replace("\r", '', $b));
156 }
157
158 public function testSerializesFilesWithCustomHeaders()
159 {
160 $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), [
161 'getMetadata' => function () {
162 return '/foo/bar.txt';
163 }
164 ]);
165
166 $b = new MultipartStream([
167 [
168 'name' => 'foo',
169 'contents' => $f1,
170 'headers' => [
171 'x-foo' => 'bar',
172 'content-disposition' => 'custom'
173 ]
174 ]
175 ], 'boundary');
176
177 $expected = <<<EOT
178 --boundary
179 x-foo: bar
180 content-disposition: custom
181 Content-Length: 3
182 Content-Type: text/plain
183
184 foo
185 --boundary--
186
187 EOT;
188
189 $this->assertEquals($expected, str_replace("\r", '', $b));
190 }
191
192 public function testSerializesFilesWithCustomHeadersAndMultipleValues()
193 {
194 $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), [
195 'getMetadata' => function () {
196 return '/foo/bar.txt';
197 }
198 ]);
199
200 $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), [
201 'getMetadata' => function () {
202 return '/foo/baz.jpg';
203 }
204 ]);
205
206 $b = new MultipartStream([
207 [
208 'name' => 'foo',
209 'contents' => $f1,
210 'headers' => [
211 'x-foo' => 'bar',
212 'content-disposition' => 'custom'
213 ]
214 ],
215 [
216 'name' => 'foo',
217 'contents' => $f2,
218 'headers' => ['cOntenT-Type' => 'custom'],
219 ]
220 ], 'boundary');
221
222 $expected = <<<EOT
223 --boundary
224 x-foo: bar
225 content-disposition: custom
226 Content-Length: 3
227 Content-Type: text/plain
228
229 foo
230 --boundary
231 cOntenT-Type: custom
232 Content-Disposition: form-data; name="foo"; filename="baz.jpg"
233 Content-Length: 3
234
235 baz
236 --boundary--
237
238 EOT;
239
240 $this->assertEquals($expected, str_replace("\r", '', $b));
241 }
242 }
243