OSSUtilsTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace AlibabaCloud\Tea\OSSUtils\Tests;
  3. use AlibabaCloud\Tea\OSSUtils\OSSUtils;
  4. use AlibabaCloud\Tea\Request;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @internal
  8. * @coversNothing
  9. */
  10. class OSSUtilsTest extends TestCase
  11. {
  12. public function testGetErrmessage()
  13. {
  14. $message = "<?xml version='1.0' encoding='UTF-8'?><Error><Code>401</Code></Error>";
  15. $res = OSSUtils::getErrMessage($message);
  16. $this->assertEquals('401', $res['Code']);
  17. }
  18. public function testToMeta()
  19. {
  20. $map = [
  21. 'size' => '1',
  22. 'test.key.id' => '9527',
  23. ];
  24. $res = OSSUtils::toMeta($map, 'test.key.');
  25. $this->assertEquals([
  26. 'test.key.size' => '1',
  27. 'test.key.id' => '9527',
  28. ], $res);
  29. }
  30. public function testParseMeta()
  31. {
  32. $map = [
  33. 'size' => '1',
  34. 'test.key.id' => '9527',
  35. ];
  36. $res = OSSUtils::parseMeta($map, 'test.key.');
  37. $this->assertEquals([
  38. 'size' => '1',
  39. 'id' => '9527',
  40. ], $res);
  41. }
  42. public function testGetContentType()
  43. {
  44. $this->assertEquals('image/webp', OSSUtils::getContentType('test.webp'));
  45. $this->assertEquals('audio/mpeg', OSSUtils::getContentType('test.mp3'));
  46. $this->assertEquals(null, OSSUtils::getContentType(null));
  47. $this->assertEquals(null, OSSUtils::getContentType(true));
  48. }
  49. public function testGetContentMD5()
  50. {
  51. $this->assertEquals('CY9rzUYh03PK3k6DJie09g==', OSSUtils::getContentMD5('test', true));
  52. }
  53. public function testEncode()
  54. {
  55. $value = 'test/encode/h%f';
  56. $this->assertEquals($value, OSSUtils::encode($value, null));
  57. $this->assertEquals($value, OSSUtils::encode($value, ''));
  58. $this->assertEquals('test/encode/aCVm', OSSUtils::encode($value, 'Base64'));
  59. $this->assertEquals('test/encode/h%25f', OSSUtils::encode($value, 'UrlEncode'));
  60. }
  61. public function testGetUserAgent()
  62. {
  63. $userAgent = 'Custom UserAgent';
  64. $res = OSSUtils::getUserAgent($userAgent);
  65. $this->assertTrue(false !== strpos($res, $userAgent));
  66. }
  67. public function testGetHost()
  68. {
  69. $host = OSSUtils::getHost(null, null, null, null);
  70. $this->assertEquals('oss-cn-hangzhou.aliyuncs.com', $host);
  71. $host = OSSUtils::getHost('testBucket', 'region', 'endpoint', 'ip');
  72. $this->assertEquals('endpoint/testBucket', $host);
  73. $host = OSSUtils::getHost('testBucket', 'region', 'endpoint', 'cname');
  74. $this->assertEquals('endpoint', $host);
  75. $host = OSSUtils::getHost('testBucket', 'region', 'endpoint', 'test');
  76. $this->assertEquals('testBucket.endpoint', $host);
  77. }
  78. public function testInject()
  79. {
  80. $stream = fopen('data://text/plain;base64,' . base64_encode('test'), 'r+');
  81. $verifyStream = OSSUtils::inject($stream, $res);
  82. $verifyStream->read(4);
  83. $verifyStream->getVerify();
  84. $this->assertEquals('CY9rzUYh03PK3k6DJie09g==', $res['md5']);
  85. $this->assertEquals('18020588380933092773', $res['crc']);
  86. }
  87. public function testGetSignatureV1()
  88. {
  89. $request = new Request();
  90. $request->pathname = '';
  91. $request->method = 'GET';
  92. $request->headers = [
  93. 'x-oss-test' => 'test',
  94. 'content-type' => 'type',
  95. 'content-md5' => 'md5',
  96. ];
  97. $request->query = [
  98. 'testQuery' => 'testQuery',
  99. 'querykey' => 'queryValue',
  100. 'x-oss-process' => 'value',
  101. ];
  102. $sign = OSSUtils::getSignature($request, 'test', 'ak', 'sk', 'v1', null);
  103. $this->assertEquals('OSS ak:q9lSDGVH1VmpjMTGSwUZn3tg3J4=', $sign);
  104. }
  105. public function testGetSignatureV2()
  106. {
  107. $request = new Request();
  108. $request->method = 'GET';
  109. $request->pathname = 'test?aa';
  110. $request->headers = [
  111. 'x-oss-test' => 'test',
  112. 'content-type' => 'type',
  113. 'content-md5' => 'md5',
  114. ];
  115. $request->query = [
  116. 'testQuery' => 'testQuery',
  117. 'querykey' => 'queryValue',
  118. 'x-oss-test' => 'test',
  119. ];
  120. $signature = OSSUtils::getSignature(
  121. $request,
  122. 'test',
  123. 'accessKeyId',
  124. 'sk',
  125. 'v2',
  126. ['querykey']
  127. );
  128. $this->assertEquals('OSS2 AccessKeyId:accessKeyId,AdditionalHeaders:querykey,Signature:NTrErwnblTk2y8h/NJKCcPCr73iRTfcl99PEc1fCgZY=', $signature);
  129. }
  130. public function testDecode()
  131. {
  132. $this->assertEquals('h%f', OSSUtils::decode('aCVm', 'Base64Decode'));
  133. $this->assertEquals('h%f', OSSUtils::decode('h%25f', 'UrlDecode'));
  134. }
  135. }