ContentTypeTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace OSS\Tests;
  3. require_once __DIR__ . '/Common.php';
  4. class ContentTypeTest extends \PHPUnit_Framework_TestCase
  5. {
  6. private function runCmd($cmd)
  7. {
  8. $output = array();
  9. $status = 0;
  10. exec($cmd . ' 2>/dev/null', $output, $status);
  11. $this->assertEquals(0, $status);
  12. }
  13. private function getContentType($bucket, $object)
  14. {
  15. $client = Common::getOssClient();
  16. $headers = $client->getObjectMeta($bucket, $object);
  17. return $headers['content-type'];
  18. }
  19. public function testByFileName()
  20. {
  21. $client = Common::getOssClient();
  22. $bucket = Common::getBucketName();
  23. $file = '/tmp/x.html';
  24. $object = 'test/x';
  25. $this->runCmd('touch ' . $file);
  26. $client->uploadFile($bucket, $object, $file);
  27. $type = $this->getContentType($bucket, $object);
  28. $this->assertEquals('text/html', $type);
  29. $file = '/tmp/x.json';
  30. $object = 'test/y';
  31. $this->runCmd('dd if=/dev/urandom of=' . $file . ' bs=1024 count=100');
  32. $client->multiuploadFile($bucket, $object, $file, array('partSize' => 100));
  33. $type = $this->getContentType($bucket, $object);
  34. $this->assertEquals('application/json', $type);
  35. }
  36. public function testByObjectKey()
  37. {
  38. $client = Common::getOssClient();
  39. $bucket = Common::getBucketName();
  40. $object = "test/x.txt";
  41. $client->putObject($bucket, $object, "hello world");
  42. $type = $this->getContentType($bucket, $object);
  43. $this->assertEquals('text/plain', $type);
  44. $file = '/tmp/x.html';
  45. $object = 'test/x.txt';
  46. $this->runCmd('touch ' . $file);
  47. $client->uploadFile($bucket, $object, $file);
  48. $type = $this->getContentType($bucket, $object);
  49. $this->assertEquals('text/html', $type);
  50. $file = '/tmp/x.none';
  51. $object = 'test/x.txt';
  52. $this->runCmd('touch ' . $file);
  53. $client->uploadFile($bucket, $object, $file);
  54. $type = $this->getContentType($bucket, $object);
  55. $this->assertEquals('text/plain', $type);
  56. $file = '/tmp/x.mp3';
  57. $object = 'test/y.json';
  58. $this->runCmd('dd if=/dev/urandom of=' . $file . ' bs=1024 count=100');
  59. $client->multiuploadFile($bucket, $object, $file, array('partSize' => 100));
  60. $type = $this->getContentType($bucket, $object);
  61. $this->assertEquals('audio/mpeg', $type);
  62. $file = '/tmp/x.none';
  63. $object = 'test/y.json';
  64. $this->runCmd('dd if=/dev/urandom of=' . $file . ' bs=1024 count=100');
  65. $client->multiuploadFile($bucket, $object, $file, array('partSize' => 100));
  66. $type = $this->getContentType($bucket, $object);
  67. $this->assertEquals('application/json', $type);
  68. }
  69. public function testByUser()
  70. {
  71. $client = Common::getOssClient();
  72. $bucket = Common::getBucketName();
  73. $object = "test/x.txt";
  74. $client->putObject($bucket, $object, "hello world", array(
  75. 'Content-Type' => 'text/html'
  76. ));
  77. $type = $this->getContentType($bucket, $object);
  78. $this->assertEquals('text/html', $type);
  79. $file = '/tmp/x.html';
  80. $object = 'test/x';
  81. $this->runCmd('touch ' . $file);
  82. $client->uploadFile($bucket, $object, $file, array(
  83. 'Content-Type' => 'application/json'
  84. ));
  85. $type = $this->getContentType($bucket, $object);
  86. $this->assertEquals('application/json', $type);
  87. $file = '/tmp/x.json';
  88. $object = 'test/y';
  89. $this->runCmd('dd if=/dev/urandom of=' . $file . ' bs=1024 count=100');
  90. $client->multiuploadFile($bucket, $object, $file, array(
  91. 'partSize' => 100,
  92. 'Content-Type' => 'audio/mpeg'
  93. ));
  94. $type = $this->getContentType($bucket, $object);
  95. $this->assertEquals('audio/mpeg', $type);
  96. }
  97. }