2 require_once 'CiviTest/CiviUnitTestCase.php';
5 * Class CRM_Utils_HttpClientTest
7 class CRM_Utils_HttpClientTest
extends CiviUnitTestCase
{
9 const VALID_HTTP_URL
= 'http://sandbox.civicrm.org/';
10 const VALID_HTTP_REGEX
= '/<html/';
11 const VALID_HTTPS_URL
= 'https://civicrm.org/INSTALL.mysql.txt';
12 const VALID_HTTPS_REGEX
= '/MySQL/';
13 const SELF_SIGNED_HTTPS_URL
= 'https://self-signed.onebitwise.com:4443/';
14 const SELF_SIGNED_HTTPS_REGEX
= '/self-signed/';
17 * @var string path to which we can store temp file
22 * @var CRM_Utils_HttpClient
26 public function setUp() {
29 $this->tmpFile
= $this->createTempDir() . '/example.txt';
31 $result = civicrm_api('Setting', 'create', array(
35 $this->assertAPISuccess($result);
36 $this->client
= new CRM_Utils_HttpClient();
39 public function tearDown() {
40 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'");
41 CRM_Core_Config
::singleton(TRUE);
45 function testFetchHttp() {
46 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, $this->tmpFile
);
47 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
48 $this->assertRegExp(self
::VALID_HTTP_REGEX
, file_get_contents($this->tmpFile
));
51 function testFetchHttps_valid() {
52 $result = $this->client
->fetch(self
::VALID_HTTPS_URL
, $this->tmpFile
);
53 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
54 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
57 function testFetchHttps_invalid_verify() {
58 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
59 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $result);
60 $this->assertEquals('', file_get_contents($this->tmpFile
));
63 function testFetchHttps_invalid_noVerify() {
64 $result = civicrm_api('Setting', 'create', array(
68 $this->assertAPISuccess($result);
70 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
71 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
72 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
75 function testFetchHttp_badOutFile() {
76 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, '/ba/d/path/too/utput');
77 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_WRITE_ERROR
, $result);
80 function testGetHttp() {
81 list($status, $data) = $this->client
->get(self
::VALID_HTTP_URL
);
82 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
83 $this->assertRegExp(self
::VALID_HTTP_REGEX
, $data);
86 function testGetHttps_valid() {
87 list($status, $data) = $this->client
->get(self
::VALID_HTTPS_URL
);
88 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
89 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, $data);
92 function testGetHttps_invalid_verify() {
93 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
94 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $status);
95 $this->assertEquals('', $data);
98 function testGetHttps_invalid_noVerify() {
99 $result = civicrm_api('Setting', 'create', array(
101 'verifySSL' => FALSE,
103 $this->assertAPISuccess($result);
105 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
106 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
107 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, $data);