import { MD5 } from 'crypto-js' import { signature, signByStr } from '..' test('test - 1 | 签名生成 (默认: MD5)', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' } }).toString() expect(res).toBe('db4349c1cc9dba17f4a50c0d629972bf') }) test('test - 2 | 签名生成 (SHA1)', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' } }).toString() expect(res).toBe('db4349c1cc9dba17f4a50c0d629972bf') }) test('test - 3 | 签名生成 (SHA256)', async () => { const res = signature({ data: { a: 1, b: 'awef' }, hashFactory: 'SHA1' }).toString() expect(res).toBe('4f197941670ef8ee828ffef2ebd8b8ed44e465bc') }) test('test - 4 | 签名生成 (中文签名)', async () => { const res = signature({ data: { a: 1, b: 'awef', c: '中文' } }).toString() expect(res).toBe('ff27192ebcf869a560783f8e19e87818') }) test('test - 5 | 签名生成 (自定义签名方法)', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' }, hashFactory: (str: string): string => { return MD5(str).toString() } }).toString() expect(res).toBe('db4349c1cc9dba17f4a50c0d629972bf') }) test('test - 6 | 生成加盐签名', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' }, suffix: '&aaefjiajefiwef' }).toString() expect(res).toBe('e6a5a8abc310c8485d65e292002f7028') }) test('test - 7 | 生成加盐签名(自定义参数后缀)', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' }, suffix: '&aaefjiajefiwef' }).toString() expect(res).toBe('e6a5a8abc310c8485d65e292002f7028') }) test('test - 8 | 生成参数排序后的签名', async () => { const res = signature({ data: { b: '123', c: 'aef', a: 'afko0' }, sort: true }).toString() expect(res).toBe('59320015ba9cc63e7791548dc3d2d86a') }) test('test - 9 | 参数签名自定义排序', async () => { const res = signature({ data: { b: '123', d: '124', c: 'aef', a: 'afko0' }, sort: (key1: string, key2: string) => { if (key1 === 'd') return -1 else return key1 < key2 ? -1 : 1 } }).toString() expect(res).toBe('5b96285af2cc1c73515f953e8b3a310e') }) test('test - 10 | 生成签名并附加给url', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' } }).addToUrl('http://test.com') expect(res).toBe('http://test.com?a=1&b=awef&c=awfejiawf&signature=db4349c1cc9dba17f4a50c0d629972bf') }) test('test - 11 | 生成签名并附加给url (自定义key)', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' } }).addToUrl('http://test.com', 'sign') expect(res).toBe('http://test.com?a=1&b=awef&c=awfejiawf&sign=db4349c1cc9dba17f4a50c0d629972bf') }) test('test - 12 | 生成签名并附加给url (同名参数覆盖)', async () => { const res = signature({ data: { a: 1, b: 'awef', c: 'awfejiawf' } }).addToUrl('http://test.com?a=3&n=efw') // # 未覆盖 expect(res).not.toBe('http://test.com?a=3&b=awef&c=awfejiawf&n=efw&signature=db4349c1cc9dba17f4a50c0d629972bf') // # 已覆盖 expect(res).toBe('http://test.com?a=3&b=awef&c=awfejiawf&n=efw&signature=0e11c4db786b084236a98e2969ee22cb') }) test('test - 13 | 根据url串生成签名 (简易)', async () => { const res = signByStr( 'http://test.com?c=123&b=13&d=qwe&e=awgk', { c: 123, f: 'qgq' }, { sort: true } ) expect(res).toBe('http://test.com?b=13&c=123&d=qwe&e=awgk&f=qgq&signature=4cb0ce5cc18ecb67f66ed5c320507e95') })