Skip to content

Commit

Permalink
🐛 修复match兼容问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Feb 13, 2022
1 parent dfa0231 commit d919e8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/pkg/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Match<T> {
return {
scheme: match[1],
host: match[2],
path: match[4] || '/',
path: match[4] || (url[url.length - 1] === '*' ? '*' : '/'),
search: match[5],
};
}
Expand All @@ -21,7 +21,7 @@ export class Match<T> {
return {
scheme: '*',
host: match[1],
path: match[3] || '/',
path: match[3] || (url[url.length - 1] === '*' ? '*' : '/'),
search: match[4],
};
}
Expand Down
18 changes: 11 additions & 7 deletions tests/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,26 @@ describe('UrlMatch', () => {


describe('UrlMatch2', () => {
const url = new UrlMatch<string>();
it('http*', () => {
const url = new UrlMatch<string>();
url.add('http*', 'ok1');
expect(url.match('http://www.http.com')).toEqual(['ok1']);
url.add('*://*', 'ok2');
expect(url.match('http://www.http.com')).toEqual(['ok1', 'ok2']);
expect(url.match('https://pan.baidu.com/disk/home?from=newversion&stayAtHome=true#/all?path=%2F&vmode=list')).toEqual(['ok1', 'ok2']);
});
it('port', () => {
const url = new UrlMatch<string>();
url.add('http://domain:8080', 'ok2');
expect(url.match('http://domain:8080/')).toEqual(['ok1', 'ok2']);
expect(url.match('http://domain:8080/123')).toEqual(['ok1']);
expect(url.match('http://domain:8080/')).toEqual(['ok2']);
expect(url.match('http://domain:8080/123')).toEqual([]);
});
it('无/', () => {
const url = new UrlMatch<string>();
url.add('http://domain2', 'ok3');
url.add('http://domain2*', 'ok4');
expect(url.match('http://domain2/')).toEqual(['ok1', 'ok3', 'ok4']);
expect(url.match('http://domain2.com/')).toEqual(['ok1', 'ok4']);
expect(url.match('http://domain2/123')).toEqual(['ok1']);
expect(url.match('http://domain2/')).toEqual(['ok3', 'ok4']);
expect(url.match('http://domain2.com/')).toEqual(['ok4']);
expect(url.match('http://domain2/123')).toEqual(['ok4']);
})
});

Expand Down

0 comments on commit d919e8a

Please sign in to comment.