4 * Class CRM_Utils_HttpClientTest
6 class CRM_Utils_HttpClientTest
extends CiviUnitTestCase
{
8 const VALID_HTTP_URL
= 'http://sandbox.civicrm.org/';
9 const VALID_HTTP_REGEX
= '/<html/';
10 const VALID_HTTPS_URL
= 'https://civicrm.org/INSTALL.mysql.txt';
11 const VALID_HTTPS_REGEX
= '/MySQL/';
12 const SELF_SIGNED_HTTPS_URL
= 'https://www-test.civicrm.org:4433/index.html';
13 const SELF_SIGNED_HTTPS_REGEX
= '/self-signed/';
16 * @var string path to which we can store temp file
21 * @var CRM_Utils_HttpClient
25 public function setUp() {
28 $this->tmpFile
= $this->createTempDir() . '/example.txt';
30 $result = civicrm_api('Setting', 'create', array(
34 $this->assertAPISuccess($result);
35 $this->client
= new CRM_Utils_HttpClient();
38 public function tearDown() {
39 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'");
40 CRM_Core_Config
::singleton(TRUE);
44 public function testFetchHttp() {
45 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, $this->tmpFile
);
46 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
47 $this->assertRegExp(self
::VALID_HTTP_REGEX
, file_get_contents($this->tmpFile
));
50 public function testFetchHttps_valid() {
51 $result = $this->client
->fetch(self
::VALID_HTTPS_URL
, $this->tmpFile
);
52 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
53 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
56 public function testFetchHttps_invalid_verify() {
57 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
58 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $result);
59 $this->assertEquals('', file_get_contents($this->tmpFile
));
62 public function testFetchHttps_invalid_noVerify() {
63 $result = civicrm_api('Setting', 'create', array(
67 $this->assertAPISuccess($result);
69 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
70 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
71 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
74 public function testFetchHttp_badOutFile() {
75 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, '/ba/d/path/too/utput');
76 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_WRITE_ERROR
, $result);
79 public function testGetHttp() {
80 list($status, $data) = $this->client
->get(self
::VALID_HTTP_URL
);
81 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
82 $this->assertRegExp(self
::VALID_HTTP_REGEX
, $data);
85 public function testGetHttps_valid() {
86 list($status, $data) = $this->client
->get(self
::VALID_HTTPS_URL
);
87 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
88 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, $data);
91 public function testGetHttps_invalid_verify() {
92 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
93 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $status);
94 $this->assertEquals('', $data);
97 public function testGetHttps_invalid_noVerify() {
98 $result = civicrm_api('Setting', 'create', array(
100 'verifySSL' => FALSE,
102 $this->assertAPISuccess($result);
104 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
105 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
106 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, $data);