[BrowserKit] fixed missing post request parameters in file uploads
This commit is contained in:
parent
3a6f02d834
commit
7abee62e57
@ -75,7 +75,7 @@ class HttpBrowser extends AbstractBrowser
|
|||||||
$fields = $request->getParameters();
|
$fields = $request->getParameters();
|
||||||
|
|
||||||
if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {
|
if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {
|
||||||
$part = new FormDataPart($uploadedFiles);
|
$part = new FormDataPart(array_merge($fields, $uploadedFiles));
|
||||||
|
|
||||||
return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];
|
return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];
|
||||||
}
|
}
|
||||||
|
@ -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
|
private function uploadFile(string $data): string
|
||||||
{
|
{
|
||||||
$path = tempnam(sys_get_temp_dir(), 'http');
|
$path = tempnam(sys_get_temp_dir(), 'http');
|
||||||
@ -167,4 +189,22 @@ class HttpBrowserTest extends AbstractBrowserTest
|
|||||||
}))
|
}))
|
||||||
->willReturn($this->createMock(ResponseInterface::class));
|
->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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user