Skip to content

Commit 0a2d84b

Browse files
authored
Merge branch 'master' into fix/header-key-naughty
2 parents 7b86b60 + 066b45c commit 0a2d84b

File tree

7 files changed

+127
-8
lines changed

7 files changed

+127
-8
lines changed

src/Qiniu/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ private function getRegion($accessKey, $bucket)
133133
$this->regionCache[$cacheId] = $region;
134134
} else {
135135
$region = Zone::queryZone($accessKey, $bucket);
136+
if (is_array($region)) {
137+
list($region, $err) = $region;
138+
if ($err != null) {
139+
throw new \Exception($err->message());
140+
}
141+
}
136142
$this->regionCache[$cacheId] = $region;
137143
}
138144
return $region;

src/Qiniu/Storage/BucketManager.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,11 @@ public function fetch($url, $bucket, $key = null)
722722
$path = '/fetch/' . $resource . '/to/' . $to;
723723

724724
$ak = $this->auth->getAccessKey();
725-
$ioHost = $this->config->getIovipHost($ak, $bucket);
725+
try {
726+
$ioHost = $this->config->getIovipHost($ak, $bucket);
727+
} catch (\Exception $err) {
728+
return array(null, $err);
729+
}
726730

727731
$url = $ioHost . $path;
728732
return $this->post($url, null);
@@ -776,7 +780,11 @@ public function asynchFetch(
776780
$data = json_encode($params);
777781

778782
$ak = $this->auth->getAccessKey();
779-
$apiHost = $this->config->getApiHost($ak, $bucket);
783+
try {
784+
$apiHost = $this->config->getApiHost($ak, $bucket);
785+
} catch (\Exception $err) {
786+
return array(null, $err);
787+
}
780788
$url = $apiHost . $path;
781789

782790
return $this->postV2($url, $data);
@@ -826,7 +834,11 @@ public function prefetch($bucket, $key)
826834
$path = '/prefetch/' . $resource;
827835

828836
$ak = $this->auth->getAccessKey();
829-
$ioHost = $this->config->getIovipHost($ak, $bucket);
837+
try {
838+
$ioHost = $this->config->getIovipHost($ak, $bucket);
839+
} catch (\Exception $err) {
840+
return array(null, $err);
841+
}
830842

831843
$url = $ioHost . $path;
832844
return $this->post($url, null);

src/Qiniu/Storage/FormUploader.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Qiniu\Storage;
44

5+
use Qiniu\Config;
56
use Qiniu\Http\Error;
67
use Qiniu\Http\Client;
78

@@ -14,7 +15,7 @@ final class FormUploader
1415
* @param string $upToken 上传凭证
1516
* @param string $key 上传文件名
1617
* @param string $data 上传二进制流
17-
* @param string $config 上传配置
18+
* @param Config $config 上传配置
1819
* @param string $params 自定义变量,规格参考
1920
* https://developer.qiniu.com/kodo/manual/1235/vars#xvar
2021
* @param string $mime 上传数据的mimeType
@@ -56,7 +57,11 @@ public static function put(
5657
return array(null, $err);
5758
}
5859

59-
$upHost = $config->getUpHost($accessKey, $bucket);
60+
try {
61+
$upHost = $config->getUpHost($accessKey, $bucket);
62+
} catch (\Exception $err) {
63+
return array(null, $err);
64+
}
6065

6166
$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
6267
if (!$response->ok()) {
@@ -71,7 +76,7 @@ public static function put(
7176
* @param string $upToken 上传凭证
7277
* @param string $key 上传文件名
7378
* @param string $filePath 上传文件的路径
74-
* @param string $config 上传配置
79+
* @param Config $config 上传配置
7580
* @param string $params 自定义变量,规格参考
7681
* https://developer.qiniu.com/kodo/manual/1235/vars#xvar
7782
* @param string $mime 上传数据的mimeType
@@ -112,7 +117,11 @@ public static function putFile(
112117
return array(null, $err);
113118
}
114119

115-
$upHost = $config->getUpHost($accessKey, $bucket);
120+
try {
121+
$upHost = $config->getUpHost($accessKey, $bucket);
122+
} catch (\Exception $err) {
123+
return array(null, $err);
124+
}
116125

117126
$response = Client::post($upHost, $fields, $headers);
118127
if (!$response->ok()) {

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class ResumeUploader
4040
* @param string $size 上传流的大小
4141
* @param string $params 自定义变量
4242
* @param string $mime 上传数据的mimeType
43-
* @param string $config
43+
* @param Config $config
4444
* @param string $resumeRecordFile 断点续传的已上传的部分信息记录文件
4545
* @param string $version 分片上传版本 目前支持v1/v2版本 默认v1
4646
* @param string $partSize 分片上传v2字段 默认大小为4MB 分片大小范围为1 MB - 1 GB

tests/Qiniu/Tests/BucketTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ public function testPrefetch()
271271
$this->assertNull($error);
272272
}
273273

274+
public function testPrefetchFailed()
275+
{
276+
list($ret, $error) = $this->bucketManager->prefetch(
277+
'fakebucket',
278+
'php-sdk.html'
279+
);
280+
$this->assertNull($ret);
281+
$this->assertNotNull($error);
282+
}
283+
274284
public function testFetch()
275285
{
276286
list($ret, $error) = $this->bucketManager->fetch(
@@ -297,6 +307,16 @@ public function testFetch()
297307
$this->assertNull($error);
298308
}
299309

310+
public function testFetchFailed()
311+
{
312+
list($ret, $error) = $this->bucketManager->fetch(
313+
'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
314+
'fakebucket'
315+
);
316+
$this->assertNull($ret);
317+
$this->assertNotNull($error);
318+
}
319+
300320
public function testAsynchFetch()
301321
{
302322
list($ret, $error) = $this->bucketManager->asynchFetch(
@@ -325,6 +345,16 @@ public function testAsynchFetch()
325345
$this->assertNull($error);
326346
}
327347

348+
public function testAsynchFetchFailed()
349+
{
350+
list($ret, $error) = $this->bucketManager->asynchFetch(
351+
'http://devtools.qiniu.com/qiniu.png',
352+
'fakebucket'
353+
);
354+
$this->assertNull($ret);
355+
$this->assertNotNull($error);
356+
}
357+
328358

329359
public function testBatchCopy()
330360
{

tests/Qiniu/Tests/ConfigTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Qiniu\Tests {
4+
use Qiniu\Config;
5+
6+
class ConfigTest extends \PHPUnit_Framework_TestCase
7+
{
8+
protected $accessKey;
9+
protected $bucketName;
10+
11+
protected function setUp()
12+
{
13+
global $accessKey;
14+
$this->accessKey = $accessKey;
15+
global $bucketName;
16+
$this->bucketName = $bucketName;
17+
}
18+
19+
public function testGetApiHost()
20+
{
21+
$conf = new Config();
22+
$hasException = false;
23+
$apiHost = '';
24+
try {
25+
$apiHost = $conf->getApiHost($this->accessKey, $this->bucketName);
26+
} catch (\Exception $e) {
27+
$hasException = true;
28+
}
29+
$this->assertFalse($hasException);
30+
$this->assertEquals('http://api.qiniu.com', $apiHost);
31+
}
32+
33+
public function testGetApiHostErrored()
34+
{
35+
$conf = new Config();
36+
$hasException = false;
37+
try {
38+
$conf->getApiHost($this->accessKey, "fakebucket");
39+
} catch (\Exception $e) {
40+
$hasException = true;
41+
}
42+
$this->assertTrue($hasException);
43+
}
44+
}
45+
}

tests/Qiniu/Tests/FormUpTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function testData2()
3838
$this->assertNotNull($ret['hash']);
3939
}
4040

41+
public function testDataFailed()
42+
{
43+
$token = $this->auth->uploadToken('fakebucket');
44+
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', null);
45+
$this->assertNull($ret);
46+
$this->assertNotNull($error);
47+
}
48+
4149
public function testFile()
4250
{
4351
$key = 'formPutFile';
@@ -56,4 +64,13 @@ public function testFile2()
5664
$this->assertNull($error);
5765
$this->assertNotNull($ret['hash']);
5866
}
67+
68+
public function testFileFailed()
69+
{
70+
$key = 'fakekey';
71+
$token = $this->auth->uploadToken('fakebucket', $key);
72+
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', null);
73+
$this->assertNull($ret);
74+
$this->assertNotNull($error);
75+
}
5976
}

0 commit comments

Comments
 (0)