corp-mp/src/lib/sign.test.ts

51 lines
1.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { signRequest } from './sign'
describe('signRequest', () => {
it('signs GET query params the same way as H5', () => {
const signed = signRequest({
method: 'GET',
params: {
phone: '13800138000',
timestamp: 1700000000,
nonce: 'abc-nonce',
},
})
expect(signed.params.signature).toBe('170c9063021f3863d00c32b5ddffeb01')
})
it('signs POST with query params plus JSON body', () => {
const signed = signRequest({
method: 'POST',
params: {
phone: '13800138000',
timestamp: 1700000000,
nonce: 'abc-nonce',
},
data: {
id: '12',
entity_name: '店',
},
})
expect(signed.params.signature).toBe('0b7bcf9a71df45452fda85a3a45b9a8a')
})
it('treats PUT like POST', () => {
const signed = signRequest({
method: 'PUT',
params: {
timestamp: 1700000000,
nonce: 'abc-nonce',
},
data: {
id: '12',
},
})
expect(typeof signed.params.signature).toBe('string')
expect(signed.params.signature).toHaveLength(32)
})
})