Merge pull request #7717 from yashodha/CRM-17908
[civicrm-core.git] / tests / phpunit / CRM / Utils / HttpClientTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_HttpClientTest
5 */
6 class CRM_Utils_HttpClientTest extends CiviUnitTestCase {
7
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/';
14
15 /**
16 * @var string path to which we can store temp file
17 */
18 protected $tmpFile;
19
20 /**
21 * @var CRM_Utils_HttpClient
22 */
23 protected $client;
24
25 public function setUp() {
26 parent::setUp();
27
28 $this->tmpFile = $this->createTempDir() . '/example.txt';
29
30 $result = civicrm_api('Setting', 'create', array(
31 'version' => 3,
32 'verifySSL' => TRUE,
33 ));
34 $this->assertAPISuccess($result);
35 $this->client = new CRM_Utils_HttpClient();
36 }
37
38 public function tearDown() {
39 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'");
40 CRM_Core_Config::singleton(TRUE);
41 parent::tearDown();
42 }
43
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));
48 }
49
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));
54 }
55
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));
60 }
61
62 public function testFetchHttps_invalid_noVerify() {
63 $result = civicrm_api('Setting', 'create', array(
64 'version' => 3,
65 'verifySSL' => FALSE,
66 ));
67 $this->assertAPISuccess($result);
68
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));
72 }
73
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);
77 }
78
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);
83 }
84
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);
89 }
90
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);
95 }
96
97 public function testGetHttps_invalid_noVerify() {
98 $result = civicrm_api('Setting', 'create', array(
99 'version' => 3,
100 'verifySSL' => FALSE,
101 ));
102 $this->assertAPISuccess($result);
103
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);
107 }
108
109 }