Skip to content

Commit e4c963c

Browse files
committed
fix httpc re-export and add test for needRetry
1 parent 169ef6e commit e4c963c

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module.exports = {
1010
conf: require('./qiniu/conf.js'),
1111
httpc: {
1212
middleware: require('./qiniu/httpc/middleware'),
13-
client: require('./qiniu/httpc/client'),
14-
responseWrapper: require('./qiniu/httpc/responseWrapper')
13+
HttpClient: require('./qiniu/httpc/client').HttpClient,
14+
ResponseWrapper: require('./qiniu/httpc/responseWrapper').ResponseWrapper
1515
},
1616
rpc: require('./qiniu/rpc.js'),
1717
util: require('./qiniu/util.js'),

qiniu/httpc/responseWrapper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ ResponseWrapper.prototype.ok = function () {
1717
* @return {boolean}
1818
*/
1919
ResponseWrapper.prototype.needRetry = function () {
20-
if (!this.resp || !this.resp.statusCode) {
21-
return true;
22-
}
23-
2420
if (this.ok()) {
2521
return false;
2622
}
2723

24+
if (!this.resp || !this.resp.statusCode || this.resp.statusCode < 0) {
25+
return true;
26+
}
27+
2828
const statusCode = this.resp.statusCode;
2929

3030
// 需要重试的特例
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,59 @@ const {
88
} = qiniu.httpc.middleware;
99

1010
describe('test http module', function () {
11+
describe('test http ResponseWrapper', function () {
12+
const { ResponseWrapper } = qiniu.httpc;
13+
14+
it('needRetry', function () {
15+
const rule = [
16+
['-1', true],
17+
['100,499', false],
18+
['500,578', true],
19+
['579', false],
20+
['580,599', true],
21+
['600,611', true],
22+
['612', false],
23+
['613,630', true],
24+
['631', false],
25+
['632,699', true]
26+
];
27+
const cases = [];
28+
for (const [codeRange, shouldRetry] of rule) {
29+
let [start, end] = codeRange.split(',');
30+
start = parseInt(start);
31+
end = parseInt(end);
32+
if (!end) {
33+
cases.push({
34+
code: start,
35+
shouldRetry
36+
});
37+
} else {
38+
for (let i = start; i <= end; i++) {
39+
cases.push({
40+
code: i,
41+
shouldRetry
42+
});
43+
}
44+
}
45+
}
46+
47+
const mockedResponseWrapper = new ResponseWrapper({
48+
data: [],
49+
resp: {
50+
statusCode: 200
51+
}
52+
});
53+
54+
for (const item of cases) {
55+
mockedResponseWrapper.resp.statusCode = item.code;
56+
mockedResponseWrapper.needRetry().should.eql(
57+
item.shouldRetry,
58+
`${item.code} need${item.shouldRetry ? '' : ' NOT'} retry`
59+
);
60+
}
61+
});
62+
});
63+
1164
class OrderRecordMiddleware extends Middleware {
1265
/**
1366
*

0 commit comments

Comments
 (0)