修改底层请求逻辑,封装请求方法
This commit is contained in:
+44
-35
@@ -1,5 +1,5 @@
|
||||
const assert = require('assert');
|
||||
const GenealogyApi = require('../public/js/api-client.js');
|
||||
const GenealogyApi = require('../utils/ApiClient.js');
|
||||
|
||||
function createStore(initial) {
|
||||
const values = Object.assign({}, initial);
|
||||
@@ -24,7 +24,8 @@ async function run() {
|
||||
'token-key': 'abc-token'
|
||||
});
|
||||
|
||||
assert.strictEqual(GenealogyApi.DEFAULT_BASE_URL, 'http://test-genealogy-api.ddxcjp.cn');
|
||||
// 接口基础地址只能由根目录 config.js 负责,公共客户端不能重复声明。
|
||||
assert.strictEqual(GenealogyApi.DEFAULT_BASE_URL, undefined);
|
||||
|
||||
const client = GenealogyApi.createClient({
|
||||
baseUrl: 'https://test-genealogy-api.ddxcjp.cn',
|
||||
@@ -33,31 +34,32 @@ async function run() {
|
||||
tokenKey: 'token-key',
|
||||
tokenHeaderName: 'Authorization',
|
||||
tokenStore: store,
|
||||
fetchImpl: async (url, options) => {
|
||||
calls.push({ url, options });
|
||||
axiosInstance: {
|
||||
request: async (config) => {
|
||||
calls.push(config);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
return {
|
||||
status: 200,
|
||||
data: {
|
||||
code: 200,
|
||||
msg: '操作成功',
|
||||
data: { token: 'server-token', url }
|
||||
})
|
||||
};
|
||||
data: { token: 'server-token', url: config.url }
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
await client.login({ phone: '13800000000', password: 'md5' });
|
||||
assert.strictEqual(calls[0].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/login');
|
||||
assert.strictEqual(calls[0].options.headers.clientid, 'client-x');
|
||||
assert.strictEqual(calls[0].options.headers.Authorization, undefined);
|
||||
assert.strictEqual(JSON.parse(calls[0].options.body).tenantId, 'tenant-x');
|
||||
assert.strictEqual(calls[0].url, '/genealogy/pc/auth/login');
|
||||
assert.strictEqual(calls[0].headers.clientid, 'client-x');
|
||||
assert.strictEqual(calls[0].headers.Authorization, undefined);
|
||||
assert.strictEqual(calls[0].data.tenantId, 'tenant-x');
|
||||
assert.strictEqual(store.values['token-key'], 'server-token');
|
||||
|
||||
await client.getProfile();
|
||||
assert.strictEqual(calls[1].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/profile');
|
||||
assert.strictEqual(calls[1].options.headers.Authorization, 'Bearer server-token');
|
||||
assert.strictEqual(calls[1].url, '/genealogy/pc/auth/profile');
|
||||
assert.strictEqual(calls[1].headers.Authorization, 'Bearer server-token');
|
||||
|
||||
await client.updateProfile({
|
||||
nickName: '张三',
|
||||
@@ -65,12 +67,12 @@ async function run() {
|
||||
cityCode: '510100',
|
||||
districtCode: '510104'
|
||||
});
|
||||
assert.strictEqual(calls[2].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/profile');
|
||||
assert.strictEqual(calls[2].options.method, 'PUT');
|
||||
assert.strictEqual(calls[2].url, '/genealogy/pc/auth/profile');
|
||||
assert.strictEqual(calls[2].method, 'put');
|
||||
|
||||
await client.uploadFile('file-content');
|
||||
assert.strictEqual(calls[3].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/files/upload');
|
||||
assert.strictEqual(calls[3].options.headers['Content-Type'], undefined);
|
||||
assert.strictEqual(calls[3].url, '/genealogy/pc/files/upload');
|
||||
assert.strictEqual(calls[3].headers['Content-Type'], undefined);
|
||||
|
||||
await client.initResumableUpload({
|
||||
fileName: 'cover.jpg',
|
||||
@@ -79,10 +81,11 @@ async function run() {
|
||||
chunkSize: 4194304,
|
||||
totalChunks: 1
|
||||
});
|
||||
assert.strictEqual(calls[4].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/files/resumable/init');
|
||||
assert.strictEqual(calls[4].url, '/genealogy/pc/files/resumable/init');
|
||||
|
||||
await client.getRegionChildren('51');
|
||||
assert.strictEqual(calls[5].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/region/children?parentCode=51');
|
||||
assert.strictEqual(calls[5].url, '/genealogy/region/children');
|
||||
assert.deepStrictEqual(calls[5].params, { parentCode: '51' });
|
||||
|
||||
await client.captchaRequire({
|
||||
sceneCode: 'WEB_H5_LOGIN',
|
||||
@@ -90,31 +93,37 @@ async function run() {
|
||||
});
|
||||
assert.strictEqual(
|
||||
calls[6].url,
|
||||
'https://test-genealogy-api.ddxcjp.cn/captcha/require?tenantId=tenant-x&clientId=client-x&sceneCode=WEB_H5_LOGIN&subject=13800000000'
|
||||
'/captcha/require'
|
||||
);
|
||||
assert.strictEqual(calls[6].options.headers.Authorization, undefined);
|
||||
assert.deepStrictEqual(calls[6].params, {
|
||||
tenantId: 'tenant-x',
|
||||
clientId: 'client-x',
|
||||
sceneCode: 'WEB_H5_LOGIN',
|
||||
subject: '13800000000'
|
||||
});
|
||||
assert.strictEqual(calls[6].headers.Authorization, undefined);
|
||||
|
||||
await client.loginBySms({
|
||||
phone: '13800000000',
|
||||
smsCode: '6666',
|
||||
validToken: 'sms-ticket'
|
||||
});
|
||||
assert.strictEqual(calls[7].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/login/sms');
|
||||
assert.strictEqual(calls[7].options.headers.Authorization, undefined);
|
||||
assert.strictEqual(JSON.parse(calls[7].options.body).grantType, 'sms');
|
||||
assert.strictEqual(JSON.parse(calls[7].options.body).tenantId, 'tenant-x');
|
||||
assert.strictEqual(JSON.parse(calls[7].options.body).clientId, 'client-x');
|
||||
assert.strictEqual(calls[7].url, '/genealogy/pc/auth/login/sms');
|
||||
assert.strictEqual(calls[7].headers.Authorization, undefined);
|
||||
assert.strictEqual(calls[7].data.grantType, 'sms');
|
||||
assert.strictEqual(calls[7].data.tenantId, 'tenant-x');
|
||||
assert.strictEqual(calls[7].data.clientId, 'client-x');
|
||||
|
||||
await client.sendSmsCode({
|
||||
sceneCode: 'WEB_H5_LOGIN',
|
||||
phone: '13800000000',
|
||||
validToken: 'captcha-ticket'
|
||||
});
|
||||
assert.strictEqual(calls[8].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/sms/code');
|
||||
assert.strictEqual(calls[8].options.headers.Authorization, undefined);
|
||||
assert.strictEqual(JSON.parse(calls[8].options.body).grantType, 'sms');
|
||||
assert.strictEqual(JSON.parse(calls[8].options.body).sceneCode, 'WEB_H5_LOGIN');
|
||||
assert.strictEqual(JSON.parse(calls[8].options.body).validToken, 'captcha-ticket');
|
||||
assert.strictEqual(calls[8].url, '/genealogy/pc/auth/sms/code');
|
||||
assert.strictEqual(calls[8].headers.Authorization, undefined);
|
||||
assert.strictEqual(calls[8].data.grantType, 'sms');
|
||||
assert.strictEqual(calls[8].data.sceneCode, 'WEB_H5_LOGIN');
|
||||
assert.strictEqual(calls[8].data.validToken, 'captcha-ticket');
|
||||
|
||||
assert.throws(
|
||||
() => client.listGenealogies(),
|
||||
|
||||
Reference in New Issue
Block a user