Skip to content

Commit 7a11eda

Browse files
authored
fix: putBucketWebsite testting (#785)
1 parent 2dacfa2 commit 7a11eda

File tree

3 files changed

+199
-4
lines changed

3 files changed

+199
-4
lines changed

test/node/bucket.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { metaSyncTime } = require('../config');
1313
// }
1414

1515
describe('test/bucket.test.js', () => {
16-
const { prefix } = utils;
16+
const { prefix, includesConf } = utils;
1717
let store;
1818
let bucket;
1919
let bucketRegion;
@@ -344,15 +344,15 @@ describe('test/bucket.test.js', () => {
344344
const result1 = await store.putBucketWebsite(bucket, website);
345345
assert.strictEqual(result1.res.status, 200);
346346
const rules1 = await store.getBucketWebsite(bucket);
347-
assert.deepStrictEqual(rules1.routingRules, routingRules);
347+
includesConf(rules1.routingRules, routingRules);
348348
assert.strictEqual(rules1.supportSubDir, website.supportSubDir);
349349
assert.strictEqual(rules1.type, website.type);
350350

351351
website.routingRules = [routingRule1];
352352
const result2 = await store.putBucketWebsite(bucket, website);
353353
assert.strictEqual(result2.res.status, 200);
354354
const rules2 = await store.getBucketWebsite(bucket);
355-
assert.deepStrictEqual(rules2.routingRules, website.routingRules);
355+
includesConf(rules2.routingRules, website.routingRules);
356356
});
357357

358358
it('should throw error when RoutingRules is not Array', async () => {

test/node/utils.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const assert = require('assert');
1515
const fs = require('fs');
1616
const urlutil = require('url');
1717
const platform = require('platform');
18+
const isObject = require('../../lib/common/utils/isObject');
19+
const isArray = require('../../lib/common/utils/isArray');
1820

1921
exports.throws = async function (block, checkError) {
2022
try {
@@ -31,7 +33,7 @@ exports.throws = async function (block, checkError) {
3133
if (!checkError.test(err.toString())) {
3234
throw new Error(`expected ${err.toString()} to match ${checkError.toString()}`);
3335
}
34-
return;
36+
return false;
3537
}
3638
throw new Error(`${block.toString()} should throws error`);
3739
};
@@ -150,3 +152,48 @@ exports.encodeCallback = function (cb) {
150152

151153
return Buffer.from(JSON.stringify(json)).toString('base64');
152154
};
155+
156+
// 如果配置属性值是数组 则判断配置的数组是不是数据的子数组。
157+
// 如果配置属性值是对象 则判断数据包含的属性值包不包含配置项属性值。
158+
// 如果配置属性值是简单数据类型 则判断数据的有配置的属性且值相等
159+
exports.includesConf = function includesConf(data, conf) {
160+
if (conf === null || typeof conf !== 'object') {
161+
return data === conf;
162+
}
163+
164+
let valid = true;
165+
if (isArray(conf)) {
166+
if (!isArray(data)) return false;
167+
for (let i = 0; i < conf.length; i++) {
168+
let itemValid = false;
169+
for (let j = 0; j < data.length; j++) {
170+
if (includesConf(data[j], conf[i])) {
171+
itemValid = true;
172+
break;
173+
}
174+
}
175+
if (!itemValid) return false;
176+
}
177+
return valid;
178+
}
179+
180+
const keys = Object.keys(conf);
181+
for (let i = 0; i < keys.length; i++) {
182+
const key = keys[i];
183+
if (!isObject(conf[key]) && !isArray(conf[key])) {
184+
if (conf[key] !== data[key]) {
185+
valid = false;
186+
break;
187+
}
188+
} else if (isObject(conf[key]) || isArray(conf[key])) {
189+
if (!includesConf(data[key], conf[key])) {
190+
valid = false;
191+
break;
192+
}
193+
} else if (conf[key] !== data[key]) {
194+
valid = false;
195+
break;
196+
}
197+
}
198+
return valid;
199+
};

test/node/utils.test.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _isIP = require('../../lib/common/utils/isIP');
2+
const { includesConf } = require('./utils');
23
const assert = require('assert');
34

45
describe('test/test.js', () => {
@@ -76,3 +77,150 @@ describe('test/test.js', () => {
7677
assert.equal(_isIP('G:0f:0F:FFFF:5:6:7:8'), false);
7778
});
7879
});
80+
81+
describe('test/includesConf.js', () => {
82+
it('shoud return true when conf-item is primitive value', () => {
83+
const data = {
84+
testNum: 1,
85+
testStr: '2',
86+
testUndefined: undefined,
87+
testNull: null,
88+
testExtral: 'extral'
89+
};
90+
const conf = {
91+
testNum: 1,
92+
testStr: '2',
93+
testUndefined: undefined,
94+
testNull: null
95+
};
96+
assert(includesConf(data, conf));
97+
});
98+
it('shoud return false when conf-item is primitive value and conf not in data', () => {
99+
const data = {
100+
testNum: 1,
101+
testStr: '2',
102+
testUndefined: undefined,
103+
testNull: null,
104+
testExtral: 'extral'
105+
};
106+
const conf = {
107+
testNonExist: 1
108+
};
109+
const conf1 = {
110+
testExtral: 'test'
111+
};
112+
assert(!includesConf(data, conf));
113+
assert(!includesConf(data, conf1));
114+
});
115+
it('shoud return true when conf-item is simple Array', () => {
116+
const data = {
117+
testArray1: ['extral', '1', 0, undefined],
118+
testExtral: 'extral'
119+
};
120+
const conf = {
121+
testArray1: ['1', 0, undefined]
122+
};
123+
assert(includesConf(data, conf));
124+
});
125+
it('shoud return false when conf-item is simple Array and conf not in data', () => {
126+
const data = {
127+
testArray1: ['extral', '1', 0, undefined],
128+
testExtral: 'extral'
129+
};
130+
const conf = {
131+
testArray1: ['1', 0, undefined, 'noexist']
132+
};
133+
assert(!includesConf(data, conf));
134+
});
135+
it('shoud return true when conf-item is simple Object', () => {
136+
const data = {
137+
testObject: { test: 1, test1: 2 },
138+
testExtral: 'extral'
139+
};
140+
const conf = {
141+
testObject: { test: 1 }
142+
};
143+
assert(includesConf(data, conf));
144+
});
145+
it('shoud return false when conf-item is simple Object and conf not in data', () => {
146+
const data = {
147+
testObject: { test: 1, test1: 2 },
148+
testExtral: 'extral'
149+
};
150+
const conf = {
151+
testObject: { test: 1, noExist: 'test' }
152+
};
153+
assert(!includesConf(data, conf));
154+
});
155+
it('shoud return true when conf-item is complex Array', () => {
156+
const data = {
157+
testArray: [{ test: 1, test1: 2 }, { test: 2 }],
158+
testExtral: 'extral'
159+
};
160+
const conf = {
161+
testArray: [{ test: 2 }]
162+
};
163+
assert(includesConf(data, conf));
164+
});
165+
it('shoud return false when conf-item is complex Array and conf not in data', () => {
166+
const data = {
167+
testArray: [{ test: 1, test1: 2 }, { test: 2 }],
168+
testExtral: 'extral'
169+
};
170+
const conf = {
171+
testArray: [{ test: 0 }]
172+
};
173+
assert(!includesConf(data, conf));
174+
});
175+
it('shoud return true when conf-item is complex Object', () => {
176+
const data = {
177+
testObject: {
178+
test01: {
179+
test11: {
180+
a: 1
181+
},
182+
test12: 1123
183+
},
184+
test02: [{ test11: 1 }, '123', 0, undefined, '456']
185+
},
186+
testExtral: 'extral'
187+
};
188+
const conf = {
189+
testObject: {
190+
test01: {
191+
test11: {
192+
a: 1
193+
}
194+
},
195+
test02: [{ test11: 1 }, '123', 0, undefined]
196+
}
197+
};
198+
assert(includesConf(data, conf));
199+
});
200+
it('shoud return false when conf-item is complex Object and conf not in data', () => {
201+
const data = {
202+
testObject: {
203+
test01: {
204+
test11: {
205+
a: 1
206+
},
207+
test12: 1123
208+
},
209+
test02: [{ test11: 1 }, '123', 0, undefined, '456']
210+
},
211+
testExtral: 'extral'
212+
};
213+
const conf = {
214+
testObject: {
215+
test01: {
216+
test11: {
217+
a: 1,
218+
b: 'test cpx'
219+
}
220+
},
221+
test02: [{ test11: 1 }, '123', 0, undefined]
222+
}
223+
};
224+
assert(!includesConf(data, conf));
225+
});
226+
});

0 commit comments

Comments
 (0)