Skip to content

Commit

Permalink
fix: RouterService match router method all (#2697)
Browse files Browse the repository at this point in the history
* fix: RouterService match router method all

* fix: RouterService match router method all

---------

Co-authored-by: 闄嗗嚡(钀ц抗) <lk195625@alibaba-inc.com>
  • Loading branch information
luckyscript and 闄嗗嚡(钀ц抗) committed Feb 2, 2023
1 parent 66e801d commit 801f534
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/service/webRouterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,10 @@ export class MidwayWebRouterService {
let matchedRouterInfo;
for (const item of routes) {
if (item.fullUrlCompiledRegexp) {
const itemRequestMethod = item['requestMethod'].toUpperCase();
if (
method.toUpperCase() === item['requestMethod'].toUpperCase() &&
('ALL' === itemRequestMethod ||
method.toUpperCase() === itemRequestMethod) &&
item.fullUrlCompiledRegexp.test(routerUrl)
) {
matchedRouterInfo = item;
Expand Down
13 changes: 12 additions & 1 deletion packages/core/test/service/webRouterService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,28 @@ describe('/test/service/webRouterService.test.ts', function () {
url: '/',
requestMethod: 'GET',
});
collector.addRouter(async (ctx) => {
return 'test all method';
} ,{
url: '/test/all/method',
requestMethod: 'ALL',
});
collector.addRouter(async (ctx) => {
return 'hello world';
}, {
url: '/abc/dddd/*',
requestMethod: 'GET',
});

let routeInfo = await collector.getMatchedRouterInfo('/api', 'get');
expect(routeInfo).toBeUndefined();

routeInfo = await collector.getMatchedRouterInfo('/abc/dddd/efg', 'GET');
expect(routeInfo.url).toEqual('/abc/dddd/*');
expect(routeInfo?.url).toEqual('/abc/dddd/*');

routeInfo = await collector.getMatchedRouterInfo('/test/all/method', 'POST');
expect(routeInfo?.url).toEqual('/test/all/method');
expect(routeInfo?.requestMethod.toUpperCase()).toEqual('ALL');

collector.addRouter(async (ctx) => {
return 'hello world';
Expand Down

0 comments on commit 801f534

Please sign in to comment.