Skip to content

Commit e4b0dc1

Browse files
codebayfabpot
authored andcommitted
[BrowserKit] fixed missing post request parameters in file uploads
1 parent 11b0f19 commit e4b0dc1

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

HttpBrowser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function getBodyAndExtraHeaders(Request $request): array
7575
$fields = $request->getParameters();
7676

7777
if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {
78-
$part = new FormDataPart($uploadedFiles);
78+
$part = new FormDataPart(array_merge($fields, $uploadedFiles));
7979

8080
return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];
8181
}

Tests/HttpBrowserTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ public function testMultiPartRequestWithInvalidItem()
134134
]);
135135
}
136136

137+
public function testMultiPartRequestWithAdditionalParameters()
138+
{
139+
$client = $this->createMock(HttpClientInterface::class);
140+
$this->expectClientToSendRequestWithFiles($client, ['file1_content', 'baz']);
141+
142+
$browser = new HttpBrowser($client);
143+
$browser->request('POST', 'http://example.com/', ['bar' => 'baz'], [
144+
'file1' => $this->getUploadedFile('file1'),
145+
]);
146+
}
147+
148+
public function testMultiPartRequestWithAdditionalParametersOfTheSameName()
149+
{
150+
$client = $this->createMock(HttpClientInterface::class);
151+
$this->expectClientToNotSendRequestWithFiles($client, ['baz']);
152+
153+
$browser = new HttpBrowser($client);
154+
$browser->request('POST', 'http://example.com/', ['file1' => 'baz'], [
155+
'file1' => $this->getUploadedFile('file1'),
156+
]);
157+
}
158+
137159
private function uploadFile(string $data): string
138160
{
139161
$path = tempnam(sys_get_temp_dir(), 'http');
@@ -167,4 +189,22 @@ protected function expectClientToSendRequestWithFiles(HttpClientInterface $clien
167189
}))
168190
->willReturn($this->createMock(ResponseInterface::class));
169191
}
192+
193+
protected function expectClientToNotSendRequestWithFiles(HttpClientInterface $client, $fileContents)
194+
{
195+
$client
196+
->expects($this->once())
197+
->method('request')
198+
->with('POST', 'http://example.com/', $this->callback(function ($options) use ($fileContents) {
199+
$this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers']));
200+
$this->assertInstanceOf('\Generator', $options['body']);
201+
$body = implode('', iterator_to_array($options['body'], false));
202+
foreach ($fileContents as $content) {
203+
$this->assertStringNotContainsString($content, $body);
204+
}
205+
206+
return true;
207+
}))
208+
->willReturn($this->createMock(ResponseInterface::class));
209+
}
170210
}

0 commit comments

Comments
 (0)