Skip to content

Commit dd9eb19

Browse files
chore: adding support for put (#809)
* chore: adding support for put * chore: fixing put test
1 parent d59f57a commit dd9eb19

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

src/Twilio/Http/CurlClient.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ public function options(string $method, string $url,
131131

132132
break;
133133
case 'put':
134-
// TODO: PUT doesn't used anywhere and it has strange implementation. Must investigate later
135-
$options[CURLOPT_PUT] = true;
136-
if ($data) {
137-
if ($buffer = \fopen('php://memory', 'w+')) {
138-
$dataString = $this->buildQuery($data);
139-
\fwrite($buffer, $dataString);
140-
\fseek($buffer, 0);
141-
$options[CURLOPT_INFILE] = $buffer;
142-
$options[CURLOPT_INFILESIZE] = \strlen($dataString);
143-
} else {
144-
throw new EnvironmentException('Unable to open a temporary file');
145-
}
134+
$options[CURLOPT_CUSTOMREQUEST] = 'PUT';
135+
if ($this->hasFile($data)) {
136+
[$headers, $body] = $this->buildMultipartOptions($data);
137+
$options[CURLOPT_POSTFIELDS] = $body;
138+
$options[CURLOPT_HTTPHEADER] = \array_merge($options[CURLOPT_HTTPHEADER], $headers);
139+
}
140+
elseif (array_key_exists('Content-Type', $headers)) {
141+
$options[CURLOPT_POSTFIELDS] = json_encode($data);
142+
}
143+
else {
144+
$options[CURLOPT_POSTFIELDS] = $this->buildQuery($data);
145+
$options[CURLOPT_HTTPHEADER][] = 'Content-Type: application/x-www-form-urlencoded';
146146
}
147147
break;
148148
case 'head':

src/Twilio/Http/GuzzleClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function request(string $method, string $url,
3535
$options['query'] = Query::build($params, PHP_QUERY_RFC1738);
3636
}
3737

38-
if ($method === 'POST') {
38+
if ($method === 'POST' || $method === 'PUT') {
3939
if ($this->hasFile($data)) {
4040
$options['multipart'] = $this->buildMultipartParam($data);
4141
} else {

tests/Twilio/Unit/Http/CurlClientTest.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,26 @@ public function postFieldsProvider(): array {
248248
];
249249
}
250250

251-
public function testPutFile(): void {
251+
/**
252+
* @param array|string $params Parameters to put
253+
* @param array|string $data Data to put
254+
* @param string $expectedContentType Excpected Content-Type header
255+
* @param string $expectedBody Expected POSTFIELDS
256+
* @dataProvider postFieldsProvider
257+
* @throws \Twilio\Exceptions\EnvironmentException
258+
*/
259+
public function testPutFile($params, $data, string $expectedContentType, string $expectedBody): void {
252260
$client = new CurlClient();
253-
$actual = $client->options('PUT', 'url', [], ['a' => 1, 'b' => 2]);
254-
$this->assertNotNull($actual[CURLOPT_INFILE]);
255-
$this->assertEquals('a=1&b=2', \fread($actual[CURLOPT_INFILE], $actual[CURLOPT_INFILESIZE]));
256-
$this->assertEquals(7, $actual[CURLOPT_INFILESIZE]);
261+
$actual = $client->options('PUT', 'url', $params, $data);
262+
263+
foreach ($actual[CURLOPT_HTTPHEADER] as $header) {
264+
if (strpos($header, 'Content-Type: ') === 0) {
265+
$this->assertStringMatchesFormat($expectedContentType, substr($header, 14));
266+
break;
267+
}
268+
}
269+
270+
$this->assertStringMatchesFormat($expectedBody, $actual[CURLOPT_POSTFIELDS]);
257271
}
258272

259273
/**

0 commit comments

Comments
 (0)