@@ -40,7 +40,33 @@ describe('router unit test', () => {
40
40
expect ( request . url ) . toBe ( '/' ) ;
41
41
} ) ;
42
42
it ( 'should return new target' , ( ) => {
43
- expect ( result ) . toBe ( 'http://foobar.com:666' ) ;
43
+ expect ( result ) . resolves . toBe ( 'http://foobar.com:666' ) ;
44
+ } ) ;
45
+ } ) ;
46
+ } ) ;
47
+
48
+ describe ( 'router.getTarget from async function' , ( ) => {
49
+ let request ;
50
+
51
+ beforeEach ( ( ) => {
52
+ proxyOptionWithRouter = {
53
+ target : 'http://localhost:6000' ,
54
+ async router ( req ) {
55
+ request = req ;
56
+ return 'http://foobar.com:666' ;
57
+ }
58
+ } ;
59
+
60
+ result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
61
+ } ) ;
62
+
63
+ describe ( 'custom dynamic router async function' , ( ) => {
64
+ it ( 'should provide the request object for dynamic routing' , ( ) => {
65
+ expect ( request . headers . host ) . toBe ( 'localhost' ) ;
66
+ expect ( request . url ) . toBe ( '/' ) ;
67
+ } ) ;
68
+ it ( 'should return new target' , ( ) => {
69
+ expect ( result ) . resolves . toBe ( 'http://foobar.com:666' ) ;
44
70
} ) ;
45
71
} ) ;
46
72
} ) ;
@@ -64,71 +90,71 @@ describe('router unit test', () => {
64
90
describe ( 'without router config' , ( ) => {
65
91
it ( 'should return the normal target when router not present in config' , ( ) => {
66
92
result = getTarget ( fakeReq , config ) ;
67
- expect ( result ) . toBeUndefined ( ) ;
93
+ expect ( result ) . resolves . toBeUndefined ( ) ;
68
94
} ) ;
69
95
} ) ;
70
96
71
97
describe ( 'with just the host in router config' , ( ) => {
72
98
it ( 'should target http://localhost:6001 when for router:"alpha.localhost"' , ( ) => {
73
99
fakeReq . headers . host = 'alpha.localhost' ;
74
100
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
75
- expect ( result ) . toBe ( 'http://localhost:6001' ) ;
101
+ expect ( result ) . resolves . toBe ( 'http://localhost:6001' ) ;
76
102
} ) ;
77
103
78
104
it ( 'should target http://localhost:6002 when for router:"beta.localhost"' , ( ) => {
79
105
fakeReq . headers . host = 'beta.localhost' ;
80
106
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
81
- expect ( result ) . toBe ( 'http://localhost:6002' ) ;
107
+ expect ( result ) . resolves . toBe ( 'http://localhost:6002' ) ;
82
108
} ) ;
83
109
} ) ;
84
110
85
111
describe ( 'with host and host + path config' , ( ) => {
86
112
it ( 'should target http://localhost:6004 without path' , ( ) => {
87
113
fakeReq . headers . host = 'gamma.localhost' ;
88
114
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
89
- expect ( result ) . toBe ( 'http://localhost:6004' ) ;
115
+ expect ( result ) . resolves . toBe ( 'http://localhost:6004' ) ;
90
116
} ) ;
91
117
92
118
it ( 'should target http://localhost:6003 exact path match' , ( ) => {
93
119
fakeReq . headers . host = 'gamma.localhost' ;
94
120
fakeReq . url = '/api' ;
95
121
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
96
- expect ( result ) . toBe ( 'http://localhost:6003' ) ;
122
+ expect ( result ) . resolves . toBe ( 'http://localhost:6003' ) ;
97
123
} ) ;
98
124
99
125
it ( 'should target http://localhost:6004 when contains path' , ( ) => {
100
126
fakeReq . headers . host = 'gamma.localhost' ;
101
127
fakeReq . url = '/api/books/123' ;
102
128
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
103
- expect ( result ) . toBe ( 'http://localhost:6003' ) ;
129
+ expect ( result ) . resolves . toBe ( 'http://localhost:6003' ) ;
104
130
} ) ;
105
131
} ) ;
106
132
107
133
describe ( 'with just the path' , ( ) => {
108
134
it ( 'should target http://localhost:6005 with just a path as router config' , ( ) => {
109
135
fakeReq . url = '/rest' ;
110
136
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
111
- expect ( result ) . toBe ( 'http://localhost:6005' ) ;
137
+ expect ( result ) . resolves . toBe ( 'http://localhost:6005' ) ;
112
138
} ) ;
113
139
114
140
it ( 'should target http://localhost:6005 with just a path as router config' , ( ) => {
115
141
fakeReq . url = '/rest/deep/path' ;
116
142
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
117
- expect ( result ) . toBe ( 'http://localhost:6005' ) ;
143
+ expect ( result ) . resolves . toBe ( 'http://localhost:6005' ) ;
118
144
} ) ;
119
145
120
146
it ( 'should target http://localhost:6000 path in not present in router config' , ( ) => {
121
147
fakeReq . url = '/unknow-path' ;
122
148
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
123
- expect ( result ) . toBeUndefined ( ) ;
149
+ expect ( result ) . resolves . toBeUndefined ( ) ;
124
150
} ) ;
125
151
} ) ;
126
152
127
153
describe ( 'matching order of router config' , ( ) => {
128
154
it ( 'should return first matching target when similar paths are configured' , ( ) => {
129
155
fakeReq . url = '/some/specific/path' ;
130
156
result = getTarget ( fakeReq , proxyOptionWithRouter ) ;
131
- expect ( result ) . toBe ( 'http://localhost:6006' ) ;
157
+ expect ( result ) . resolves . toBe ( 'http://localhost:6006' ) ;
132
158
} ) ;
133
159
} ) ;
134
160
} ) ;
0 commit comments