3
3
// This file is licensed under the MIT License.
4
4
// License text available at https://opensource.org/licenses/MIT
5
5
6
- import { expect } from '@loopback/testlab' ;
7
- import { TrieRouter , RouteEntry } from '../../..' ;
6
+ import {
7
+ expect ,
8
+ ShotRequestOptions ,
9
+ stubExpressContext ,
10
+ } from '@loopback/testlab' ;
11
+ import { TrieRouter , RouteEntry , Request } from '../../..' ;
8
12
import { anOperationSpec } from '@loopback/openapi-spec-builder' ;
13
+ import { ResolvedRoute } from '../../../src' ;
9
14
10
15
describe ( 'trie router' , ( ) => {
11
16
class TestTrieRouter extends TrieRouter {
@@ -14,10 +19,15 @@ describe('trie router', () => {
14
19
}
15
20
}
16
21
17
- const getVerbAndPath = ( r : RouteEntry ) => ( { verb : r . verb , path : r . path } ) ;
22
+ let router : TestTrieRouter ;
23
+ beforeEach ( givenTrieRouter ) ;
24
+
25
+ const getVerbAndPath = ( r ?: RouteEntry ) => ( {
26
+ verb : r && r . verb ,
27
+ path : r && r . path ,
28
+ } ) ;
18
29
19
30
it ( 'adds routes to routesWithoutPathVars' , ( ) => {
20
- const router = givenTrieRouter ( ) ;
21
31
const staticRoutes = router . staticRoutes . map ( getVerbAndPath ) ;
22
32
23
33
for ( const r of [
@@ -36,30 +46,76 @@ describe('trie router', () => {
36
46
{ verb : 'get' , path : '/orders/{id}/exists' } ,
37
47
{ verb : 'get' , path : '/orders/{id}' } ,
38
48
{ verb : 'delete' , path : '/orders/{id}' } ,
49
+ { verb : 'get' , path : '/users/{id}' } ,
50
+ { verb : 'get' , path : '/users/{userId}/orders' } ,
51
+ { verb : 'get' , path : '/users/{id}/products' } ,
39
52
] ) {
40
53
expect ( staticRoutes ) . to . not . containEql ( r ) ;
41
54
}
42
55
} ) ;
43
56
44
- it ( 'list routes by order' , ( ) => {
45
- const router = givenTrieRouter ( ) ;
46
-
57
+ it ( 'lists routes by order' , ( ) => {
47
58
expect ( router . list ( ) . map ( getVerbAndPath ) ) . to . eql ( [
48
59
{ verb : 'post' , path : '/orders' } ,
49
60
{ verb : 'put' , path : '/orders/{id}' } ,
50
61
{ verb : 'patch' , path : '/orders/{id}' } ,
51
62
{ verb : 'patch' , path : '/orders' } ,
52
63
{ verb : 'get' , path : '/orders/{id}/exists' } ,
64
+ { verb : 'get' , path : '/users/{userId}/orders' } ,
65
+ { verb : 'get' , path : '/users/{id}/products' } ,
53
66
{ verb : 'get' , path : '/orders/count' } ,
54
67
{ verb : 'get' , path : '/orders/{id}' } ,
68
+ { verb : 'get' , path : '/users/{id}' } ,
55
69
{ verb : 'get' , path : '/orders' } ,
56
70
{ verb : 'delete' , path : '/orders/{id}' } ,
57
71
{ verb : 'delete' , path : '/orders' } ,
58
72
] ) ;
59
73
} ) ;
60
74
75
+ it ( 'finds route for GET /users/{id}' , ( ) => {
76
+ const req = givenRequest ( { method : 'get' , url : '/users/123' } ) ;
77
+ const route = router . find ( req ) ;
78
+ expect ( getRouteInfo ( route ) ) . to . containEql ( {
79
+ verb : 'get' ,
80
+ path : '/users/{id}' ,
81
+ params : { id : '123' } ,
82
+ } ) ;
83
+ } ) ;
84
+
85
+ it ( 'finds route for GET /users/{id}/products' , ( ) => {
86
+ const req = givenRequest ( { method : 'get' , url : '/users/123/products' } ) ;
87
+ const route = router . find ( req ) ;
88
+ expect ( getRouteInfo ( route ) ) . to . containEql ( {
89
+ verb : 'get' ,
90
+ path : '/users/{id}/products' ,
91
+ params : { id : '123' } ,
92
+ } ) ;
93
+ } ) ;
94
+
95
+ it ( 'finds route for GET /users/{userId}/orders' , ( ) => {
96
+ const req = givenRequest ( { method : 'get' , url : '/users/123/orders' } ) ;
97
+ const route = router . find ( req ) ;
98
+ expect ( getRouteInfo ( route ) ) . to . containEql ( {
99
+ verb : 'get' ,
100
+ path : '/users/{userId}/orders' ,
101
+ params : { userId : '123' } ,
102
+ } ) ;
103
+ } ) ;
104
+
105
+ function getRouteInfo ( r ?: ResolvedRoute ) {
106
+ return {
107
+ verb : r && r . verb ,
108
+ path : r && r . path ,
109
+ params : r && r . pathParams ,
110
+ } ;
111
+ }
112
+
113
+ function givenRequest ( options ?: ShotRequestOptions ) : Request {
114
+ return stubExpressContext ( options ) . request ;
115
+ }
116
+
61
117
function givenTrieRouter ( ) {
62
- const router = new TestTrieRouter ( ) ;
118
+ router = new TestTrieRouter ( ) ;
63
119
for ( const r of givenRoutes ( ) ) {
64
120
router . add ( r ) ;
65
121
}
@@ -92,6 +148,10 @@ describe('trie router', () => {
92
148
addRoute ( 'deleteAll' , 'delete' , '/orders' ) ;
93
149
addRoute ( 'updateAll' , 'patch' , '/orders' ) ;
94
150
151
+ addRoute ( 'getUserById' , 'get' , '/users/{id}' ) ;
152
+ addRoute ( 'getUserOrders' , 'get' , '/users/{userId}/orders' ) ;
153
+ addRoute ( 'getUserRecommendation' , 'get' , '/users/{id}/products' ) ;
154
+
95
155
return routes ;
96
156
}
97
157
} ) ;
0 commit comments