Skip to content

Commit 08e391c

Browse files
[BrowserKit] Fix submitting forms with empty file fields
1 parent 818423e commit 08e391c

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/Symfony/Component/BrowserKit/HttpBrowser.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function getBodyAndExtraHeaders(Request $request, array $headers): array
8585
if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {
8686
$part = new FormDataPart(array_replace_recursive($fields, $uploadedFiles));
8787

88-
return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];
88+
return [$part->bodyToIterable(...), $part->getPreparedHeaders()->toArray()];
8989
}
9090

9191
if (!$fields) {
@@ -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;

src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php

Lines changed: 33 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy