Skip to content

Commit

Permalink
chore: fix contentType check in post and put (#811)
Browse files Browse the repository at this point in the history
* chore: fix contentType check in post and put

* chore: fix contentType tests
  • Loading branch information
tiwarishubham635 authored Jun 20, 2024
1 parent 1c6f896 commit e634588
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/Twilio/Http/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ public function options(string $method, string $url,
$options[CURLOPT_POSTFIELDS] = $body;
$options[CURLOPT_HTTPHEADER] = \array_merge($options[CURLOPT_HTTPHEADER], $headers);
}
elseif (array_key_exists('Content-Type', $headers)) {
elseif ($headers['Content-Type'] === 'application/json') {
$options[CURLOPT_POSTFIELDS] = json_encode($data);
}
else {
$options[CURLOPT_POSTFIELDS] = $this->buildQuery($data);
$options[CURLOPT_HTTPHEADER][] = 'Content-Type: application/x-www-form-urlencoded';
}

break;
Expand All @@ -137,12 +136,11 @@ public function options(string $method, string $url,
$options[CURLOPT_POSTFIELDS] = $body;
$options[CURLOPT_HTTPHEADER] = \array_merge($options[CURLOPT_HTTPHEADER], $headers);
}
elseif (array_key_exists('Content-Type', $headers)) {
elseif ($headers['Content-Type'] === 'application/json') {
$options[CURLOPT_POSTFIELDS] = json_encode($data);
}
else {
$options[CURLOPT_POSTFIELDS] = $this->buildQuery($data);
$options[CURLOPT_HTTPHEADER][] = 'Content-Type: application/x-www-form-urlencoded';
}
break;
case 'head':
Expand Down
11 changes: 7 additions & 4 deletions tests/Twilio/Unit/Http/CurlClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Twilio\Http\CurlClient;
use Twilio\Http\File;
use Twilio\Values;
use Twilio\Tests\Unit\UnitTest;

class CurlClientTest extends UnitTest {
Expand Down Expand Up @@ -136,7 +137,8 @@ public function buildQueryProvider(): array {
public function testQueryString(string $method, array $params, string $expected): void {
$client = new CurlClient();

$actual = $client->options($method, 'url', $params);
$headers = Values::of(['Content-Type' => 'application/x-www-form-urlencoded' ]);
$actual = $client->options($method, 'url', $params, [], $headers);

$this->assertEquals($expected, $actual[CURLOPT_URL]);
}
Expand Down Expand Up @@ -176,8 +178,8 @@ public function queryStringProvider(): array {
*/
public function testPostFields($params, $data, string $expectedContentType, string $expectedBody): void {
$client = new CurlClient();

$actual = $client->options('POST', 'url', $params, $data);
$headers = Values::of(['Content-Type' => $expectedContentType ]);
$actual = $client->options('POST', 'url', $params, $data, $headers);
foreach ($actual[CURLOPT_HTTPHEADER] as $header) {
if (strpos($header, 'Content-Type: ') === 0) {
$this->assertStringMatchesFormat($expectedContentType, substr($header, 14));
Expand Down Expand Up @@ -258,7 +260,8 @@ public function postFieldsProvider(): array {
*/
public function testPutFile($params, $data, string $expectedContentType, string $expectedBody): void {
$client = new CurlClient();
$actual = $client->options('PUT', 'url', $params, $data);
$headers = Values::of(['Content-Type' => $expectedContentType ]);
$actual = $client->options('PUT', 'url', $params, $data, $headers);

foreach ($actual[CURLOPT_HTTPHEADER] as $header) {
if (strpos($header, 'Content-Type: ') === 0) {
Expand Down

0 comments on commit e634588

Please sign in to comment.