Skip to content

Commit

Permalink
bug #36181 [BrowserKit] fixed missing post request parameters in file…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fabpot committed Mar 28, 2020
2 parents 3a6f02d + 7abee62 commit b580dd8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/BrowserKit/HttpBrowser.php
Expand Up @@ -75,7 +75,7 @@ private function getBodyAndExtraHeaders(Request $request): array
$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()];
}
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php
Expand Up @@ -134,6 +134,28 @@ public function testMultiPartRequestWithInvalidItem()
]);
}

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');
Expand Down Expand Up @@ -167,4 +189,22 @@ protected function expectClientToSendRequestWithFiles(HttpClientInterface $clien
}))
->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));
}
}

0 comments on commit b580dd8

Please sign in to comment.