Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
3 | class CRM_Utils_HttpClientTest extends CiviUnitTestCase { | |
4 | ||
5 | const VALID_HTTP_URL = 'http://civicrm.org/INSTALL.mysql.txt'; | |
6 | const VALID_HTTP_REGEX = '/MySQL/'; | |
1e8a2e1b | 7 | const VALID_HTTPS_URL = 'https://civicrm.org/INSTALL.mysql.txt'; |
6a488035 TO |
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/'; | |
11 | ||
12 | /** | |
13 | * @var string path to which we can store temp file | |
14 | */ | |
15 | protected $tmpFile; | |
16 | ||
3b6f287b TO |
17 | /** |
18 | * @var CRM_Utils_HttpClient | |
19 | */ | |
20 | protected $client; | |
21 | ||
6a488035 TO |
22 | public function setUp() { |
23 | parent::setUp(); | |
24 | ||
25 | $this->tmpFile = $this->createTempDir() . '/example.txt'; | |
26 | ||
27 | $result = civicrm_api('Setting', 'create', array( | |
28 | 'version' => 3, | |
29 | 'verifySSL' => TRUE, | |
30 | )); | |
31 | $this->assertAPISuccess($result); | |
3b6f287b | 32 | $this->client = new CRM_Utils_HttpClient(); |
6a488035 TO |
33 | } |
34 | ||
35 | public function tearDown() { | |
36 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'"); | |
37 | CRM_Core_Config::singleton(TRUE); | |
38 | parent::tearDown(); | |
39 | } | |
40 | ||
41 | function testFetchHttp() { | |
3b6f287b | 42 | $result = $this->client->fetch(self::VALID_HTTP_URL, $this->tmpFile); |
6a488035 TO |
43 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); |
44 | $this->assertRegExp(self::VALID_HTTP_REGEX, file_get_contents($this->tmpFile)); | |
45 | } | |
46 | ||
47 | function testFetchHttps_valid() { | |
3b6f287b | 48 | $result = $this->client->fetch(self::VALID_HTTPS_URL, $this->tmpFile); |
6a488035 TO |
49 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); |
50 | $this->assertRegExp(self::VALID_HTTPS_REGEX, file_get_contents($this->tmpFile)); | |
51 | } | |
52 | ||
53 | function testFetchHttps_invalid_verify() { | |
3b6f287b | 54 | $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); |
6a488035 TO |
55 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_DL_ERROR, $result); |
56 | $this->assertEquals('', file_get_contents($this->tmpFile)); | |
57 | } | |
58 | ||
59 | function testFetchHttps_invalid_noVerify() { | |
60 | $result = civicrm_api('Setting', 'create', array( | |
61 | 'version' => 3, | |
62 | 'verifySSL' => FALSE, | |
63 | )); | |
64 | $this->assertAPISuccess($result); | |
65 | ||
3b6f287b | 66 | $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); |
6a488035 TO |
67 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); |
68 | $this->assertRegExp(self::SELF_SIGNED_HTTPS_REGEX, file_get_contents($this->tmpFile)); | |
69 | } | |
70 | ||
71 | function testFetchHttp_badOutFile() { | |
3b6f287b | 72 | $result = $this->client->fetch(self::VALID_HTTP_URL, '/ba/d/path/too/utput'); |
6a488035 TO |
73 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_WRITE_ERROR, $result); |
74 | } | |
75 | ||
afcc9be0 TO |
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); | |
80 | } | |
81 | ||
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); | |
86 | } | |
87 | ||
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); | |
92 | } | |
93 | ||
94 | function testGetHttps_invalid_noVerify() { | |
95 | $result = civicrm_api('Setting', 'create', array( | |
96 | 'version' => 3, | |
97 | 'verifySSL' => FALSE, | |
98 | )); | |
99 | $this->assertAPISuccess($result); | |
100 | ||
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); | |
104 | } | |
105 | ||
6a488035 | 106 | } |