From 7abee62e573f2009fbdf4f41c04953343ba2b242 Mon Sep 17 00:00:00 2001 From: Mark Spink Date: Mon, 23 Mar 2020 14:34:58 +0000 Subject: [PATCH] [BrowserKit] fixed missing post request parameters in file uploads --- .../Component/BrowserKit/HttpBrowser.php | 2 +- .../BrowserKit/Tests/HttpBrowserTest.php | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/BrowserKit/HttpBrowser.php b/src/Symfony/Component/BrowserKit/HttpBrowser.php index a1e6dd9af011..9c2b3fcf57e5 100644 --- a/src/Symfony/Component/BrowserKit/HttpBrowser.php +++ b/src/Symfony/Component/BrowserKit/HttpBrowser.php @@ -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()]; } diff --git a/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php b/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php index fa3d531aa986..77586f44e774 100644 --- a/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php @@ -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'); @@ -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)); + } }