No Description

ResumeUpTest.php 9.3KB

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