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

7abee62e57 [BrowserKit] fixed missing post request parameters in file uploads
This commit is contained in:
Fabien Potencier 2020-03-28 11:15:56 +01:00
commit b580dd861d
2 changed files with 41 additions and 1 deletions

View File

@ -75,7 +75,7 @@ class HttpBrowser extends AbstractBrowser
$fields = $request->getParameters();
if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {
$part = new FormDataPart($uploadedFiles);
$part = new FormDataPart(array_merge($fields, $uploadedFiles));
return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];
}

View File

@ -134,6 +134,28 @@ class HttpBrowserTest extends AbstractBrowserTest
]);
}
public function testMultiPartRequestWithAdditionalParameters()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToSendRequestWithFiles($client, ['file1_content', 'baz']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', ['bar' => 'baz'], [
'file1' => $this->getUploadedFile('file1'),
]);
}
public function testMultiPartRequestWithAdditionalParametersOfTheSameName()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToNotSendRequestWithFiles($client, ['baz']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', ['file1' => 'baz'], [
'file1' => $this->getUploadedFile('file1'),
]);
}
private function uploadFile(string $data): string
{
$path = tempnam(sys_get_temp_dir(), 'http');
@ -167,4 +189,22 @@ class HttpBrowserTest extends AbstractBrowserTest
}))
->willReturn($this->createMock(ResponseInterface::class));
}
protected function expectClientToNotSendRequestWithFiles(HttpClientInterface $client, $fileContents)
{
$client
->expects($this->once())
->method('request')
->with('POST', 'http://example.com/', $this->callback(function ($options) use ($fileContents) {
$this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers']));
$this->assertInstanceOf('\Generator', $options['body']);
$body = implode('', iterator_to_array($options['body'], false));
foreach ($fileContents as $content) {
$this->assertStringNotContainsString($content, $body);
}
return true;
}))
->willReturn($this->createMock(ResponseInterface::class));
}
}