55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
const assert = require('assert');
|
|
const StorageUtil = require('../utils/StorageUtil.js');
|
|
const FormUtil = require('../utils/FormUtil.js');
|
|
const RequestUtil = require('../utils/RequestUtil.js');
|
|
|
|
assert.strictEqual(FormUtil.trimOrUndefined(' 张三 '), '张三');
|
|
assert.strictEqual(FormUtil.trimOrUndefined(' '), undefined);
|
|
assert.strictEqual(FormUtil.toNumberOrUndefined('12'), 12);
|
|
assert.strictEqual(FormUtil.toNumberOrUndefined(''), undefined);
|
|
assert.strictEqual(FormUtil.toBoolean('on'), true);
|
|
assert.strictEqual(FormUtil.toBoolean('0'), false);
|
|
|
|
const store = {};
|
|
const storage = {
|
|
getItem: (key) => Object.prototype.hasOwnProperty.call(store, key) ? store[key] : null,
|
|
setItem: (key, value) => {
|
|
store[key] = String(value);
|
|
},
|
|
removeItem: (key) => {
|
|
delete store[key];
|
|
}
|
|
};
|
|
|
|
StorageUtil.write(storage, 'token', 'abc');
|
|
assert.strictEqual(StorageUtil.read(storage, 'token'), 'abc');
|
|
StorageUtil.remove(storage, 'token');
|
|
assert.strictEqual(StorageUtil.read(storage, 'token'), null);
|
|
|
|
const calls = [];
|
|
const requester = RequestUtil.createRequester({
|
|
baseUrl: 'https://test-genealogy-api.ddxcjp.cn',
|
|
clientId: 'pc-client',
|
|
tokenHeaderName: 'Authorization',
|
|
getToken: () => 'token-x',
|
|
fetchImpl: async (url, options) => {
|
|
calls.push({ url, options });
|
|
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ code: 200, data: { ok: true } })
|
|
};
|
|
}
|
|
});
|
|
|
|
requester('GET', '/genealogy/pc/auth/profile', {
|
|
query: { keyword: '汤 氏' }
|
|
}).then((data) => {
|
|
assert.deepStrictEqual(data, { ok: true });
|
|
assert.strictEqual(calls[0].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/profile?keyword=%E6%B1%A4+%E6%B0%8F');
|
|
assert.strictEqual(calls[0].options.headers.clientid, 'pc-client');
|
|
assert.strictEqual(calls[0].options.headers.Authorization, 'Bearer token-x');
|
|
console.log('utils tests passed');
|
|
});
|