UNPKG

@qiniu/miku-delivery-mp-ks

Version:

Kuaishou Mini Program SDK for Miku Delivery

128 lines (127 loc) 6.42 kB
import { getCacheKey, applyRewrites, applyQueryStringConfig } from './cache-key'; describe('getCacheKey', () => { it('should work well', () => { const cacheKey = getCacheKey('https://foo.com/bar?a=1&b&c=2', { rewrites: [], queryString: { type: 'all' } }, ''); expect(cacheKey).toBe('foo.com/bar?a=1&b&c=2'); }); it('should work well with bucket ID', () => { const cacheKey = getCacheKey('https://foo.com/bar?a=1&b&c=2', { rewrites: [], queryString: { type: 'all' } }, 'test-bucket'); expect(cacheKey).toBe('test-bucket/bar?a=1&b&c=2'); }); it('should work well with rewrites', () => { const cacheKey = getCacheKey('https://foo.com/w/300/200/apple', { rewrites: [ { pattern: '/w/(\\d+)/(\\d+)/(.*)', repl: '/${3}?imageView/2/w/${1}' } ], queryString: { type: 'all' } }, ''); expect(cacheKey).toBe('foo.com/apple?imageView/2/w/300'); }); it('should work well with queryString', () => { const cacheKey = getCacheKey('https://foo.com/bar?a=1&b&c=2', { rewrites: [], queryString: { type: 'include', values: ['b', 'a'] } }, ''); expect(cacheKey).toBe('foo.com/bar?a=1&b'); }); it('should work well with no path', () => { const cacheKey = getCacheKey('https://foo.com?a=1', { rewrites: [], queryString: { type: 'all' } }, ''); expect(cacheKey).toBe('foo.com?a=1'); }); it('should work well with no path & query', () => { const cacheKey = getCacheKey('https://foo.com', { rewrites: [], queryString: { type: 'include', values: ['b', 'a'] } }, ''); expect(cacheKey).toBe('foo.com'); }); it('should work well with `?` in rewrites result', () => { const cacheKey = getCacheKey('https://foo.com/w/300/200/apple?a=1', { rewrites: [ { pattern: '/w/(\\d+)/(\\d+)/(.*)', repl: '/${3}?imageView/2/w/${1}' } ], queryString: { type: 'all' } }, ''); expect(cacheKey).toBe('foo.com/apple?imageView/2/w/300?a=1'); }); it('should work with special URL', () => { // query 值里有 `/`,hash 里有 `/` & `?` const cacheKey = getCacheKey('https://foo.com/bar?path=/a/b#/baz?quz', { rewrites: [ { pattern: '/bar', repl: '/new-bar' } ], queryString: { type: 'all' } }, ''); expect(cacheKey).toBe('foo.com/new-bar?path=/a/b'); }); }); describe('applyRewrites', () => { it('should work well with empty rewrites', () => { expect(applyRewrites([], '/foo?a=1')).toBe('/foo?a=1'); expect(applyRewrites([], '')).toBe(''); }); it('should work well with matched rewrite', () => { expect(applyRewrites([ { pattern: '(.*)\\d$', repl: '${1}' } ], '/123')).toBe('/12'); }); it('should only apply with the first matched rewrite', () => { expect(applyRewrites([ { pattern: '.', repl: '/a' }, { pattern: '.', repl: '/b' }, ], '/123')).toBe('/a'); expect(applyRewrites([ { pattern: '.', repl: '/b' }, { pattern: '.', repl: '/a' }, ], '/123')).toBe('/b'); }); it('should work well with non-matched capturing group', () => { expect(applyRewrites([{ pattern: '(f)|(g)', repl: '/${2}' }], '/f')).toBe('/'); }); it('should work well with non-present capturing group', () => { expect(applyRewrites([{ pattern: '(.*)', repl: '/${2}' }], '/f')).toBe('/${2}'); }); }); describe('applyQueryStringConfig', () => { it('should work well with type: all', () => { expect(applyQueryStringConfig({ type: 'all' }, '')).toBe(''); expect(applyQueryStringConfig({ type: 'all' }, 'foo')).toBe('foo'); expect(applyQueryStringConfig({ type: 'all' }, 'foo&bar=1')).toBe('foo&bar=1'); expect(applyQueryStringConfig({ type: 'all' }, 'foo&bar=1&foo&bar=2/123')).toBe('foo&bar=1&foo&bar=2/123'); }); it('should work well with type: none', () => { expect(applyQueryStringConfig({ type: 'none' }, '')).toBe(''); expect(applyQueryStringConfig({ type: 'none' }, 'foo')).toBe(''); expect(applyQueryStringConfig({ type: 'none' }, 'foo&bar=1')).toBe(''); expect(applyQueryStringConfig({ type: 'none' }, 'foo&bar=1&foo&bar=2/123')).toBe(''); }); it('should work well with type: include', () => { expect(applyQueryStringConfig({ type: 'include', values: [] }, '')).toBe(''); expect(applyQueryStringConfig({ type: 'include', values: ['a'] }, '')).toBe(''); expect(applyQueryStringConfig({ type: 'include', values: ['a'] }, 'a')).toBe('a'); expect(applyQueryStringConfig({ type: 'include', values: ['a'] }, 'a&b')).toBe('a'); expect(applyQueryStringConfig({ type: 'include', values: ['a'] }, 'a=1')).toBe('a=1'); expect(applyQueryStringConfig({ type: 'include', values: [] }, 'a=123&b=456')).toBe(''); expect(applyQueryStringConfig({ type: 'include', values: ['a', 'ttt'] }, 'a=123&b&c=2')).toBe('a=123'); expect(applyQueryStringConfig({ type: 'include', values: ['cc', 'ttt', 'a[]', '测试', '用途'] }, 'a[]=123&b&cc&d=2&测试&用途=无')).toBe('a%5B%5D=123&cc&%E6%B5%8B%E8%AF%95&%E7%94%A8%E9%80%94=%E6%97%A0'); }); it('should work well with type: exclude', () => { expect(applyQueryStringConfig({ type: 'exclude', values: [] }, '')).toBe(''); expect(applyQueryStringConfig({ type: 'exclude', values: ['a'] }, '')).toBe(''); expect(applyQueryStringConfig({ type: 'exclude', values: ['a'] }, 'a')).toBe(''); expect(applyQueryStringConfig({ type: 'exclude', values: ['a'] }, 'a&b')).toBe('b'); expect(applyQueryStringConfig({ type: 'exclude', values: ['a'] }, 'a=1')).toBe(''); expect(applyQueryStringConfig({ type: 'exclude', values: ['a'] }, 'a=123&b=456')).toBe('b=456'); expect(applyQueryStringConfig({ type: 'exclude', values: ['c'] }, 'a=123&b&c=2')).toBe('a=123&b'); expect(applyQueryStringConfig({ type: 'exclude', values: ['cc', 'ttt', 'a[]', '测试'] }, 'a[]=123&b&cc&d=2&测试&用途=无')).toBe('b&d=2&%E7%94%A8%E9%80%94=%E6%97%A0'); }); });