93 lines
2.5 KiB
JavaScript
93 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const PddClient = require('../../../app/lib/pdd_client');
|
|
|
|
describe('test/app/lib/pdd_client.test.js', () => {
|
|
it('应从 config.pdd 读取凭据并发送签名后的 JSON 请求', async () => {
|
|
let requestOptions = null;
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {
|
|
pdd: {
|
|
client_id: '1',
|
|
client_secret: 'testSecret',
|
|
},
|
|
},
|
|
},
|
|
async curl(url, options) {
|
|
requestOptions = { url, options };
|
|
return {
|
|
data: {
|
|
code: 0,
|
|
msg: 'ok',
|
|
},
|
|
};
|
|
},
|
|
};
|
|
|
|
const client = new PddClient(ctx);
|
|
const result = await client.syncInvoke({
|
|
type: 'pdd.order.number.list.get',
|
|
timestamp: '1480411125',
|
|
dataType: 'XML',
|
|
accessToken: 'asd78172s8ds9a921j9qqwda12312w1w21211',
|
|
bizParams: {
|
|
order_status: '1',
|
|
page: '1',
|
|
page_size: '10',
|
|
},
|
|
});
|
|
|
|
assert.deepEqual(result, { code: 0, msg: 'ok' });
|
|
assert.equal(requestOptions.url, 'https://gw-api.pinduoduo.com/api/router');
|
|
assert.equal(requestOptions.options.method, 'POST');
|
|
assert.equal(requestOptions.options.headers['Content-Type'], 'application/json');
|
|
assert.equal(requestOptions.options.data.type, 'pdd.order.number.list.get');
|
|
assert.equal(requestOptions.options.data.client_id, '1');
|
|
assert.equal(requestOptions.options.data.sign, 'E4DE3ED21002510DED352819E7AE6775');
|
|
});
|
|
|
|
it('request 入参中的 clientId 和 clientSecret 应覆盖默认配置', async () => {
|
|
let requestOptions = null;
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {
|
|
pdd: {
|
|
client_id: 'default-client-id',
|
|
client_secret: 'default-secret',
|
|
},
|
|
},
|
|
},
|
|
async curl(url, options) {
|
|
requestOptions = { url, options };
|
|
return {
|
|
data: {
|
|
success: true,
|
|
},
|
|
};
|
|
},
|
|
};
|
|
|
|
const client = new PddClient(ctx);
|
|
await client.asyncInvoke({
|
|
type: 'pdd.order.number.list.get',
|
|
clientId: 'override-client-id',
|
|
clientSecret: 'override-secret',
|
|
timestamp: '1480411125',
|
|
dataType: 'XML',
|
|
accessToken: 'token-1',
|
|
bizParams: {
|
|
order_status: '1',
|
|
page: '1',
|
|
page_size: '10',
|
|
},
|
|
});
|
|
|
|
assert.equal(requestOptions.options.data.client_id, 'override-client-id');
|
|
assert.notEqual(requestOptions.options.data.sign, 'E4DE3ED21002510DED352819E7AE6775');
|
|
});
|
|
});
|