Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
aba1cd8b EM |
2 | |
3 | /** | |
4 | * Class CRM_Utils_HttpClientTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 TO |
7 | class CRM_Utils_HttpClientTest extends CiviUnitTestCase { |
8 | ||
001b60bb TO |
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\./'; | |
6a488035 TO |
15 | |
16 | /** | |
17 | * @var string path to which we can store temp file | |
18 | */ | |
19 | protected $tmpFile; | |
20 | ||
3b6f287b TO |
21 | /** |
22 | * @var CRM_Utils_HttpClient | |
23 | */ | |
24 | protected $client; | |
25 | ||
6a488035 TO |
26 | public function setUp() { |
27 | parent::setUp(); | |
28 | ||
29 | $this->tmpFile = $this->createTempDir() . '/example.txt'; | |
30 | ||
31 | $result = civicrm_api('Setting', 'create', array( | |
32 | 'version' => 3, | |
33 | 'verifySSL' => TRUE, | |
34 | )); | |
35 | $this->assertAPISuccess($result); | |
3b6f287b | 36 | $this->client = new CRM_Utils_HttpClient(); |
6a488035 TO |
37 | } |
38 | ||
39 | public function tearDown() { | |
40 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'"); | |
41 | CRM_Core_Config::singleton(TRUE); | |
42 | parent::tearDown(); | |
43 | } | |
44 | ||
00be9182 | 45 | public function testFetchHttp() { |
3b6f287b | 46 | $result = $this->client->fetch(self::VALID_HTTP_URL, $this->tmpFile); |
6a488035 TO |
47 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); |
48 | $this->assertRegExp(self::VALID_HTTP_REGEX, file_get_contents($this->tmpFile)); | |
49 | } | |
50 | ||
00be9182 | 51 | public function testFetchHttps_valid() { |
3b6f287b | 52 | $result = $this->client->fetch(self::VALID_HTTPS_URL, $this->tmpFile); |
6a488035 TO |
53 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); |
54 | $this->assertRegExp(self::VALID_HTTPS_REGEX, file_get_contents($this->tmpFile)); | |
55 | } | |
56 | ||
00be9182 | 57 | public function testFetchHttps_invalid_verify() { |
3b6f287b | 58 | $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); |
6a488035 TO |
59 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_DL_ERROR, $result); |
60 | $this->assertEquals('', file_get_contents($this->tmpFile)); | |
61 | } | |
62 | ||
00be9182 | 63 | public function testFetchHttps_invalid_noVerify() { |
6a488035 TO |
64 | $result = civicrm_api('Setting', 'create', array( |
65 | 'version' => 3, | |
66 | 'verifySSL' => FALSE, | |
67 | )); | |
68 | $this->assertAPISuccess($result); | |
69 | ||
3b6f287b | 70 | $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); |
6a488035 TO |
71 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); |
72 | $this->assertRegExp(self::SELF_SIGNED_HTTPS_REGEX, file_get_contents($this->tmpFile)); | |
73 | } | |
74 | ||
00be9182 | 75 | public function testFetchHttp_badOutFile() { |
3b6f287b | 76 | $result = $this->client->fetch(self::VALID_HTTP_URL, '/ba/d/path/too/utput'); |
6a488035 TO |
77 | $this->assertEquals(CRM_Utils_HttpClient::STATUS_WRITE_ERROR, $result); |
78 | } | |
79 | ||
00be9182 | 80 | public function testGetHttp() { |
afcc9be0 TO |
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); | |
84 | } | |
85 | ||
00be9182 | 86 | public function testGetHttps_valid() { |
afcc9be0 TO |
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); | |
90 | } | |
91 | ||
00be9182 | 92 | public function testGetHttps_invalid_verify() { |
afcc9be0 TO |
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); | |
96 | } | |
97 | ||
00be9182 | 98 | public function testGetHttps_invalid_noVerify() { |
afcc9be0 TO |
99 | $result = civicrm_api('Setting', 'create', array( |
100 | 'version' => 3, | |
101 | 'verifySSL' => FALSE, | |
102 | )); | |
103 | $this->assertAPISuccess($result); | |
104 | ||
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); | |
108 | } | |
109 | ||
6a488035 | 110 | } |