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

Appearance settings

Commit b580dd8

Browse filesBrowse files
committed
bug #36181 [BrowserKit] fixed missing post request parameters in file uploads (codebay)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [BrowserKit] fixed missing post request parameters in file uploads | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Pull Request #35827 "[BrowserKit] Nested file array prevents uploading file" introduced a bug that had not been previously covered by unit tests for the component. Requests that include additional parameters with a file upload are not being included Commits ------- 7abee62 [BrowserKit] fixed missing post request parameters in file uploads
2 parents 3a6f02d + 7abee62 commit b580dd8
Copy full SHA for b580dd8

File tree

Expand file treeCollapse file tree

2 files changed

+41
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-1
lines changed

‎src/Symfony/Component/BrowserKit/HttpBrowser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/BrowserKit/HttpBrowser.php
+1-1Lines changed: 1 addition & 1 deletion
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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php
+40Lines changed: 40 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.