Skip to content

Commit 0c806f2

Browse files
[BrowserKit] Fix submitting forms with empty file fields
1 parent 65d4b3f commit 0c806f2

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

HttpBrowser.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,15 @@ private function getUploadedFiles(array $files): array
143143
}
144144
if (!isset($file['tmp_name'])) {
145145
$uploadedFiles[$name] = $this->getUploadedFiles($file);
146+
continue;
146147
}
147-
if (isset($file['tmp_name'])) {
148-
$uploadedFiles[$name] = DataPart::fromPath($file['tmp_name'], $file['name']);
148+
149+
if ('' === $file['tmp_name']) {
150+
$uploadedFiles[$name] = new DataPart('', '');
151+
continue;
149152
}
153+
154+
$uploadedFiles[$name] = DataPart::fromPath($file['tmp_name'], $file['name']);
150155
}
151156

152157
return $uploadedFiles;

Tests/HttpBrowserTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\BrowserKit\CookieJar;
1515
use Symfony\Component\BrowserKit\History;
1616
use Symfony\Component\BrowserKit\HttpBrowser;
17+
use Symfony\Component\HttpClient\MockHttpClient;
18+
use Symfony\Component\HttpClient\Response\MockResponse;
1719
use Symfony\Contracts\HttpClient\HttpClientInterface;
1820
use Symfony\Contracts\HttpClient\ResponseInterface;
1921

@@ -208,6 +210,37 @@ public static function forwardSlashesRequestPathProvider()
208210
];
209211
}
210212

213+
public function testEmptyUpload()
214+
{
215+
$client = new MockHttpClient(function ($method, $url, $options) {
216+
$this->assertSame('POST', $method);
217+
$this->assertSame('http://localhost/', $url);
218+
$this->assertStringStartsWith('Content-Type: multipart/form-data; boundary=', $options['normalized_headers']['content-type'][0]);
219+
220+
$body = '';
221+
while ('' !== $data = $options['body'](1024)) {
222+
$body .= $data;
223+
}
224+
225+
$expected = <<<EOTXT
226+
--%s\r
227+
Content-Type: application/octet-stream\r
228+
Content-Transfer-Encoding: 8bit\r
229+
Content-Disposition: form-data; name="file"; filename=""\r
230+
\r
231+
\r
232+
--%s--\r
233+
234+
EOTXT;
235+
$this->assertStringMatchesFormat($expected, $body);
236+
237+
return new MockResponse();
238+
});
239+
240+
$browser = new HttpBrowser($client);
241+
$browser->request('POST', '/', [], ['file' => ['tmp_name' => '', 'name' => 'file']]);
242+
}
243+
211244
private function uploadFile(string $data): string
212245
{
213246
$path = tempnam(sys_get_temp_dir(), 'http');

0 commit comments

Comments
 (0)