Skip to content

Commit b43a5eb

Browse files
committed
fix curl tests
1 parent b252bae commit b43a5eb

File tree

11 files changed

+118
-39
lines changed

11 files changed

+118
-39
lines changed

src/Parse/HttpClients/ParseCurlHttpClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ public function setCAFile($caFile)
306306
$this->parseCurl->setOption(CURLOPT_CAINFO, $caFile);
307307
}
308308

309+
/**
310+
* Sets multiple curl options
311+
* https://www.php.net/manual/en/function.curl-setopt.php
312+
*
313+
* @param array $options Array of options to set
314+
* @throws ParseException
315+
*/
316+
public function setHttpOptions($options)
317+
{
318+
$this->parseCurl->setOptionsArray($options);
319+
}
320+
309321
/**
310322
* Gets the error code
311323
*

src/Parse/HttpClients/ParseHttpable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public function setTimeout($timeout);
6363
*/
6464
public function setCAFile($caFile);
6565

66+
/**
67+
* Sets http options to pass to the http client
68+
*
69+
* @param string $httpOptions Options to set
70+
*/
71+
public function setHttpOptions($httpOptions);
72+
6673
/**
6774
* Gets the error code
6875
*

src/Parse/HttpClients/ParseStreamHttpClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class ParseStreamHttpClient implements ParseHttpable
7878
*/
7979
private $caFile;
8080

81+
/**
82+
* Options to pass to the stream context.
83+
*
84+
* @var array
85+
*/
86+
private static $httpOptions;
87+
8188
/**
8289
* Optional timeout for this request
8390
*
@@ -195,6 +202,12 @@ public function send($url, $method = 'GET', $data = array())
195202
$this->options['ssl']['cafile'] = $this->caFile;
196203
}
197204

205+
if (isset($this->httpOptions)) {
206+
foreach ($data as $key => $value) {
207+
$this->options[$key] = $value;
208+
}
209+
}
210+
198211
// add additional options for this request
199212
$this->options['http'] = array(
200213
'method' => $method,
@@ -348,6 +361,17 @@ public function setCAFile($caFile)
348361
$this->caFile = $caFile;
349362
}
350363

364+
/**
365+
* Sets http options to pass to the stream context
366+
* https://www.php.net/manual/en/context.php
367+
*
368+
* @param array $httpOptions options to set
369+
*/
370+
public function setHttpOptions($httpOptions)
371+
{
372+
$this->$httpOptions = $httpOptions;
373+
}
374+
351375
/**
352376
* Sets the request timeout
353377
*

src/Parse/ParseClient.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ final class ParseClient
103103
*/
104104
private static $caFile;
105105

106+
/**
107+
* Options to pass to the http client.
108+
*
109+
* @var array
110+
*/
111+
private static $httpOptions;
112+
106113
/**
107114
* Constant for version string to include with requests.
108115
*
@@ -230,6 +237,7 @@ public static function getServerHealth()
230237

231238
// try to get a response from the server
232239
$url = self::createRequestUrl('health');
240+
233241
$response = $httpClient->send($url);
234242

235243
$errorCode = $httpClient->getErrorCode();
@@ -301,6 +309,21 @@ public static function setCAFile($caFile)
301309
self::$caFile = $caFile;
302310
}
303311

312+
/**
313+
* Sets http options to pass to the http client
314+
* For curl
315+
* https://www.php.net/manual/en/function.curl-setopt.php
316+
*
317+
* For stream context
318+
* https://www.php.net/manual/en/context.php
319+
*
320+
* @param array $httpOptions options to set
321+
*/
322+
public static function setHttpOptions($httpOptions)
323+
{
324+
self::$httpOptions = $httpOptions;
325+
}
326+
304327
/**
305328
* ParseClient::_encode, internal method for encoding object values.
306329
*
@@ -452,6 +475,9 @@ private static function getPreparedHttpClient()
452475
// set CA file
453476
$httpClient->setCAFile(self::$caFile);
454477
}
478+
if (isset(self::$httpOptions)) {
479+
$httpClient->setHttpOptions(self::$httpOptions);
480+
}
455481

456482
return $httpClient;
457483
}

tests/Parse/Helper.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public static function setHttpClient()
6363
// default client set
6464
if (function_exists('curl_init')) {
6565
// cURL client
66-
Helper::print("diamon lewis");
67-
6866
ParseClient::setHttpClient(new ParseCurlHttpClient());
6967
} else {
7068
// stream client
@@ -110,7 +108,7 @@ public static function print($text)
110108

111109
public static function printArray($array)
112110
{
113-
var_dump($array);
111+
print_r($array);
114112
ob_end_flush();
115113
}
116114
}

tests/Parse/ParseClientTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
use PHPUnit\Framework\TestCase;
1818

19+
defined('CURLOPT_PINNEDPUBLICKEY') || define('CURLOPT_PINNEDPUBLICKEY', 10230);
20+
1921
class ParseClientTest extends TestCase
2022
{
2123
public static function setUpBeforeClass() : void
@@ -671,19 +673,45 @@ public function testCheckBadServer()
671673
/**
672674
* @group test-http-options
673675
*/
674-
public function testHttpOptions()
676+
public function testCurlHttpOptions()
675677
{
676678
if (function_exists('curl_init')) {
677-
// set a curl client
678679
ParseClient::setHttpClient(new ParseCurlHttpClient());
679-
echo dirname(__DIR__).'/keys/client.crt';
680-
ParseClient::setCAFile(dirname(__DIR__).'/keys/client.crt');
681680
ParseClient::setServerURL('https://localhost:1338', 'parse');
681+
ParseClient::setHttpOptions([
682+
CURLOPT_SSL_VERIFYPEER => false,
683+
CURLOPT_PINNEDPUBLICKEY => 'sha256//Oz+R70/uIv0irdBWc7RNPyCGeZNbN+CBiPLjJxXWigg=',
684+
CURLOPT_SSLCERT => dirname(__DIR__).'/keys/client.crt',
685+
CURLOPT_SSLKEY => dirname(__DIR__).'/keys/client.key',
686+
]);
682687
$health = ParseClient::getServerHealth();
683-
Helper::printArray($health);
688+
684689
$this->assertNotNull($health);
685690
$this->assertEquals($health['status'], 200);
686691
$this->assertEquals($health['response']['status'], 'ok');
692+
Helper::setServerURL();
687693
}
688694
}
695+
696+
/**
697+
* @group test-http-options
698+
*/
699+
public function testStreamHttpOptions()
700+
{
701+
ParseClient::setHttpClient(new ParseStreamHttpClient());
702+
ParseClient::setServerURL('https://localhost:1338', 'parse');
703+
// ParseClient::setHttpOptions([
704+
// CURLOPT_SSL_VERIFYPEER => false,
705+
// CURLOPT_PINNEDPUBLICKEY => 'sha256//Oz+R70/uIv0irdBWc7RNPyCGeZNbN+CBiPLjJxXWigg=',
706+
// CURLOPT_SSLCERT => dirname(__DIR__).'/keys/client.crt',
707+
// CURLOPT_SSLKEY => dirname(__DIR__).'/keys/client.key',
708+
// ]);
709+
$health = ParseClient::getServerHealth();
710+
711+
Helper::printArray($health);
712+
$this->assertNotNull($health);
713+
$this->assertEquals($health['status'], 200);
714+
$this->assertEquals($health['response']['status'], 'ok');
715+
Helper::setServerURL();
716+
}
689717
}

tests/gencerts.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22
# https://gist.github.com/ryankurte/bc0d8cff6e73a6bb1950
3+
# https://curl.se/libcurl/c/CURLOPT_PINNEDPUBLICKEY.html
34
# ./gencerts.sh parseca localhost parsephp keys/
45
# ./gencerts.sh parseca client parsephp keys/
56

tests/keys/client-copy.pem

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/keys/localhost.pubkey.der

550 Bytes
Binary file not shown.

tests/keys/localhost.pubkey.pem

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA2EH42IZ9cCArjIkp0kRT
3+
2AKQFRXBOYRVghseMr8e5bFsMh5UAcxIzUJYVmbvbN9waItDa/OpI1DaYIal59GU
4+
oYYTlyH8c8Op4Qpv9Cb5SdBPIyMo4xZyQ0TOqGKU6IwcZHSFvqHmCK/PxpDuSOnc
5+
QuZFZoFYnWsGLOigHd8QMB0a0QQgOKr+Lb+xJFwWY55VgnzKcgnb9KaoXcq21ABm
6+
e6+0VEYP7sL6WjLHP68JsAiSXVKbCPdmxMp8ZgahJADtntBUvN57tCAaYcJaJGa8
7+
rFdl9pbbtITvGiTDrwPHzgV4Vh5RNdm6AIsW1wdD8zt6dmx9Q2N8aoUTzymIpdMO
8+
BdqqoIYpOpSFZiOe8fwCaJJvNRZgIn5i0BjvA7sh2OtXM+HymcJFcp58zbEkQmEN
9+
tKDmVaSVruslB+8pe5GXKIqifIjmEhgXDODA8+jP5Mo8tGW3LOY92Y2yEE3xRJc5
10+
3dDm8b0lZIzak/+XlRS6xFEDZKsMSy53TTiadB4KHUgdsX6yu51IdAo/LFg51z9b
11+
NRXLL5E3nc3VUb0mQfrS3cODStiIL7CKWpLYbnoxuCtI3gdOV2ZwZ5vMkwh0Wk1I
12+
osm4jzzqSB5933Q52w+pIjyuM+sRUDIvTrDs/X39tpbLJGUNAvcOjm68xGTZgYcr
13+
YSOZqUHq6rgxq2o5ZeiuhisCAwEAAQ==
14+
-----END PUBLIC KEY-----

tests/server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ function onRequest(req) {
9191

9292
// Create TLS enabled server
9393
const server = https.createServer(serverOptions, app);
94-
// const server = http.createServer(app);
9594
server.on('request', onRequest);
9695

9796
// Start Server

0 commit comments

Comments
 (0)