Skip to content

Commit

Permalink
fix: putBucketWebsite testting (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie authored May 10, 2020
1 parent 2dacfa2 commit 7a11eda
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 4 deletions.
6 changes: 3 additions & 3 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { metaSyncTime } = require('../config');
// }

describe('test/bucket.test.js', () => {
const { prefix } = utils;
const { prefix, includesConf } = utils;
let store;
let bucket;
let bucketRegion;
Expand Down Expand Up @@ -344,15 +344,15 @@ describe('test/bucket.test.js', () => {
const result1 = await store.putBucketWebsite(bucket, website);
assert.strictEqual(result1.res.status, 200);
const rules1 = await store.getBucketWebsite(bucket);
assert.deepStrictEqual(rules1.routingRules, routingRules);
includesConf(rules1.routingRules, routingRules);
assert.strictEqual(rules1.supportSubDir, website.supportSubDir);
assert.strictEqual(rules1.type, website.type);

website.routingRules = [routingRule1];
const result2 = await store.putBucketWebsite(bucket, website);
assert.strictEqual(result2.res.status, 200);
const rules2 = await store.getBucketWebsite(bucket);
assert.deepStrictEqual(rules2.routingRules, website.routingRules);
includesConf(rules2.routingRules, website.routingRules);
});

it('should throw error when RoutingRules is not Array', async () => {
Expand Down
49 changes: 48 additions & 1 deletion test/node/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const assert = require('assert');
const fs = require('fs');
const urlutil = require('url');
const platform = require('platform');
const isObject = require('../../lib/common/utils/isObject');
const isArray = require('../../lib/common/utils/isArray');

exports.throws = async function (block, checkError) {
try {
Expand All @@ -31,7 +33,7 @@ exports.throws = async function (block, checkError) {
if (!checkError.test(err.toString())) {
throw new Error(`expected ${err.toString()} to match ${checkError.toString()}`);
}
return;
return false;
}
throw new Error(`${block.toString()} should throws error`);
};
Expand Down Expand Up @@ -150,3 +152,48 @@ exports.encodeCallback = function (cb) {

return Buffer.from(JSON.stringify(json)).toString('base64');
};

// 如果配置属性值是数组 则判断配置的数组是不是数据的子数组。
// 如果配置属性值是对象 则判断数据包含的属性值包不包含配置项属性值。
// 如果配置属性值是简单数据类型 则判断数据的有配置的属性且值相等
exports.includesConf = function includesConf(data, conf) {
if (conf === null || typeof conf !== 'object') {
return data === conf;
}

let valid = true;
if (isArray(conf)) {
if (!isArray(data)) return false;
for (let i = 0; i < conf.length; i++) {
let itemValid = false;
for (let j = 0; j < data.length; j++) {
if (includesConf(data[j], conf[i])) {
itemValid = true;
break;
}
}
if (!itemValid) return false;
}
return valid;
}

const keys = Object.keys(conf);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!isObject(conf[key]) && !isArray(conf[key])) {
if (conf[key] !== data[key]) {
valid = false;
break;
}
} else if (isObject(conf[key]) || isArray(conf[key])) {
if (!includesConf(data[key], conf[key])) {
valid = false;
break;
}
} else if (conf[key] !== data[key]) {
valid = false;
break;
}
}
return valid;
};
148 changes: 148 additions & 0 deletions test/node/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _isIP = require('../../lib/common/utils/isIP');
const { includesConf } = require('./utils');
const assert = require('assert');

describe('test/test.js', () => {
Expand Down Expand Up @@ -76,3 +77,150 @@ describe('test/test.js', () => {
assert.equal(_isIP('G:0f:0F:FFFF:5:6:7:8'), false);
});
});

describe('test/includesConf.js', () => {
it('shoud return true when conf-item is primitive value', () => {
const data = {
testNum: 1,
testStr: '2',
testUndefined: undefined,
testNull: null,
testExtral: 'extral'
};
const conf = {
testNum: 1,
testStr: '2',
testUndefined: undefined,
testNull: null
};
assert(includesConf(data, conf));
});
it('shoud return false when conf-item is primitive value and conf not in data', () => {
const data = {
testNum: 1,
testStr: '2',
testUndefined: undefined,
testNull: null,
testExtral: 'extral'
};
const conf = {
testNonExist: 1
};
const conf1 = {
testExtral: 'test'
};
assert(!includesConf(data, conf));
assert(!includesConf(data, conf1));
});
it('shoud return true when conf-item is simple Array', () => {
const data = {
testArray1: ['extral', '1', 0, undefined],
testExtral: 'extral'
};
const conf = {
testArray1: ['1', 0, undefined]
};
assert(includesConf(data, conf));
});
it('shoud return false when conf-item is simple Array and conf not in data', () => {
const data = {
testArray1: ['extral', '1', 0, undefined],
testExtral: 'extral'
};
const conf = {
testArray1: ['1', 0, undefined, 'noexist']
};
assert(!includesConf(data, conf));
});
it('shoud return true when conf-item is simple Object', () => {
const data = {
testObject: { test: 1, test1: 2 },
testExtral: 'extral'
};
const conf = {
testObject: { test: 1 }
};
assert(includesConf(data, conf));
});
it('shoud return false when conf-item is simple Object and conf not in data', () => {
const data = {
testObject: { test: 1, test1: 2 },
testExtral: 'extral'
};
const conf = {
testObject: { test: 1, noExist: 'test' }
};
assert(!includesConf(data, conf));
});
it('shoud return true when conf-item is complex Array', () => {
const data = {
testArray: [{ test: 1, test1: 2 }, { test: 2 }],
testExtral: 'extral'
};
const conf = {
testArray: [{ test: 2 }]
};
assert(includesConf(data, conf));
});
it('shoud return false when conf-item is complex Array and conf not in data', () => {
const data = {
testArray: [{ test: 1, test1: 2 }, { test: 2 }],
testExtral: 'extral'
};
const conf = {
testArray: [{ test: 0 }]
};
assert(!includesConf(data, conf));
});
it('shoud return true when conf-item is complex Object', () => {
const data = {
testObject: {
test01: {
test11: {
a: 1
},
test12: 1123
},
test02: [{ test11: 1 }, '123', 0, undefined, '456']
},
testExtral: 'extral'
};
const conf = {
testObject: {
test01: {
test11: {
a: 1
}
},
test02: [{ test11: 1 }, '123', 0, undefined]
}
};
assert(includesConf(data, conf));
});
it('shoud return false when conf-item is complex Object and conf not in data', () => {
const data = {
testObject: {
test01: {
test11: {
a: 1
},
test12: 1123
},
test02: [{ test11: 1 }, '123', 0, undefined, '456']
},
testExtral: 'extral'
};
const conf = {
testObject: {
test01: {
test11: {
a: 1,
b: 'test cpx'
}
},
test02: [{ test11: 1 }, '123', 0, undefined]
}
};
assert(!includesConf(data, conf));
});
});

0 comments on commit 7a11eda

Please sign in to comment.