123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace OSS\Tests;
- use OSS\Core\OssException;
- use OSS\OssClient;
- class OssClientTest extends \PHPUnit_Framework_TestCase
- {
- public function testConstrunct()
- {
- try {
- $ossClient = new OssClient('id', 'key', 'http://oss-cn-hangzhou.aliyuncs.com');
- $this->assertFalse($ossClient->isUseSSL());
- $ossClient->setUseSSL(true);
- $this->assertTrue($ossClient->isUseSSL());
- $this->assertTrue(true);
- $this->assertEquals(3, $ossClient->getMaxRetries());
- $ossClient->setMaxTries(4);
- $this->assertEquals(4, $ossClient->getMaxRetries());
- $ossClient->setTimeout(10);
- $ossClient->setConnectTimeout(20);
- } catch (OssException $e) {
- assertFalse(true);
- }
- }
- public function testConstrunct2()
- {
- try {
- $ossClient = new OssClient('id', "", 'http://oss-cn-hangzhou.aliyuncs.com');
- $this->assertFalse(true);
- } catch (OssException $e) {
- $this->assertEquals("access key secret is empty", $e->getMessage());
- }
- }
- public function testConstrunct3()
- {
- try {
- $ossClient = new OssClient("", 'key', 'http://oss-cn-hangzhou.aliyuncs.com');
- $this->assertFalse(true);
- } catch (OssException $e) {
- $this->assertEquals("access key id is empty", $e->getMessage());
- }
- }
- public function testConstrunct4()
- {
- try {
- $ossClient = new OssClient('id', 'key', "");
- $this->assertFalse(true);
- } catch (OssException $e) {
- $this->assertEquals('endpoint is empty', $e->getMessage());
- }
- }
- public function testConstrunct5()
- {
- try {
- $ossClient = new OssClient('id', 'key', "123.123.123.1");
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- }
- public function testConstrunct6()
- {
- try {
- $ossClient = new OssClient('id', 'key', "https://123.123.123.1");
- $this->assertTrue($ossClient->isUseSSL());
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- }
- public function testConstrunct7()
- {
- try {
- $ossClient = new OssClient('id', 'key', "http://123.123.123.1");
- $this->assertFalse($ossClient->isUseSSL());
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- }
- public function testConstrunct8()
- {
- try {
- $ossClient = new OssClient('id', 'key', "http://123.123.123.1", true);
- $ossClient->listBuckets();
- $this->assertFalse(true);
- } catch (OssException $e) {
- }
- }
- public function testConstrunct9()
- {
- try {
- $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
- $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
- $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
- $ossClient->listBuckets();
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- public function testSupportPutEmptyObject()
- {
- try {
- $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
- $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
- $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
- $bucket = getenv('OSS_BUCKET');
- $ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false);
- $ossClient->putObject($bucket,'test_emptybody','');
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- public function testCreateObjectDir()
- {
- try {
- $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
- $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
- $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
- $bucket = getenv('OSS_BUCKET');
- $object='test-dir';
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
- $ossClient->createObjectDir($bucket,$object);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- public function testGetBucketCors()
- {
- try {
- $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
- $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
- $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
- $bucket = getenv('OSS_BUCKET');
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
- $ossClient->getBucketCors($bucket);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- public function testGetBucketCname()
- {
- try {
- $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
- $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
- $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
- $bucket = getenv('OSS_BUCKET');
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
- $ossClient->getBucketCname($bucket);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- public function testProxySupport()
- {
- $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
- $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
- $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
- $bucket = getenv('OSS_BUCKET') . '-proxy';
- $requestProxy = getenv('OSS_PROXY');
- $key = 'test-proxy-srv-object';
- $content = 'test-content';
- $proxys = parse_url($requestProxy);
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, null, $requestProxy);
- $result = $ossClient->createBucket($bucket);
- $this->checkProxy($result, $proxys);
- $result = $ossClient->putObject($bucket, $key, $content);
- $this->checkProxy($result, $proxys);
- $result = $ossClient->getObject($bucket, $key);
- $this->assertEquals($content, $result);
- // list object
- $objectListInfo = $ossClient->listObjects($bucket);
- $objectList = $objectListInfo->getObjectList();
- $this->assertNotNull($objectList);
- $this->assertTrue(is_array($objectList));
- $objects = array();
- foreach ($objectList as $value) {
- $objects[] = $value->getKey();
- }
- $this->assertEquals(1, count($objects));
- $this->assertTrue(in_array($key, $objects));
- $result = $ossClient->deleteObject($bucket, $key);
- $this->checkProxy($result,$proxys);
- $result = $ossClient->deleteBucket($bucket);
- $this->checkProxy($result, $proxys);
- }
- private function checkProxy($result, $proxys)
- {
- $this->assertEquals($result['info']['primary_ip'], $proxys['host']);
- $this->assertEquals($result['info']['primary_port'], $proxys['port']);
- $this->assertTrue(array_key_exists('via', $result));
- }
- }
|