File: PostBodyTest.php

package info (click to toggle)
php-guzzlehttp 5.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,560 kB
  • ctags: 2,519
  • sloc: php: 11,610; makefile: 182; python: 22; sh: 6
file content (242 lines) | stat: -rw-r--r-- 7,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace GuzzleHttp\Tests\Post;

use GuzzleHttp\Message\Request;
use GuzzleHttp\Post\PostBody;
use GuzzleHttp\Post\PostFile;
use GuzzleHttp\Query;

/**
 * @covers GuzzleHttp\Post\PostBody
 */
class PostBodyTest extends \PHPUnit_Framework_TestCase
{
    public function testWrapsBasicStreamFunctionality()
    {
        $b = new PostBody();
        $this->assertTrue($b->isSeekable());
        $this->assertTrue($b->isReadable());
        $this->assertFalse($b->isWritable());
        $this->assertFalse($b->write('foo'));
    }

    public function testApplyingWithNothingDoesNothing()
    {
        $b = new PostBody();
        $m = new Request('POST', '/');
        $b->applyRequestHeaders($m);
        $this->assertFalse($m->hasHeader('Content-Length'));
        $this->assertFalse($m->hasHeader('Content-Type'));
    }

    public function testCanForceMultipartUploadsWhenApplying()
    {
        $b = new PostBody();
        $b->forceMultipartUpload(true);
        $m = new Request('POST', '/');
        $b->applyRequestHeaders($m);
        $this->assertContains(
            'multipart/form-data',
            $m->getHeader('Content-Type')
        );
    }

    public function testApplyingWithFilesAddsMultipartUpload()
    {
        $b = new PostBody();
        $p = new PostFile('foo', fopen(__FILE__, 'r'));
        $b->addFile($p);
        $this->assertEquals([$p], $b->getFiles());
        $this->assertNull($b->getFile('missing'));
        $this->assertSame($p, $b->getFile('foo'));
        $m = new Request('POST', '/');
        $b->applyRequestHeaders($m);
        $this->assertContains(
            'multipart/form-data',
            $m->getHeader('Content-Type')
        );
        $this->assertTrue($m->hasHeader('Content-Length'));
    }

    public function testApplyingWithFieldsAddsMultipartUpload()
    {
        $b = new PostBody();
        $b->setField('foo', 'bar');
        $this->assertEquals(['foo' => 'bar'], $b->getFields());
        $m = new Request('POST', '/');
        $b->applyRequestHeaders($m);
        $this->assertContains(
            'application/x-www-form',
            $m->getHeader('Content-Type')
        );
        $this->assertTrue($m->hasHeader('Content-Length'));
    }

    public function testMultipartWithNestedFields()
    {
        $b = new PostBody();
        $b->setField('foo', ['bar' => 'baz']);
        $b->forceMultipartUpload(true);
        $this->assertEquals(['foo' => ['bar' => 'baz']], $b->getFields());
        $m = new Request('POST', '/');
        $b->applyRequestHeaders($m);
        $this->assertContains(
            'multipart/form-data',
            $m->getHeader('Content-Type')
        );
        $this->assertTrue($m->hasHeader('Content-Length'));
        $contents = $b->getContents();
        $this->assertContains('name="foo[bar]"', $contents);
        $this->assertNotContains('name="foo"', $contents);
    }

    public function testCountProvidesFieldsAndFiles()
    {
        $b = new PostBody();
        $b->setField('foo', 'bar');
        $b->addFile(new PostFile('foo', fopen(__FILE__, 'r')));
        $this->assertEquals(2, count($b));
        $b->clearFiles();
        $b->removeField('foo');
        $this->assertEquals(0, count($b));
        $this->assertEquals([], $b->getFiles());
        $this->assertEquals([], $b->getFields());
    }

    public function testHasFields()
    {
        $b = new PostBody();
        $b->setField('foo', 'bar');
        $b->setField('baz', '123');
        $this->assertEquals('bar', $b->getField('foo'));
        $this->assertEquals('123', $b->getField('baz'));
        $this->assertNull($b->getField('ahh'));
        $this->assertTrue($b->hasField('foo'));
        $this->assertFalse($b->hasField('test'));
        $b->replaceFields(['abc' => '123']);
        $this->assertFalse($b->hasField('foo'));
        $this->assertTrue($b->hasField('abc'));
    }

    public function testConvertsFieldsToQueryStyleBody()
    {
        $b = new PostBody();
        $b->setField('foo', 'bar');
        $b->setField('baz', '123');
        $this->assertEquals('foo=bar&baz=123', $b);
        $this->assertEquals(15, $b->getSize());
        $b->seek(0);
        $this->assertEquals('foo=bar&baz=123', $b->getContents());
        $b->seek(0);
        $this->assertEquals('foo=bar&baz=123', $b->read(1000));
        $this->assertEquals(15, $b->tell());
        $this->assertTrue($b->eof());
    }

    public function testCanSpecifyQueryAggregator()
    {
        $b = new PostBody();
        $b->setField('foo', ['baz', 'bar']);
        $this->assertEquals('foo%5B0%5D=baz&foo%5B1%5D=bar', (string) $b);
        $b = new PostBody();
        $b->setField('foo', ['baz', 'bar']);
        $agg = Query::duplicateAggregator();
        $b->setAggregator($agg);
        $this->assertEquals('foo=baz&foo=bar', (string) $b);
    }

    public function testDetachesAndCloses()
    {
        $b = new PostBody();
        $b->setField('foo', 'bar');
        $b->detach();
        $b->close();
        $this->assertEquals('', $b->read(10));
    }

    public function testDetachesWhenBodyIsPresent()
    {
        $b = new PostBody();
        $b->setField('foo', 'bar');
        $b->getContents();
        $b->detach();
    }

    public function testFlushAndMetadataPlaceholders()
    {
        $b = new PostBody();
        $this->assertEquals([], $b->getMetadata());
        $this->assertNull($b->getMetadata('foo'));
    }

    public function testCreatesMultipartUploadWithMultiFields()
    {
        $b = new PostBody();
        $b->setField('testing', ['baz', 'bar']);
        $b->setField('other', 'hi');
        $b->setField('third', 'there');
        $b->addFile(new PostFile('foo', fopen(__FILE__, 'r')));
        $s = (string) $b;
        $this->assertContains(file_get_contents(__FILE__), $s);
        $this->assertContains('testing=bar', $s);
        $this->assertContains(
            'Content-Disposition: form-data; name="third"',
            $s
        );
        $this->assertContains(
            'Content-Disposition: form-data; name="other"',
            $s
        );
    }

    public function testMultipartWithBase64Fields()
    {
        $b = new PostBody();
        $b->setField('foo64', '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc=');
        $b->forceMultipartUpload(true);
        $this->assertEquals(
            ['foo64' => '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc='],
            $b->getFields()
        );
        $m = new Request('POST', '/');
        $b->applyRequestHeaders($m);
        $this->assertContains(
            'multipart/form-data',
            $m->getHeader('Content-Type')
        );
        $this->assertTrue($m->hasHeader('Content-Length'));
        $contents = $b->getContents();
        $this->assertContains('name="foo64"', $contents);
        $this->assertContains(
            '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc=',
            $contents
        );
    }

    public function testMultipartWithAmpersandInValue()
    {
        $b = new PostBody();
        $b->setField('a', 'b&c=d');
        $b->forceMultipartUpload(true);
        $this->assertEquals(['a' => 'b&c=d'], $b->getFields());
        $m = new Request('POST', '/');
        $b->applyRequestHeaders($m);
        $this->assertContains(
            'multipart/form-data',
            $m->getHeader('Content-Type')
        );
        $this->assertTrue($m->hasHeader('Content-Length'));
        $contents = $b->getContents();
        $this->assertContains('name="a"', $contents);
        $this->assertContains('b&c=d', $contents);
    }

    /**
     * @expectedException \GuzzleHttp\Stream\Exception\CannotAttachException
     */
    public function testCannotAttach()
    {
        $b = new PostBody();
        $b->attach('foo');
    }
}