2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Utils_HttpClientTest
extends CiviUnitTestCase
{
5 const VALID_HTTP_URL
= 'http://civicrm.org/INSTALL.mysql.txt';
6 const VALID_HTTP_REGEX
= '/MySQL/';
7 const VALID_HTTPS_URL
= 'https://civicrm.org/INSTALL.mysql.txt';
8 const VALID_HTTPS_REGEX
= '/MySQL/';
9 const SELF_SIGNED_HTTPS_URL
= 'https://self-signed.onebitwise.com:4443/';
10 const SELF_SIGNED_HTTPS_REGEX
= '/self-signed/';
13 * @var string path to which we can store temp file
18 * @var CRM_Utils_HttpClient
22 public function setUp() {
25 $this->tmpFile
= $this->createTempDir() . '/example.txt';
27 $result = civicrm_api('Setting', 'create', array(
31 $this->assertAPISuccess($result);
32 $this->client
= new CRM_Utils_HttpClient();
35 public function tearDown() {
36 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'");
37 CRM_Core_Config
::singleton(TRUE);
41 function testFetchHttp() {
42 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, $this->tmpFile
);
43 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
44 $this->assertRegExp(self
::VALID_HTTP_REGEX
, file_get_contents($this->tmpFile
));
47 function testFetchHttps_valid() {
48 $result = $this->client
->fetch(self
::VALID_HTTPS_URL
, $this->tmpFile
);
49 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
50 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
53 function testFetchHttps_invalid_verify() {
54 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
55 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $result);
56 $this->assertEquals('', file_get_contents($this->tmpFile
));
59 function testFetchHttps_invalid_noVerify() {
60 $result = civicrm_api('Setting', 'create', array(
64 $this->assertAPISuccess($result);
66 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
67 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
68 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
71 function testFetchHttp_badOutFile() {
72 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, '/ba/d/path/too/utput');
73 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_WRITE_ERROR
, $result);
76 function testGetHttp() {
77 list($status, $data) = $this->client
->get(self
::VALID_HTTP_URL
);
78 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
79 $this->assertRegExp(self
::VALID_HTTP_REGEX
, $data);
82 function testGetHttps_valid() {
83 list($status, $data) = $this->client
->get(self
::VALID_HTTPS_URL
);
84 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
85 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, $data);
88 function testGetHttps_invalid_verify() {
89 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
90 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $status);
91 $this->assertEquals('', $data);
94 function testGetHttps_invalid_noVerify() {
95 $result = civicrm_api('Setting', 'create', array(
99 $this->assertAPISuccess($result);
101 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
102 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
103 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, $data);