Skip to content

Commit

Permalink
bug #40828 [BrowserKit] Merge fields and files recursively if they ar…
Browse files Browse the repository at this point in the history
…e multidimensional array (januszmk)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[BrowserKit] Merge fields and files recursively if they are multidimensional array

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

if we have a form that is prefixed, with form fields like: text: `foo[name]`, file: `foo[file]`, request is loosing non-files fields. encountered this issue while submitting `Symfony\Component\DomCrawler\Form` in tests

Commits
-------

66c3415e3b [BrowserKit] Merge fields and files recursively if they are multidimensional array
  • Loading branch information
fabpot committed Jul 25, 2022
2 parents 32ca9a7 + 540c794 commit 2a1ff40
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion HttpBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
}
Expand Down
8 changes: 6 additions & 2 deletions Tests/HttpBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}))
Expand All @@ -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()
Expand Down

0 comments on commit 2a1ff40

Please sign in to comment.