Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(multipart-form-param): fixes issue of sending multipart object params #14

Merged
merged 2 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Request/Parameters/FormParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Core\Request\Parameters;

use Core\Types\Sdk\CoreFileWrapper;
use Core\Utils\CoreHelper;
use CoreInterfaces\Core\Request\RequestArraySerialization;
use CoreInterfaces\Core\Request\RequestSetterInterface;

Expand Down Expand Up @@ -57,6 +58,12 @@ public function plain(): self
return $this;
}

private function isMultipart(): bool
{
return isset($this->encodingHeaders['content-type']) &&
$this->encodingHeaders['content-type'] != 'application/x-www-form-urlencoded';
}

/**
* Adds the parameter to the request provided.
*
Expand All @@ -77,6 +84,10 @@ public function apply(RequestSetterInterface $request): void
return;
}
$this->value = $this->prepareValue($this->value);
if ($this->isMultipart()) {
$request->addMultipartFormParam($this->key, CoreHelper::serialize($this->value));
return;
}
$encodedValue = $this->httpBuildQuery([$this->key => $this->value], $this->format);
if (empty($encodedValue)) {
return;
Expand Down
14 changes: 10 additions & 4 deletions tests/ApiCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,17 @@ public function testSendSingleForm()
$this->assertEquals([], $result->body['parametersMultipart']);
}

public function testSendFileForm()
public function testSendMultipartFormParameters()
{
$result = MockHelper::newApiCall()
->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))
->parameters(FormParam::init('myFile', MockHelper::getFileWrapper())))
->responseHandler(MockHelper::responseHandler()
->type(MockClass::class))
->parameters(
FormParam::init('myFile', MockHelper::getFileWrapper())
->encodingHeader('content-type', 'image/png'),
FormParam::init('object', new MockClass(["key" => 234]))
->encodingHeader('content-type', 'application/json')
))
->responseHandler(MockHelper::responseHandler()->type(MockClass::class))
->execute();
$this->assertInstanceOf(MockClass::class, $result);
$this->assertEquals([], $result->body['parametersEncoded']);
Expand All @@ -342,6 +346,8 @@ public function testSendFileForm()
$this->assertEquals('text/plain', $file->getMimeType());
$this->assertEquals('My Text', $file->getPostFilename());
$this->assertEquals($file, $result->body['parameters']['myFile']);
$object = $result->body['parametersMultipart']['object'];
$this->assertEquals('{"body":{"key":234}}', $object);
}

public function testSendFileFormWithEncodingHeader()
Expand Down