8000 bug #59752 [BrowserKit] Fix submitting forms with empty file fields (… · symfonyaml/symfony@e1cf65c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit e1cf65c

Browse files
bug symfony#59752 [BrowserKit] Fix submitting forms with empty file fields (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- [BrowserKit] Fix submitting forms with empty file fields | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#49014 | License | MIT Replaces symfony#53586 and symfony#59621 Commits ------- 0fbfc3e [BrowserKit] Fix submitting forms with empty file fields
2 parents 068105e + 0fbfc3e commit e1cf65c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/Symfony/Component/BrowserKit/HttpBrowser.php

Lines changed: 7 additions & 2 deletions
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;

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)
0