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

chore: fix contentType check in post and put #811

Merged
merged 2 commits into from
Jun 20, 2024
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
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
Loading