-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from cdimascio/paths-2
improve route sorting to match openapi spec
- Loading branch information
Showing
2 changed files
with
54 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
sortRoutes, | ||
} from '../src/framework/openapi.spec.loader'; | ||
import { expect } from 'chai'; | ||
|
||
describe('url sorter', () => { | ||
it('should sort dynamic leafs after static leafs', async () => { | ||
const urls = asRouteMetadatas(['/my/:id', '/my/path']); | ||
const expected = ['/my/path', '/my/:id']; | ||
|
||
urls.sort(sortRoutes); | ||
|
||
expect(urls[0].expressRoute).to.equal(expected[0]); | ||
expect(urls[1].expressRoute).to.equal(expected[1]); | ||
}); | ||
|
||
it('should sort dynamic inner paths after static inner paths', async () => { | ||
const urls = asRouteMetadatas([ | ||
'/my/:id/test', | ||
'/my/path/test', | ||
'/a/:b/c/:d', | ||
'/a/:b/c/d', | ||
]); | ||
const expected = [ | ||
'/a/:b/c/d', | ||
'/a/:b/c/:d', | ||
'/my/path/test', | ||
'/my/:id/test', | ||
]; | ||
|
||
urls.sort(sortRoutes); | ||
|
||
expect(urls[0].expressRoute).to.equal(expected[0]); | ||
expect(urls[1].expressRoute).to.equal(expected[1]); | ||
expect(urls[2].expressRoute).to.equal(expected[2]); | ||
expect(urls[3].expressRoute).to.equal(expected[3]); | ||
}); | ||
}); | ||
|
||
function asRouteMetadatas(urls: string[]) { | ||
return urls.map(u => ({ | ||
expressRoute: u, | ||
})); | ||
} |