Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d49d80

Browse files
Add support for extra_headers option in Upload and Admin API
--------- Co-authored-by: zgould-cloudinary <[email protected]>
1 parent e5d878c commit 9d49d80

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/Api/BaseApiClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ protected static function finalizeEndPoint($endPoint)
318318
protected function callAsync($method, $endPoint, $options)
319319
{
320320
$endPoint = self::finalizeEndPoint($endPoint);
321+
$options['headers'] = ArrayUtils::mergeNonEmpty(
322+
ArrayUtils::get($options, 'headers', []),
323+
ArrayUtils::get($options, 'extra_headers', [])
324+
);
321325
$this->getLogger()->debug("Making async $method request", ['method' => $method, 'endPoint' => $endPoint]);
322326

323327
return $this

tests/Helpers/MockApiClientTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,8 @@ static function ($options, $item) {
9898
}
9999
);
100100
}
101+
public function getLastRequestHeaders()
102+
{
103+
return $this->mockHandler->getLastRequest()->getHeaders();
104+
}
101105
}

tests/Unit/Upload/UploadApiTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,57 @@ public function testUploadFolderDecoupling()
132132

133133
self::assertSubset($options, $lastOptions);
134134
}
135+
136+
/**
137+
* @dataProvider headersDataProvider
138+
*/
139+
public function testHeadersExtraHeaders($input, $expectedOutput)
140+
{
141+
$mockUploadApi = new MockUploadApi();
142+
$mockUploadApi->upload(self::TEST_BASE64_IMAGE, $input);
143+
$mockOutput = $mockUploadApi->getApiClient()->getLastRequestHeaders();
144+
self::assertSubset($expectedOutput, $mockOutput);
145+
}
146+
147+
public function headersDataProvider()
148+
{
149+
return [
150+
[
151+
[
152+
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
153+
'extra_headers' => ['test1' => 'Bearer abc123', 'test2' => 'MyApp/1.0']
154+
],
155+
['Content-Type' => ['application/json'], 'Accept' => ['application/json'],
156+
'test1' => ['Bearer abc123'], 'test2' => ['MyApp/1.0']]
157+
],
158+
[
159+
[
160+
'headers' => ['X-Request-ID' => '12345'],
161+
'extra_headers' => ['Accept-Encoding' => 'gzip']
162+
],
163+
['X-Request-ID' => ['12345'], 'Accept-Encoding' => ['gzip']]
164+
],
165+
[
166+
[
167+
'headers' => ['Content-Language' => 'en-US'],
168+
'extra_headers' => []
169+
],
170+
['Content-Language' => ['en-US']]
171+
],
172+
[
173+
[
174+
'headers' => [],
175+
'extra_headers' => ['X-Debug' => ['true']]
176+
],
177+
['X-Debug' => ['true']]
178+
],
179+
[
180+
[
181+
'headers' => [],
182+
'extra_headers' => []
183+
],
184+
[]
185+
]
186+
];
187+
}
135188
}

0 commit comments

Comments
 (0)