Skip to content

Commit

Permalink
[BrowserKit] fixed missing post request parameters in file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
codebay authored and fabpot committed Mar 28, 2020
1 parent 3a6f02d commit 7abee62
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 7abee62

Please sign in to comment.