Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit08e391c

Browse files
[BrowserKit] Fix submitting forms with empty file fields
1 parent818423e commit08e391c

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 =newFormDataPart(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] =newDataPart('','');
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
useSymfony\Component\BrowserKit\CookieJar;
1515
useSymfony\Component\BrowserKit\History;
1616
useSymfony\Component\BrowserKit\HttpBrowser;
17+
useSymfony\Component\HttpClient\MockHttpClient;
18+
useSymfony\Component\HttpClient\Response\MockResponse;
1719
useSymfony\Contracts\HttpClient\HttpClientInterface;
1820
useSymfony\Contracts\HttpClient\ResponseInterface;
1921

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

213+
publicfunctiontestEmptyUpload()
214+
{
215+
$client =newMockHttpClient(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+
returnnewMockResponse();
238+
});
239+
240+
$browser =newHttpBrowser($client);
241+
$browser->request('POST','/', [], ['file' => ['tmp_name' =>'','name' =>'file']]);
242+
}
243+
211244
privatefunctionuploadFile(string$data):string
212245
{
213246
$path =tempnam(sys_get_temp_dir(),'http');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp