phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / tests / phpunit / CRM / Utils / HttpClientTest.php
CommitLineData
6a488035
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
aba1cd8b
EM
3
4/**
5 * Class CRM_Utils_HttpClientTest
6 */
6a488035
TO
7class CRM_Utils_HttpClientTest extends CiviUnitTestCase {
8
ce64851f
AH
9 const VALID_HTTP_URL = 'http://sandbox.civicrm.org/';
10 const VALID_HTTP_REGEX = '/<html/';
1e8a2e1b 11 const VALID_HTTPS_URL = 'https://civicrm.org/INSTALL.mysql.txt';
6a488035
TO
12 const VALID_HTTPS_REGEX = '/MySQL/';
13 const SELF_SIGNED_HTTPS_URL = 'https://self-signed.onebitwise.com:4443/';
14 const SELF_SIGNED_HTTPS_REGEX = '/self-signed/';
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
45 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
51 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
57 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
63 function testFetchHttps_invalid_noVerify() {
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
75 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
afcc9be0
TO
80 function testGetHttp() {
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
86 function testGetHttps_valid() {
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
92 function testGetHttps_invalid_verify() {
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
98 function testGetHttps_invalid_noVerify() {
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}