From dd618956ffb17022f9a94372603cb75669fb9a83 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 11 Jul 2022 14:36:49 +0200 Subject: [PATCH 1/3] fix sending request to paths containing multiple slashes --- Client.php | 2 +- Tests/HttpBrowserTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Client.php b/Client.php index 06b55cd..3f17725 100644 --- a/Client.php +++ b/Client.php @@ -676,7 +676,7 @@ protected function getAbsoluteUri($uri) } // protocol relative URL - if (str_starts_with($uri, '//')) { + if ('' !== trim($uri, '/') && str_starts_with($uri, '//')) { return parse_url($currentUri, \PHP_URL_SCHEME).':'.$uri; } diff --git a/Tests/HttpBrowserTest.php b/Tests/HttpBrowserTest.php index 8125d1a..879e88f 100644 --- a/Tests/HttpBrowserTest.php +++ b/Tests/HttpBrowserTest.php @@ -172,6 +172,30 @@ public function testMultiPartRequestWithAdditionalParametersOfTheSameName() ]); } + /** + * @dataProvider forwardSlashesRequestPathProvider + */ + public function testMultipleForwardSlashesRequestPath(string $requestPath) + { + $client = $this->createMock(HttpClientInterface::class); + $client + ->expects($this->once()) + ->method('request') + ->with('GET', 'http://localhost'.$requestPath) + ->willReturn($this->createMock(ResponseInterface::class)); + $browser = new HttpBrowser($client); + $browser->request('GET', $requestPath); + } + + public function forwardSlashesRequestPathProvider() + { + return [ + 'one slash' => ['/'], + 'two slashes' => ['//'], + 'multiple slashes' => ['////'], + ]; + } + private function uploadFile(string $data): string { $path = tempnam(sys_get_temp_dir(), 'http'); From 32ca9a71284e8962e3d16a2bac7bb7a303ee2f95 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 20 Jul 2022 11:29:12 +0200 Subject: [PATCH 2/3] Fix CS --- Client.php | 2 +- Tests/ClassThatInheritClient.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Client.php b/Client.php index 3f17725..e299aa2 100644 --- a/Client.php +++ b/Client.php @@ -312,7 +312,7 @@ public function clickLink(string $linkText): Crawler * * @return Crawler */ - public function submit(Form $form, array $values = []/*, array $serverParameters = []*/) + public function submit(Form $form, array $values = []/* , array $serverParameters = [] */) { if (\func_num_args() < 3 && __CLASS__ !== static::class && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) { @trigger_error(sprintf('The "%s()" method will have a new "array $serverParameters = []" argument in version 5.0, not defining it is deprecated since Symfony 4.2.', static::class.'::'.__FUNCTION__), \E_USER_DEPRECATED); diff --git a/Tests/ClassThatInheritClient.php b/Tests/ClassThatInheritClient.php index 9b44547..5572133 100644 --- a/Tests/ClassThatInheritClient.php +++ b/Tests/ClassThatInheritClient.php @@ -40,7 +40,7 @@ protected function doRequest($request): Response /** * @param array $serverParameters */ - public function submit(DomCrawlerForm $form, array $values = []/*, array $serverParameters = []*/): Crawler + public function submit(DomCrawlerForm $form, array $values = []/* , array $serverParameters = [] */): Crawler { return parent::submit($form, $values); } From 540c7943f5665ce7ed674c6b6846a48cd4b40942 Mon Sep 17 00:00:00 2001 From: Janusz Mocek Date: Thu, 15 Apr 2021 19:28:38 +0200 Subject: [PATCH 3/3] [BrowserKit] Merge fields and files recursively if they are multidimensional array --- HttpBrowser.php | 2 +- Tests/HttpBrowserTest.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/HttpBrowser.php b/HttpBrowser.php index 6f5749c..2094e06 100644 --- a/HttpBrowser.php +++ b/HttpBrowser.php @@ -82,7 +82,7 @@ private function getBodyAndExtraHeaders(Request $request, array $headers): array $fields = $request->getParameters(); if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) { - $part = new FormDataPart(array_merge($fields, $uploadedFiles)); + $part = new FormDataPart(array_replace_recursive($fields, $uploadedFiles)); return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()]; } diff --git a/Tests/HttpBrowserTest.php b/Tests/HttpBrowserTest.php index 8125d1a..bac9dfc 100644 --- a/Tests/HttpBrowserTest.php +++ b/Tests/HttpBrowserTest.php @@ -86,7 +86,11 @@ public function testMultiPartRequestWithSingleFile() ->with('POST', 'http://example.com/', $this->callback(function ($options) { $this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers'])); $this->assertInstanceOf(\Generator::class, $options['body']); - $this->assertStringContainsString('my_file', implode('', iterator_to_array($options['body']))); + $values = implode('', iterator_to_array($options['body'], false)); + $this->assertStringContainsString('name="foo[file]"', $values); + $this->assertStringContainsString('my_file', $values); + $this->assertStringContainsString('name="foo[bar]"', $values); + $this->assertStringContainsString('foo2', $values); return true; })) @@ -95,7 +99,7 @@ public function testMultiPartRequestWithSingleFile() $browser = new HttpBrowser($client); $path = tempnam(sys_get_temp_dir(), 'http'); file_put_contents($path, 'my_file'); - $browser->request('POST', 'http://example.com/', [], ['file' => ['tmp_name' => $path, 'name' => 'foo']]); + $browser->request('POST', 'http://example.com/', ['foo' => ['bar' => 'foo2']], ['foo' => ['file' => ['tmp_name' => $path, 'name' => 'foo']]]); } public function testMultiPartRequestWithNormalFlatArray()