Skip to content

Commit

Permalink
Merge pull request #332 from saloonphp/fix/remove-array-filter-on-mul…
Browse files Browse the repository at this point in the history
…tipart-builder

Fix | V3 - Remove array filter on multipart builder
  • Loading branch information
Sammyjo20 committed Nov 22, 2023
2 parents b6696ec + 84c082b commit a5769b6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Http/Senders/Factories/GuzzleMultipartBodyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class GuzzleMultipartBodyFactory implements MultipartBodyFactory
public function create(StreamFactoryInterface $streamFactory, array $multipartValues, string $boundary): StreamInterface
{
$elements = array_map(static function (MultipartValue $value) {
return array_filter([
return [
'name' => $value->name,
'filename' => $value->filename,
'contents' => $value->value,
'headers' => $value->headers,
]);
];
}, $multipartValues);

return new MultipartStream($elements, $boundary);
Expand Down
31 changes: 31 additions & 0 deletions tests/Feature/Body/HasMultipartBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GuzzleHttp\Promise\FulfilledPromise;
use Saloon\Tests\Fixtures\Connectors\TestConnector;
use Saloon\Repositories\Body\MultipartBodyRepository;
use Saloon\Tests\Fixtures\Requests\MixedMultipartRequest;
use Saloon\Tests\Fixtures\Requests\HasMultipartBodyRequest;
use Saloon\Tests\Fixtures\Connectors\HasMultipartBodyConnector;

Expand Down Expand Up @@ -88,3 +89,33 @@

expect($asserted)->toBeTrue();
});

test('can send a real multipart request and files are sent', function () {
$connector = new TestConnector;
$request = new MixedMultipartRequest;

$request->body()->add('name', 'Howdy');
$request->body()->add('file', file_get_contents('tests/Fixtures/Howdy.txt'), 'hi.txt');

$response = $connector->send($request);

$data = $response->json();

expect($data)->toHaveKey('name', 'Howdy');
expect($data)->toHaveKey('file_contents', 'Hello World!' . PHP_EOL);
});

test('can send an empty string as the contents', function () {
$connector = new TestConnector;
$request = new MixedMultipartRequest;

$request->body()->add('name', 'Howdy');
$request->body()->add('file', '', 'hi.txt');

$response = $connector->send($request);

$data = $response->json();

expect($data)->toHaveKey('name', 'Howdy');
expect($data)->toHaveKey('file_contents', '');
});
1 change: 1 addition & 0 deletions tests/Fixtures/Howdy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
25 changes: 25 additions & 0 deletions tests/Fixtures/Requests/MixedMultipartRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Saloon\Tests\Fixtures\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Contracts\Body\HasBody;
use Saloon\Traits\Body\HasMultipartBody;

class MixedMultipartRequest extends Request implements HasBody
{
use HasMultipartBody;

protected Method $method = Method::POST;

/**
* Define the endpoint for the request.
*/
public function resolveEndpoint(): string
{
return '/mixed-multipart';
}
}

0 comments on commit a5769b6

Please sign in to comment.