source: externals/php/vendor/guzzlehttp/psr7/tests/MultipartStreamTest.php@ cf345ee

Last change on this file since cf345ee was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 5.1 KB
Line 
1<?php
2namespace GuzzleHttp\Tests;
3
4use GuzzleHttp\Psr7;
5use GuzzleHttp\Psr7\MultipartStream;
6
7class MultipartStreamTest extends \PHPUnit_Framework_TestCase
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 testSerializesFiles()
70 {
71 $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), [
72 'getMetadata' => function () {
73 return '/foo/bar.txt';
74 }
75 ]);
76
77 $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), [
78 'getMetadata' => function () {
79 return '/foo/baz.jpg';
80 }
81 ]);
82
83 $f3 = Psr7\FnStream::decorate(Psr7\stream_for('bar'), [
84 'getMetadata' => function () {
85 return '/foo/bar.gif';
86 }
87 ]);
88
89 $b = new MultipartStream([
90 [
91 'name' => 'foo',
92 'contents' => $f1
93 ],
94 [
95 'name' => 'qux',
96 'contents' => $f2
97 ],
98 [
99 'name' => 'qux',
100 'contents' => $f3
101 ],
102 ], 'boundary');
103
104 $expected = <<<EOT
105--boundary
106Content-Disposition: form-data; name="foo"; filename="bar.txt"
107Content-Length: 3
108Content-Type: text/plain
109
110foo
111--boundary
112Content-Disposition: form-data; name="qux"; filename="baz.jpg"
113Content-Length: 3
114Content-Type: image/jpeg
115
116baz
117--boundary
118Content-Disposition: form-data; name="qux"; filename="bar.gif"
119Content-Length: 3
120Content-Type: image/gif
121
122bar
123--boundary--
124
125EOT;
126
127 $this->assertEquals($expected, str_replace("\r", '', $b));
128 }
129
130 public function testSerializesFilesWithCustomHeaders()
131 {
132 $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), [
133 'getMetadata' => function () {
134 return '/foo/bar.txt';
135 }
136 ]);
137
138 $b = new MultipartStream([
139 [
140 'name' => 'foo',
141 'contents' => $f1,
142 'headers' => [
143 'x-foo' => 'bar',
144 'content-disposition' => 'custom'
145 ]
146 ]
147 ], 'boundary');
148
149 $expected = <<<EOT
150--boundary
151x-foo: bar
152content-disposition: custom
153Content-Length: 3
154Content-Type: text/plain
155
156foo
157--boundary--
158
159EOT;
160
161 $this->assertEquals($expected, str_replace("\r", '', $b));
162 }
163
164 public function testSerializesFilesWithCustomHeadersAndMultipleValues()
165 {
166 $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), [
167 'getMetadata' => function () {
168 return '/foo/bar.txt';
169 }
170 ]);
171
172 $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), [
173 'getMetadata' => function () {
174 return '/foo/baz.jpg';
175 }
176 ]);
177
178 $b = new MultipartStream([
179 [
180 'name' => 'foo',
181 'contents' => $f1,
182 'headers' => [
183 'x-foo' => 'bar',
184 'content-disposition' => 'custom'
185 ]
186 ],
187 [
188 'name' => 'foo',
189 'contents' => $f2,
190 'headers' => ['cOntenT-Type' => 'custom'],
191 ]
192 ], 'boundary');
193
194 $expected = <<<EOT
195--boundary
196x-foo: bar
197content-disposition: custom
198Content-Length: 3
199Content-Type: text/plain
200
201foo
202--boundary
203cOntenT-Type: custom
204Content-Disposition: form-data; name="foo"; filename="baz.jpg"
205Content-Length: 3
206
207baz
208--boundary--
209
210EOT;
211
212 $this->assertEquals($expected, str_replace("\r", '', $b));
213 }
214}
Note: See TracBrowser for help on using the repository browser.