Skip to content

Commit a5769b6

Browse files
authored
Merge pull request #332 from saloonphp/fix/remove-array-filter-on-multipart-builder
Fix | V3 - Remove array filter on multipart builder
2 parents b6696ec + 84c082b commit a5769b6

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

src/Http/Senders/Factories/GuzzleMultipartBodyFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class GuzzleMultipartBodyFactory implements MultipartBodyFactory
2020
public function create(StreamFactoryInterface $streamFactory, array $multipartValues, string $boundary): StreamInterface
2121
{
2222
$elements = array_map(static function (MultipartValue $value) {
23-
return array_filter([
23+
return [
2424
'name' => $value->name,
2525
'filename' => $value->filename,
2626
'contents' => $value->value,
2727
'headers' => $value->headers,
28-
]);
28+
];
2929
}, $multipartValues);
3030

3131
return new MultipartStream($elements, $boundary);

tests/Feature/Body/HasMultipartBodyTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GuzzleHttp\Promise\FulfilledPromise;
1111
use Saloon\Tests\Fixtures\Connectors\TestConnector;
1212
use Saloon\Repositories\Body\MultipartBodyRepository;
13+
use Saloon\Tests\Fixtures\Requests\MixedMultipartRequest;
1314
use Saloon\Tests\Fixtures\Requests\HasMultipartBodyRequest;
1415
use Saloon\Tests\Fixtures\Connectors\HasMultipartBodyConnector;
1516

@@ -88,3 +89,33 @@
8889

8990
expect($asserted)->toBeTrue();
9091
});
92+
93+
test('can send a real multipart request and files are sent', function () {
94+
$connector = new TestConnector;
95+
$request = new MixedMultipartRequest;
96+
97+
$request->body()->add('name', 'Howdy');
98+
$request->body()->add('file', file_get_contents('tests/Fixtures/Howdy.txt'), 'hi.txt');
99+
100+
$response = $connector->send($request);
101+
102+
$data = $response->json();
103+
104+
expect($data)->toHaveKey('name', 'Howdy');
105+
expect($data)->toHaveKey('file_contents', 'Hello World!' . PHP_EOL);
106+
});
107+
108+
test('can send an empty string as the contents', function () {
109+
$connector = new TestConnector;
110+
$request = new MixedMultipartRequest;
111+
112+
$request->body()->add('name', 'Howdy');
113+
$request->body()->add('file', '', 'hi.txt');
114+
115+
$response = $connector->send($request);
116+
117+
$data = $response->json();
118+
119+
expect($data)->toHaveKey('name', 'Howdy');
120+
expect($data)->toHaveKey('file_contents', '');
121+
});

tests/Fixtures/Howdy.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Saloon\Tests\Fixtures\Requests;
6+
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Contracts\Body\HasBody;
10+
use Saloon\Traits\Body\HasMultipartBody;
11+
12+
class MixedMultipartRequest extends Request implements HasBody
13+
{
14+
use HasMultipartBody;
15+
16+
protected Method $method = Method::POST;
17+
18+
/**
19+
* Define the endpoint for the request.
20+
*/
21+
public function resolveEndpoint(): string
22+
{
23+
return '/mixed-multipart';
24+
}
25+
}

0 commit comments

Comments
 (0)