Skip to content

Commit

Permalink
feat(complex-headers): adds support to cater sending complex typed he…
Browse files Browse the repository at this point in the history
…aders in requests (#70)
  • Loading branch information
asadali214 authored Feb 27, 2025
1 parent 175aad6 commit c3eaad6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function appendPath(string $path): void
*/
public function addHeader(string $key, $value): void
{
$this->headers[$key] = $value;
$this->headers[$key] = CoreHelper::serialize($value);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/ApiCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ public function testCollectedHeaderParams()
$this->assertEquals(890.098, $request->getHeaders()['key5']);
}

public function testComplexHeaderParams()
{
$request = (new RequestBuilder(RequestMethod::POST, '/some/path'))
->parameters(
HeaderParam::init('class', new MockClass([
'my string' => 'value',
])),
HeaderParam::init('file', MockHelper::getFileWrapper()),
HeaderParam::init('array', ['my number' => 123]),
HeaderParam::init('false', false),
HeaderParam::init('true', true),
HeaderParam::init('number', 1234),
HeaderParam::init('string', 'value s')
)
->build(MockHelper::getClient());

$this->assertEquals('{"body":{"my string":"value"}}', $request->getHeaders()['class']);
$this->assertEquals('{"my number":123}', $request->getHeaders()['array']);
$this->assertEquals(
'This test file is created to test CoreFileWrapper functionality',
$request->getHeaders()['file']
);
$this->assertEquals('false', $request->getHeaders()['false']);
$this->assertEquals('true', $request->getHeaders()['true']);
$this->assertEquals(1234, $request->getHeaders()['number']);
$this->assertEquals('value s', $request->getHeaders()['string']);
}

public function testCollectedQueryParams()
{
$options = ['key1' => true, 'key2' => 'some string', 'key3' => 23];
Expand Down
2 changes: 1 addition & 1 deletion tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testEndpoint()
->bodyMatcher(NativeBodyMatcher::init(TestParam::object('{"body":{"httpMethod":"POST","queryUrl":' .
'"https:\/\/my\/path\/v2\/api\/path\/poster?&date+array=Fri%2C+01+Oct+2021+00%3A00%3A00+GMT%2CThu' .
'%2C+30+Sep+2021+00%3A00%3A00+GMT&token=someAuthToken&authorization=accessToken","headers":{' .
'"additionalHead1":"headVal1","additionalHead2":"headVal2","header":1234,"token":"someAuthToken",' .
'"additionalHead1":"headVal1","additionalHead2":"headVal2","header":"1234","token":"someAuthToken",' .
'"authorization":"accessToken","Accept":"application\/json"},"parameters":{"form 2":{"key1":' .
'"value 1","key2":"false","key3":2.3}},"parametersEncoded":{' .
'"form 2":"form+2%5Bkey1%5D=value+1&form+2%5Bkey2%5D=false&form+2%5Bkey3%5D=2.3"},' .
Expand Down

0 comments on commit c3eaad6

Please sign in to comment.