4 * Class CRM_Utils_HttpClientTest
7 class CRM_Utils_HttpClientTest
extends CiviUnitTestCase
{
9 const VALID_HTTP_URL
= 'http://httpclienttest-http.civicrm.org/index.html';
10 const VALID_HTTP_REGEX
= '/This is httpclienttest-http\./';
11 const VALID_HTTPS_URL
= 'https://httpclienttest-https.civicrm.org/index.html';
12 const VALID_HTTPS_REGEX
= '/This is httpclienttest-https\./';
13 const SELF_SIGNED_HTTPS_URL
= 'https://httpclienttest-selfsign.civicrm.org:4433/index.html';
14 const SELF_SIGNED_HTTPS_REGEX
= '/This is httpclienttest-selfsign\./';
18 * Path to which we can store temp file
23 * @var CRM_Utils_HttpClient
27 public function setUp() {
30 $this->tmpFile
= $this->createTempDir() . '/example.txt';
32 $result = civicrm_api('Setting', 'create', [
36 $this->assertAPISuccess($result);
37 $this->client
= new CRM_Utils_HttpClient();
40 public function tearDown() {
41 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'");
42 CRM_Core_Config
::singleton(TRUE);
46 public function testFetchHttp() {
47 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, $this->tmpFile
);
48 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
49 $this->assertRegExp(self
::VALID_HTTP_REGEX
, file_get_contents($this->tmpFile
));
52 public function testFetchHttps_valid() {
53 $result = $this->client
->fetch(self
::VALID_HTTPS_URL
, $this->tmpFile
);
54 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
55 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
58 public function testFetchHttps_invalid_verify() {
59 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
60 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $result);
61 $this->assertEquals('', file_get_contents($this->tmpFile
));
64 public function testFetchHttps_invalid_noVerify() {
65 $result = civicrm_api('Setting', 'create', [
69 $this->assertAPISuccess($result);
71 $result = $this->client
->fetch(self
::SELF_SIGNED_HTTPS_URL
, $this->tmpFile
);
72 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $result);
73 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, file_get_contents($this->tmpFile
));
76 public function testFetchHttp_badOutFile() {
77 $result = $this->client
->fetch(self
::VALID_HTTP_URL
, '/ba/d/path/too/utput');
78 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_WRITE_ERROR
, $result);
81 public function testGetHttp() {
82 list($status, $data) = $this->client
->get(self
::VALID_HTTP_URL
);
83 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
84 $this->assertRegExp(self
::VALID_HTTP_REGEX
, $data);
87 public function testGetHttps_valid() {
88 list($status, $data) = $this->client
->get(self
::VALID_HTTPS_URL
);
89 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
90 $this->assertRegExp(self
::VALID_HTTPS_REGEX
, $data);
93 public function testGetHttps_invalid_verify() {
94 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
95 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_DL_ERROR
, $status);
96 $this->assertEquals('', $data);
99 public function testGetHttps_invalid_noVerify() {
100 $result = civicrm_api('Setting', 'create', [
102 'verifySSL' => FALSE,
104 $this->assertAPISuccess($result);
106 list($status, $data) = $this->client
->get(self
::SELF_SIGNED_HTTPS_URL
);
107 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
108 $this->assertRegExp(self
::SELF_SIGNED_HTTPS_REGEX
, $data);