ResumeUpTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace Qiniu\Tests;
  3. use phpDocumentor\Reflection\DocBlock\Tags\Version;
  4. use Qiniu\Region;
  5. use Qiniu\Storage\BucketManager;
  6. use Qiniu\Storage\ResumeUploader;
  7. use Qiniu\Storage\UploadManager;
  8. use Qiniu\Http\Client;
  9. use Qiniu\Config;
  10. use Qiniu\Zone;
  11. class ResumeUpTest extends \PHPUnit_Framework_TestCase
  12. {
  13. private static $keyToDelete = array();
  14. public static function tearDownAfterClass()
  15. {
  16. global $bucketName;
  17. global $testAuth;
  18. $config = new Config();
  19. $bucketManager = new BucketManager($testAuth, $config);
  20. foreach (self::$keyToDelete as $key) {
  21. $bucketManager->delete($bucketName, $key);
  22. }
  23. }
  24. protected $bucketName;
  25. protected $auth;
  26. protected function setUp()
  27. {
  28. global $bucketName;
  29. $this->bucketName = $bucketName;
  30. global $testAuth;
  31. $this->auth = $testAuth;
  32. }
  33. public function test4ML()
  34. {
  35. $key = "resumePutFile4ML_".rand();
  36. $upManager = new UploadManager();
  37. $token = $this->auth->uploadToken($this->bucketName, $key);
  38. $tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
  39. $resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
  40. $this->assertNotFalse($resumeFile);
  41. list($ret, $error) = $upManager->putFile(
  42. $token,
  43. $key,
  44. $tempFile,
  45. null,
  46. 'application/octet-stream',
  47. false,
  48. $resumeFile
  49. );
  50. $this->assertNull($error);
  51. $this->assertNotNull($ret['hash']);
  52. unlink($resumeFile);
  53. $domain = getenv('QINIU_TEST_DOMAIN');
  54. $response = Client::get("http://$domain/$key");
  55. $this->assertEquals(200, $response->statusCode);
  56. $this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
  57. unlink($tempFile);
  58. }
  59. public function test4ML2()
  60. {
  61. $key = 'resumePutFile4ML_'.rand();
  62. $cfg = new Config();
  63. $upManager = new UploadManager($cfg);
  64. $token = $this->auth->uploadToken($this->bucketName, $key);
  65. $tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
  66. $resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
  67. $this->assertNotFalse($resumeFile);
  68. list($ret, $error) = $upManager->putFile(
  69. $token,
  70. $key,
  71. $tempFile,
  72. null,
  73. 'application/octet-stream',
  74. false,
  75. $resumeFile
  76. );
  77. $this->assertNull($error);
  78. $this->assertNotNull($ret['hash']);
  79. unlink($resumeFile);
  80. $domain = getenv('QINIU_TEST_DOMAIN');
  81. $response = Client::get("http://$domain/$key");
  82. $this->assertEquals(200, $response->statusCode);
  83. $this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
  84. unlink($tempFile);
  85. }
  86. // public function test8M()
  87. // {
  88. // $key = 'resumePutFile8M';
  89. // $upManager = new UploadManager();
  90. // $token = $this->auth->uploadToken($this->bucketName, $key);
  91. // $tempFile = qiniuTempFile(8*1024*1024+10);
  92. // list($ret, $error) = $upManager->putFile($token, $key, $tempFile);
  93. // $this->assertNull($error);
  94. // $this->assertNotNull($ret['hash']);
  95. // unlink($tempFile);
  96. // }
  97. public function testFileWithFileType()
  98. {
  99. $config = new Config();
  100. $bucketManager = new BucketManager($this->auth, $config);
  101. $testCases = array(
  102. array(
  103. "fileType" => 1,
  104. "name" => "IA"
  105. ),
  106. array(
  107. "fileType" => 2,
  108. "name" => "Archive"
  109. ),
  110. array(
  111. "fileType" => 3,
  112. "name" => "DeepArchive"
  113. )
  114. );
  115. foreach ($testCases as $testCase) {
  116. $key = 'FileType'.$testCase["name"].rand();
  117. $police = array(
  118. "fileType" => $testCase["fileType"],
  119. );
  120. $token = $this->auth->uploadToken($this->bucketName, $key, 3600, $police);
  121. $upManager = new UploadManager();
  122. list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain');
  123. $this->assertNull($error);
  124. $this->assertNotNull($ret);
  125. array_push(self::$keyToDelete, $key);
  126. list($ret, $err) = $bucketManager->stat($this->bucketName, $key);
  127. $this->assertNull($err);
  128. $this->assertEquals($testCase["fileType"], $ret["type"]);
  129. }
  130. }
  131. public function testResumeUploadWithParams()
  132. {
  133. $key = "resumePutFile4ML_".rand();
  134. $upManager = new UploadManager();
  135. $policy = array('returnBody' => '{"hash":$(etag),"fname":$(fname),"var_1":$(x:var_1),"var_2":$(x:var_2)}');
  136. $token = $this->auth->uploadToken($this->bucketName, $key, 3600, $policy);
  137. $tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
  138. $resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
  139. $this->assertNotFalse($resumeFile);
  140. list($ret, $error) = $upManager->putFile(
  141. $token,
  142. $key,
  143. $tempFile,
  144. ["x:var_1" => "val_1", "x:var_2" => "val_2", "x-qn-meta-m1" => "val_1", "x-qn-meta-m2" => "val_2"],
  145. 'application/octet-stream',
  146. false,
  147. $resumeFile
  148. );
  149. $this->assertNull($error);
  150. $this->assertNotNull($ret['hash']);
  151. $this->assertEquals("val_1", $ret['var_1']);
  152. $this->assertEquals("val_2", $ret['var_2']);
  153. $this->assertEquals(basename($tempFile), $ret['fname']);
  154. unlink($resumeFile);
  155. $domain = getenv('QINIU_TEST_DOMAIN');
  156. $response = Client::get("http://$domain/$key");
  157. $this->assertEquals(200, $response->statusCode);
  158. $this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
  159. $this->assertEquals("val_1", $response->headers()["X-Qn-Meta-M1"]);
  160. $this->assertEquals("val_2", $response->headers()["X-Qn-Meta-M2"]);
  161. unlink($tempFile);
  162. }
  163. public function testResumeUploadV2()
  164. {
  165. $cfg = new Config();
  166. $upManager = new UploadManager($cfg);
  167. $testFileSize = array(
  168. config::BLOCK_SIZE / 2,
  169. config::BLOCK_SIZE,
  170. config::BLOCK_SIZE + 10,
  171. config::BLOCK_SIZE * 2,
  172. config::BLOCK_SIZE * 2.5
  173. );
  174. $partSize = 5 * 1024 * 1024;
  175. foreach ($testFileSize as $item) {
  176. $key = 'resumePutFile4ML_'.rand()."_";
  177. $token = $this->auth->uploadToken($this->bucketName, $key);
  178. $tempFile = qiniuTempFile($item);
  179. $resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
  180. $this->assertNotFalse($resumeFile);
  181. list($ret, $error) = $upManager->putFile(
  182. $token,
  183. $key,
  184. $tempFile,
  185. null,
  186. 'application/octet-stream',
  187. false,
  188. $resumeFile,
  189. 'v2',
  190. $partSize
  191. );
  192. $this->assertNull($error);
  193. $this->assertNotNull($ret['hash']);
  194. unlink($resumeFile);
  195. $domain = getenv('QINIU_TEST_DOMAIN');
  196. $response = Client::get("http://$domain/$key");
  197. $this->assertEquals(200, $response->statusCode);
  198. $this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
  199. unlink($tempFile);
  200. }
  201. }
  202. public function testResumeUploadV2WithParams()
  203. {
  204. $key = "resumePutFile4ML_".rand();
  205. $upManager = new UploadManager();
  206. $policy = array('returnBody' => '{"hash":$(etag),"fname":$(fname),"var_1":$(x:var_1),"var_2":$(x:var_2)}');
  207. $token = $this->auth->uploadToken($this->bucketName, $key, 3600, $policy);
  208. $tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
  209. $resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
  210. $this->assertNotFalse($resumeFile);
  211. list($ret, $error) = $upManager->putFile(
  212. $token,
  213. $key,
  214. $tempFile,
  215. ["x:var_1" => "val_1", "x:var_2" => "val_2", "x-qn-meta-m1" => "val_1", "x-qn-meta-m2" => "val_2"],
  216. 'application/octet-stream',
  217. false,
  218. $resumeFile,
  219. 'v2'
  220. );
  221. $this->assertNull($error);
  222. $this->assertNotNull($ret['hash']);
  223. $this->assertEquals("val_1", $ret['var_1']);
  224. $this->assertEquals("val_2", $ret['var_2']);
  225. $this->assertEquals(basename($tempFile), $ret['fname']);
  226. unlink($resumeFile);
  227. $domain = getenv('QINIU_TEST_DOMAIN');
  228. $response = Client::get("http://$domain/$key");
  229. $this->assertEquals(200, $response->statusCode);
  230. $this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
  231. $this->assertEquals("val_1", $response->headers()["X-Qn-Meta-M1"]);
  232. $this->assertEquals("val_2", $response->headers()["X-Qn-Meta-M2"]);
  233. unlink($tempFile);
  234. }
  235. // valid versions are tested above
  236. // Use PHPUnit's Data Provider to test multiple Exception is better,
  237. // but not match the test style of this project
  238. public function testResumeUploadWithInvalidVersion()
  239. {
  240. $cfg = new Config();
  241. $upManager = new UploadManager($cfg);
  242. $testFileSize = config::BLOCK_SIZE * 2;
  243. $partSize = 5 * 1024 * 1024;
  244. $testInvalidVersions = array(
  245. // High probability invalid versions
  246. 'v',
  247. '1',
  248. '2'
  249. );
  250. $expectExceptionCount = 0;
  251. foreach ($testInvalidVersions as $invalidVersion) {
  252. $key = 'resumePutFile4ML_'.rand()."_";
  253. $token = $this->auth->uploadToken($this->bucketName, $key);
  254. $tempFile = qiniuTempFile($testFileSize);
  255. $resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
  256. $this->assertNotFalse($resumeFile);
  257. try {
  258. $upManager->putFile(
  259. $token,
  260. $key,
  261. $tempFile,
  262. null,
  263. 'application/octet-stream',
  264. false,
  265. $resumeFile,
  266. $invalidVersion,
  267. $partSize
  268. );
  269. } catch (\Exception $e) {
  270. $isRightException = false;
  271. $expectExceptionCount++;
  272. while ($e) {
  273. $isRightException = $e instanceof \UnexpectedValueException;
  274. if ($isRightException) {
  275. break;
  276. }
  277. $e = $e->getPrevious();
  278. }
  279. $this->assertTrue($isRightException);
  280. }
  281. unlink($resumeFile);
  282. unlink($tempFile);
  283. }
  284. $this->assertEquals(count($testInvalidVersions), $expectExceptionCount);
  285. }
  286. }