OSSUtilsTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  47. public function testGetContentMD5()
  48. {
  49. $this->assertEquals('CY9rzUYh03PK3k6DJie09g==', OSSUtils::getContentMD5('test', true));
  50. }
  51. public function testEncode()
  52. {
  53. $value = 'test/encode/h%f';
  54. $this->assertEquals($value, OSSUtils::encode($value, null));
  55. $this->assertEquals($value, OSSUtils::encode($value, ''));
  56. $this->assertEquals('test/encode/aCVm', OSSUtils::encode($value, 'Base64'));
  57. $this->assertEquals('test/encode/h%25f', OSSUtils::encode($value, 'UrlEncode'));
  58. }
  59. public function testGetUserAgent()
  60. {
  61. $userAgent = 'Custom UserAgent';
  62. $res = OSSUtils::getUserAgent($userAgent);
  63. $this->assertTrue(false !== strpos($res, $userAgent));
  64. }
  65. public function testGetHost()
  66. {
  67. $host = OSSUtils::getHost(null, null, null, null);
  68. $this->assertEquals('oss-cn-hangzhou.aliyuncs.com', $host);
  69. $host = OSSUtils::getHost('testBucket', 'region', 'endpoint', 'ip');
  70. $this->assertEquals('endpoint/testBucket', $host);
  71. $host = OSSUtils::getHost('testBucket', 'region', 'endpoint', 'cname');
  72. $this->assertEquals('endpoint', $host);
  73. $host = OSSUtils::getHost('testBucket', 'region', 'endpoint', 'test');
  74. $this->assertEquals('testBucket.endpoint', $host);
  75. }
  76. public function testInject()
  77. {
  78. $stream = fopen('data://text/plain;base64,' . base64_encode('test'), 'r+');
  79. $verifyStream = OSSUtils::inject($stream, $res);
  80. $verifyStream->read(4);
  81. $verifyStream->getVerify();
  82. $this->assertEquals('CY9rzUYh03PK3k6DJie09g==', $res['md5']);
  83. $this->assertEquals('18020588380933092773', $res['crc']);
  84. }
  85. public function testGetSignatureV1()
  86. {
  87. $request = new Request();
  88. $request->pathname = '';
  89. $request->method = 'GET';
  90. $request->headers = [
  91. 'x-oss-test' => 'test',
  92. 'content-type' => 'type',
  93. 'content-md5' => 'md5',
  94. ];
  95. $request->query = [
  96. 'testQuery' => 'testQuery',
  97. 'querykey' => 'queryValue',
  98. 'x-oss-process' => 'value',
  99. ];
  100. $sign = OSSUtils::getSignature($request, 'test', 'ak', 'sk', 'v1', null);
  101. $this->assertEquals('OSS ak:q9lSDGVH1VmpjMTGSwUZn3tg3J4=', $sign);
  102. }
  103. public function testGetSignatureV2()
  104. {
  105. $request = new Request();
  106. $request->method = 'GET';
  107. $request->pathname = 'test?aa';
  108. $request->headers = [
  109. 'x-oss-test' => 'test',
  110. 'content-type' => 'type',
  111. 'content-md5' => 'md5',
  112. ];
  113. $request->query = [
  114. 'testQuery' => 'testQuery',
  115. 'querykey' => 'queryValue',
  116. 'x-oss-test' => 'test',
  117. ];
  118. $signature = OSSUtils::getSignature(
  119. $request,
  120. 'test',
  121. 'accessKeyId',
  122. 'sk',
  123. 'v2',
  124. ['querykey']
  125. );
  126. $this->assertEquals('OSS2 AccessKeyId:accessKeyId,AdditionalHeaders:querykey,Signature:NTrErwnblTk2y8h/NJKCcPCr73iRTfcl99PEc1fCgZY=', $signature);
  127. }
  128. public function testDecode()
  129. {
  130. $this->assertEquals('h%f', OSSUtils::decode('aCVm', 'Base64Decode'));
  131. $this->assertEquals('h%f', OSSUtils::decode('h%25f', 'UrlDecode'));
  132. }
  133. }