| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Tests;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Psr7;
|
|---|
| 5 | use GuzzleHttp\Psr7\MultipartStream;
|
|---|
| 6 |
|
|---|
| 7 | class 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
|
|---|
| 106 | Content-Disposition: form-data; name="foo"; filename="bar.txt"
|
|---|
| 107 | Content-Length: 3
|
|---|
| 108 | Content-Type: text/plain
|
|---|
| 109 |
|
|---|
| 110 | foo
|
|---|
| 111 | --boundary
|
|---|
| 112 | Content-Disposition: form-data; name="qux"; filename="baz.jpg"
|
|---|
| 113 | Content-Length: 3
|
|---|
| 114 | Content-Type: image/jpeg
|
|---|
| 115 |
|
|---|
| 116 | baz
|
|---|
| 117 | --boundary
|
|---|
| 118 | Content-Disposition: form-data; name="qux"; filename="bar.gif"
|
|---|
| 119 | Content-Length: 3
|
|---|
| 120 | Content-Type: image/gif
|
|---|
| 121 |
|
|---|
| 122 | bar
|
|---|
| 123 | --boundary--
|
|---|
| 124 |
|
|---|
| 125 | EOT;
|
|---|
| 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
|
|---|
| 151 | x-foo: bar
|
|---|
| 152 | content-disposition: custom
|
|---|
| 153 | Content-Length: 3
|
|---|
| 154 | Content-Type: text/plain
|
|---|
| 155 |
|
|---|
| 156 | foo
|
|---|
| 157 | --boundary--
|
|---|
| 158 |
|
|---|
| 159 | EOT;
|
|---|
| 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
|
|---|
| 196 | x-foo: bar
|
|---|
| 197 | content-disposition: custom
|
|---|
| 198 | Content-Length: 3
|
|---|
| 199 | Content-Type: text/plain
|
|---|
| 200 |
|
|---|
| 201 | foo
|
|---|
| 202 | --boundary
|
|---|
| 203 | cOntenT-Type: custom
|
|---|
| 204 | Content-Disposition: form-data; name="foo"; filename="baz.jpg"
|
|---|
| 205 | Content-Length: 3
|
|---|
| 206 |
|
|---|
| 207 | baz
|
|---|
| 208 | --boundary--
|
|---|
| 209 |
|
|---|
| 210 | EOT;
|
|---|
| 211 |
|
|---|
| 212 | $this->assertEquals($expected, str_replace("\r", '', $b));
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|