FormUpTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Qiniu\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use Qiniu\Http\RequestOptions;
  5. use Qiniu\Storage\BucketManager;
  6. use Qiniu\Storage\FormUploader;
  7. use Qiniu\Storage\UploadManager;
  8. use Qiniu\Config;
  9. class FormUpTest extends TestCase
  10. {
  11. private static $bucketName;
  12. private static $auth;
  13. private static $cfg;
  14. private static $keysToCleanup;
  15. /**
  16. * @beforeClass
  17. */
  18. public static function setUpConfigAndBucket()
  19. {
  20. global $bucketName;
  21. self::$bucketName = $bucketName;
  22. global $testAuth;
  23. self::$auth = $testAuth;
  24. self::$cfg = new Config();
  25. self::$keysToCleanup = array();
  26. }
  27. /**
  28. * @afterClass
  29. */
  30. public static function cleanupTestData()
  31. {
  32. $bucketManager = new BucketManager(self::$auth);
  33. $ops = BucketManager::buildBatchDelete(self::$bucketName, self::$keysToCleanup);
  34. // ignore result for cleanup
  35. $bucketManager->batch($ops);
  36. }
  37. private static function getObjectKey($key)
  38. {
  39. $result = $key . rand();
  40. self::$keysToCleanup[] = $result;
  41. return $result;
  42. }
  43. public function testData()
  44. {
  45. $key = self::getObjectKey('formput');
  46. $token = self::$auth->uploadToken(self::$bucketName);
  47. list($ret, $error) = FormUploader::put($token, $key, 'hello world', self::$cfg, null, 'text/plain', null);
  48. $this->assertNull($error);
  49. $this->assertNotNull($ret['hash']);
  50. }
  51. public function testDataWithProxy()
  52. {
  53. $key = self::getObjectKey('formput');
  54. $token = self::$auth->uploadToken(self::$bucketName);
  55. list($ret, $error) = FormUploader::put(
  56. $token,
  57. $key,
  58. 'hello world',
  59. self::$cfg,
  60. null,
  61. 'text/plain',
  62. null,
  63. $this->makeReqOpt()
  64. );
  65. $this->assertNull($error);
  66. $this->assertNotNull($ret['hash']);
  67. }
  68. public function testData2()
  69. {
  70. $key = self::getObjectKey('formput');
  71. $upManager = new UploadManager();
  72. $token = self::$auth->uploadToken(self::$bucketName);
  73. list($ret, $error) = $upManager->put($token, $key, 'hello world', null, 'text/plain', null);
  74. $this->assertNull($error);
  75. $this->assertNotNull($ret['hash']);
  76. }
  77. public function testData2WithProxy()
  78. {
  79. $key = self::getObjectKey('formput');
  80. $upManager = new UploadManager();
  81. $token = self::$auth->uploadToken(self::$bucketName);
  82. list($ret, $error) = $upManager->put(
  83. $token,
  84. $key,
  85. 'hello world',
  86. null,
  87. 'text/plain',
  88. null,
  89. $this->makeReqOpt()
  90. );
  91. $this->assertNull($error);
  92. $this->assertNotNull($ret['hash']);
  93. }
  94. public function testDataFailed()
  95. {
  96. $key = self::getObjectKey('formput');
  97. $token = self::$auth->uploadToken('fakebucket');
  98. list($ret, $error) = FormUploader::put(
  99. $token,
  100. $key,
  101. 'hello world',
  102. self::$cfg,
  103. null,
  104. 'text/plain',
  105. null
  106. );
  107. $this->assertNull($ret);
  108. $this->assertNotNull($error);
  109. }
  110. public function testFile()
  111. {
  112. $key = self::getObjectKey('formPutFile');
  113. $token = self::$auth->uploadToken(self::$bucketName, $key);
  114. list($ret, $error) = FormUploader::putFile(
  115. $token,
  116. $key,
  117. __file__,
  118. self::$cfg,
  119. null,
  120. 'text/plain',
  121. null
  122. );
  123. $this->assertNull($error);
  124. $this->assertNotNull($ret['hash']);
  125. }
  126. public function testFileWithProxy()
  127. {
  128. $key = self::getObjectKey('formPutFile');
  129. $token = self::$auth->uploadToken(self::$bucketName, $key);
  130. list($ret, $error) = FormUploader::putFile(
  131. $token,
  132. $key,
  133. __file__,
  134. self::$cfg,
  135. null,
  136. 'text/plain',
  137. $this->makeReqOpt()
  138. );
  139. $this->assertNull($error);
  140. $this->assertNotNull($ret['hash']);
  141. }
  142. public function testFile2()
  143. {
  144. $key = self::getObjectKey('formPutFile');
  145. $token = self::$auth->uploadToken(self::$bucketName, $key);
  146. $upManager = new UploadManager();
  147. list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain', null);
  148. $this->assertNull($error);
  149. $this->assertNotNull($ret['hash']);
  150. }
  151. public function testFile2WithProxy()
  152. {
  153. $key = self::getObjectKey('formPutFile');
  154. $token = self::$auth->uploadToken(self::$bucketName, $key);
  155. $upManager = new UploadManager();
  156. list($ret, $error) = $upManager->putFile(
  157. $token,
  158. $key,
  159. __file__,
  160. null,
  161. 'text/plain',
  162. false,
  163. null,
  164. 'v1',
  165. Config::BLOCK_SIZE,
  166. $this->makeReqOpt()
  167. );
  168. $this->assertNull($error);
  169. $this->assertNotNull($ret['hash']);
  170. }
  171. public function testFileFailed()
  172. {
  173. $key = self::getObjectKey('fakekey');
  174. $token = self::$auth->uploadToken('fakebucket', $key);
  175. list($ret, $error) = FormUploader::putFile($token, $key, __file__, self::$cfg, null, 'text/plain', null);
  176. $this->assertNull($ret);
  177. $this->assertNotNull($error);
  178. }
  179. private function makeReqOpt()
  180. {
  181. $reqOpt = new RequestOptions();
  182. $reqOpt->proxy = 'socks5://127.0.0.1:8080';
  183. $reqOpt->proxy_user_password = 'user:pass';
  184. return $reqOpt;
  185. }
  186. }