OssClientPresignTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\Credentials\StaticCredentialsProvider;
  5. use OSS\Http\RequestCore;
  6. use OSS\Http\ResponseCore;
  7. use OSS\OssClient;
  8. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  9. class OssClientPresignTest extends TestOssClientBase
  10. {
  11. protected $stsOssClient;
  12. public function testObjectWithSignV1()
  13. {
  14. $config = array(
  15. 'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V1
  16. );
  17. $this->bucket = Common::getBucketName() . '-' . time();
  18. $this->ossClient = Common::getOssClient($config);
  19. $this->ossClient->createBucket($this->bucket);
  20. Common::waitMetaSync();
  21. $object = "a.file";
  22. $this->ossClient->putObject($this->bucket, $object, "hi oss");
  23. $timeout = 3600;
  24. $options = array(
  25. "response-content-disposition" => "inline"
  26. );
  27. try {
  28. $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
  29. } catch (OssException $e) {
  30. $this->assertFalse(true);
  31. }
  32. $this->assertStringContainsString("response-content-disposition=inline", $signedUrl);
  33. $options = array(
  34. "response-content-disposition" => "attachment",
  35. );
  36. try {
  37. $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
  38. } catch (OssException $e) {
  39. $this->assertFalse(true);
  40. }
  41. $this->assertStringContainsString("response-content-disposition=attachment", $signedUrl);
  42. $httpCore = new RequestCore($signedUrl);
  43. $httpCore->set_body("");
  44. $httpCore->set_method("GET");
  45. $httpCore->connect_timeout = 10;
  46. $httpCore->timeout = 10;
  47. $httpCore->add_header("Content-Type", "");
  48. $httpCore->send_request();
  49. $this->assertEquals(200, $httpCore->response_code);
  50. }
  51. protected function tearDown(): void
  52. {
  53. $this->ossClient->deleteObject($this->bucket, "a.file");
  54. parent::tearDown();
  55. }
  56. protected function setUp(): void
  57. {
  58. }
  59. }