scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Post; |
||
4 | |||
5 | use GuzzleHttp\Message\Request; |
||
6 | use GuzzleHttp\Post\PostBody; |
||
7 | use GuzzleHttp\Post\PostFile; |
||
8 | use GuzzleHttp\Query; |
||
9 | |||
10 | /** |
||
11 | * @covers GuzzleHttp\Post\PostBody |
||
12 | */ |
||
13 | class PostBodyTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | public function testWrapsBasicStreamFunctionality() |
||
16 | { |
||
17 | $b = new PostBody(); |
||
18 | $this->assertTrue($b->isSeekable()); |
||
19 | $this->assertTrue($b->isReadable()); |
||
20 | $this->assertFalse($b->isWritable()); |
||
21 | $this->assertFalse($b->write('foo')); |
||
22 | } |
||
23 | |||
24 | public function testApplyingWithNothingDoesNothing() |
||
25 | { |
||
26 | $b = new PostBody(); |
||
27 | $m = new Request('POST', '/'); |
||
28 | $b->applyRequestHeaders($m); |
||
29 | $this->assertFalse($m->hasHeader('Content-Length')); |
||
30 | $this->assertFalse($m->hasHeader('Content-Type')); |
||
31 | } |
||
32 | |||
33 | public function testCanForceMultipartUploadsWhenApplying() |
||
34 | { |
||
35 | $b = new PostBody(); |
||
36 | $b->forceMultipartUpload(true); |
||
37 | $m = new Request('POST', '/'); |
||
38 | $b->applyRequestHeaders($m); |
||
39 | $this->assertContains( |
||
40 | 'multipart/form-data', |
||
41 | $m->getHeader('Content-Type') |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | public function testApplyingWithFilesAddsMultipartUpload() |
||
46 | { |
||
47 | $b = new PostBody(); |
||
48 | $p = new PostFile('foo', fopen(__FILE__, 'r')); |
||
49 | $b->addFile($p); |
||
50 | $this->assertEquals([$p], $b->getFiles()); |
||
51 | $this->assertNull($b->getFile('missing')); |
||
52 | $this->assertSame($p, $b->getFile('foo')); |
||
53 | $m = new Request('POST', '/'); |
||
54 | $b->applyRequestHeaders($m); |
||
55 | $this->assertContains( |
||
56 | 'multipart/form-data', |
||
57 | $m->getHeader('Content-Type') |
||
58 | ); |
||
59 | $this->assertTrue($m->hasHeader('Content-Length')); |
||
60 | } |
||
61 | |||
62 | public function testApplyingWithFieldsAddsMultipartUpload() |
||
63 | { |
||
64 | $b = new PostBody(); |
||
65 | $b->setField('foo', 'bar'); |
||
66 | $this->assertEquals(['foo' => 'bar'], $b->getFields()); |
||
67 | $m = new Request('POST', '/'); |
||
68 | $b->applyRequestHeaders($m); |
||
69 | $this->assertContains( |
||
70 | 'application/x-www-form', |
||
71 | $m->getHeader('Content-Type') |
||
72 | ); |
||
73 | $this->assertTrue($m->hasHeader('Content-Length')); |
||
74 | } |
||
75 | |||
76 | public function testMultipartWithNestedFields() |
||
77 | { |
||
78 | $b = new PostBody(); |
||
79 | $b->setField('foo', ['bar' => 'baz']); |
||
80 | $b->forceMultipartUpload(true); |
||
81 | $this->assertEquals(['foo' => ['bar' => 'baz']], $b->getFields()); |
||
82 | $m = new Request('POST', '/'); |
||
83 | $b->applyRequestHeaders($m); |
||
84 | $this->assertContains( |
||
85 | 'multipart/form-data', |
||
86 | $m->getHeader('Content-Type') |
||
87 | ); |
||
88 | $this->assertTrue($m->hasHeader('Content-Length')); |
||
89 | $contents = $b->getContents(); |
||
90 | $this->assertContains('name="foo[bar]"', $contents); |
||
91 | $this->assertNotContains('name="foo"', $contents); |
||
92 | } |
||
93 | |||
94 | public function testCountProvidesFieldsAndFiles() |
||
95 | { |
||
96 | $b = new PostBody(); |
||
97 | $b->setField('foo', 'bar'); |
||
98 | $b->addFile(new PostFile('foo', fopen(__FILE__, 'r'))); |
||
99 | $this->assertEquals(2, count($b)); |
||
100 | $b->clearFiles(); |
||
101 | $b->removeField('foo'); |
||
102 | $this->assertEquals(0, count($b)); |
||
103 | $this->assertEquals([], $b->getFiles()); |
||
104 | $this->assertEquals([], $b->getFields()); |
||
105 | } |
||
106 | |||
107 | public function testHasFields() |
||
108 | { |
||
109 | $b = new PostBody(); |
||
110 | $b->setField('foo', 'bar'); |
||
111 | $b->setField('baz', '123'); |
||
112 | $this->assertEquals('bar', $b->getField('foo')); |
||
113 | $this->assertEquals('123', $b->getField('baz')); |
||
114 | $this->assertNull($b->getField('ahh')); |
||
115 | $this->assertTrue($b->hasField('foo')); |
||
116 | $this->assertFalse($b->hasField('test')); |
||
117 | $b->replaceFields(['abc' => '123']); |
||
118 | $this->assertFalse($b->hasField('foo')); |
||
119 | $this->assertTrue($b->hasField('abc')); |
||
120 | } |
||
121 | |||
122 | public function testConvertsFieldsToQueryStyleBody() |
||
123 | { |
||
124 | $b = new PostBody(); |
||
125 | $b->setField('foo', 'bar'); |
||
126 | $b->setField('baz', '123'); |
||
127 | $this->assertEquals('foo=bar&baz=123', $b); |
||
128 | $this->assertEquals(15, $b->getSize()); |
||
129 | $b->seek(0); |
||
130 | $this->assertEquals('foo=bar&baz=123', $b->getContents()); |
||
131 | $b->seek(0); |
||
132 | $this->assertEquals('foo=bar&baz=123', $b->read(1000)); |
||
133 | $this->assertEquals(15, $b->tell()); |
||
134 | $this->assertTrue($b->eof()); |
||
135 | } |
||
136 | |||
137 | public function testCanSpecifyQueryAggregator() |
||
138 | { |
||
139 | $b = new PostBody(); |
||
140 | $b->setField('foo', ['baz', 'bar']); |
||
141 | $this->assertEquals('foo%5B0%5D=baz&foo%5B1%5D=bar', (string) $b); |
||
142 | $b = new PostBody(); |
||
143 | $b->setField('foo', ['baz', 'bar']); |
||
144 | $agg = Query::duplicateAggregator(); |
||
145 | $b->setAggregator($agg); |
||
146 | $this->assertEquals('foo=baz&foo=bar', (string) $b); |
||
147 | } |
||
148 | |||
149 | public function testDetachesAndCloses() |
||
150 | { |
||
151 | $b = new PostBody(); |
||
152 | $b->setField('foo', 'bar'); |
||
153 | $b->detach(); |
||
154 | $this->assertTrue($b->close()); |
||
155 | $this->assertEquals('', $b->read(10)); |
||
156 | } |
||
157 | |||
158 | public function testCreatesMultipartUploadWithMultiFields() |
||
159 | { |
||
160 | $b = new PostBody(); |
||
161 | $b->setField('testing', ['baz', 'bar']); |
||
162 | $b->setField('other', 'hi'); |
||
163 | $b->setField('third', 'there'); |
||
164 | $b->addFile(new PostFile('foo', fopen(__FILE__, 'r'))); |
||
165 | $s = (string) $b; |
||
166 | $this->assertContains(file_get_contents(__FILE__), $s); |
||
167 | $this->assertContains('testing=bar', $s); |
||
168 | $this->assertContains( |
||
169 | 'Content-Disposition: form-data; name="third"', |
||
170 | $s |
||
171 | ); |
||
172 | $this->assertContains( |
||
173 | 'Content-Disposition: form-data; name="other"', |
||
174 | $s |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | public function testMultipartWithBase64Fields() |
||
179 | { |
||
180 | $b = new PostBody(); |
||
181 | $b->setField('foo64', '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc='); |
||
182 | $b->forceMultipartUpload(true); |
||
183 | $this->assertEquals( |
||
184 | ['foo64' => '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc='], |
||
185 | $b->getFields() |
||
186 | ); |
||
187 | $m = new Request('POST', '/'); |
||
188 | $b->applyRequestHeaders($m); |
||
189 | $this->assertContains( |
||
190 | 'multipart/form-data', |
||
191 | $m->getHeader('Content-Type') |
||
192 | ); |
||
193 | $this->assertTrue($m->hasHeader('Content-Length')); |
||
194 | $contents = $b->getContents(); |
||
195 | $this->assertContains('name="foo64"', $contents); |
||
196 | $this->assertContains( |
||
197 | '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc=', |
||
198 | $contents |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | public function testMultipartWithAmpersandInValue() |
||
203 | { |
||
204 | $b = new PostBody(); |
||
205 | $b->setField('a', 'b&c=d'); |
||
206 | $b->forceMultipartUpload(true); |
||
207 | $this->assertEquals(['a' => 'b&c=d'], $b->getFields()); |
||
208 | $m = new Request('POST', '/'); |
||
209 | $b->applyRequestHeaders($m); |
||
210 | $this->assertContains( |
||
211 | 'multipart/form-data', |
||
212 | $m->getHeader('Content-Type') |
||
213 | ); |
||
214 | $this->assertTrue($m->hasHeader('Content-Length')); |
||
215 | $contents = $b->getContents(); |
||
216 | $this->assertContains('name="a"', $contents); |
||
217 | $this->assertContains('b&c=d', $contents); |
||
218 | } |
||
219 | } |