ConfigTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Qiniu\Tests {
  3. use PHPUnit\Framework\TestCase;
  4. use Qiniu\Config;
  5. class ConfigTest extends TestCase
  6. {
  7. protected $accessKey;
  8. protected $bucketName;
  9. /**
  10. * @before
  11. */
  12. protected function setUpAkAndBucket()
  13. {
  14. global $accessKey;
  15. $this->accessKey = $accessKey;
  16. global $bucketName;
  17. $this->bucketName = $bucketName;
  18. }
  19. public function testGetApiHost()
  20. {
  21. $conf = new Config();
  22. $hasException = false;
  23. $apiHost = '';
  24. try {
  25. $apiHost = $conf->getApiHost($this->accessKey, $this->bucketName);
  26. } catch (\Exception $e) {
  27. $hasException = true;
  28. }
  29. $this->assertFalse($hasException);
  30. }
  31. public function testGetApiHostErrored()
  32. {
  33. $conf = new Config();
  34. $hasException = false;
  35. try {
  36. $conf->getApiHost($this->accessKey, "fakebucket");
  37. } catch (\Exception $e) {
  38. $hasException = true;
  39. }
  40. $this->assertTrue($hasException);
  41. }
  42. public function testGetApiHostV2()
  43. {
  44. $conf = new Config();
  45. list($apiHost, $err) = $conf->getApiHostV2($this->accessKey, $this->bucketName);
  46. $this->assertNull($err);
  47. }
  48. public function testGetApiHostV2Errored()
  49. {
  50. $conf = new Config();
  51. list($apiHost, $err) = $conf->getApiHostV2($this->accessKey, "fakebucket");
  52. $this->assertNotNull($err->code());
  53. $this->assertEquals(631, $err->code());
  54. $this->assertNull($apiHost);
  55. }
  56. public function testSetUcHost()
  57. {
  58. $conf = new Config();
  59. $this->assertEquals("http://uc.qbox.me", $conf->getUcHost());
  60. $conf->setUcHost("uc.example.com");
  61. $this->assertEquals("http://uc.example.com", $conf->getUcHost());
  62. $conf = new Config();
  63. $conf->useHTTPS = true;
  64. $this->assertEquals("https://uc.qbox.me", $conf->getUcHost());
  65. $conf->setUcHost("uc.example.com");
  66. $this->assertEquals("https://uc.example.com", $conf->getUcHost());
  67. }
  68. public function testGetRegionWithCustomDomain()
  69. {
  70. $conf = new Config();
  71. $conf->setQueryRegionHost(
  72. "uc.qbox.me"
  73. );
  74. list(, $err) = $conf->getRsHostV2($this->accessKey, $this->bucketName);
  75. $this->assertNull($err);
  76. }
  77. public function testGetRegionWithBackupDomains()
  78. {
  79. $conf = new Config();
  80. $conf->setQueryRegionHost(
  81. "fake-uc.phpsdk.qiniu.com",
  82. array(
  83. "unavailable-uc.phpsdk.qiniu.com",
  84. "uc.qbox.me" // real uc
  85. )
  86. );
  87. list(, $err) = $conf->getRsHostV2($this->accessKey, $this->bucketName);
  88. $this->assertNull($err);
  89. }
  90. public function testGetRegionWithUcAndBackupDomains()
  91. {
  92. $conf = new Config();
  93. $conf->setUcHost("fake-uc.phpsdk.qiniu.com");
  94. $conf->setBackupQueryRegionHosts(
  95. array(
  96. "unavailable-uc.phpsdk.qiniu.com",
  97. "uc.qbox.me" // real uc
  98. )
  99. );
  100. list(, $err) = $conf->getRsHostV2($this->accessKey, $this->bucketName);
  101. $this->assertNull($err);
  102. }
  103. }
  104. }