@@ -22,22 +22,19 @@ describe("API Tests", function () {
22
22
proxy = newProxy ;
23
23
} )
24
24
. then ( function ( ) {
25
- r = ( options = { } ) => {
26
- const path = options . path || '' ;
27
- delete options . path ;
28
- return fetch ( {
25
+ r = ( path , options ) => {
26
+ options = options || { } ;
27
+ path = path || '' ;
28
+ const url = `${ options . url || apiUrl } ${ path } ` ;
29
+ delete options . url ;
30
+ const fetchOptions = {
29
31
method : "GET" ,
30
32
headers : {
31
33
Authorization : `token ${ proxy . authToken } ` ,
32
34
} ,
33
- url : `${ apiUrl } ${ path } ` ,
34
35
...options ,
35
- } ) . then ( ( res ) => {
36
- if ( ! res . ok ) {
37
- throw res ;
38
- }
39
- return res . text ( ) ; // return body
40
- } ) ;
36
+ } ;
37
+ return fetch ( url , fetchOptions ) ;
41
38
} ;
42
39
} )
43
40
. then ( function ( ) {
@@ -84,9 +81,7 @@ describe("API Tests", function () {
84
81
} ) ;
85
82
86
83
it ( "GET /api/routes fetches the routing table" , function ( done ) {
87
- r ( )
88
- . then ( function ( body ) {
89
- var reply = JSON . parse ( body ) ;
84
+ r ( ) . then ( res => res . json ( ) ) . then ( function ( reply ) {
90
85
var keys = Object . keys ( reply ) ;
91
86
expect ( keys . length ) . toEqual ( 1 ) ;
92
87
expect ( keys ) . toContain ( "/" ) ;
@@ -100,24 +95,22 @@ describe("API Tests", function () {
100
95
proxy
101
96
. addRoute ( path , { target : url } )
102
97
. then ( function ( ) {
103
- return r ( { path } ) ;
98
+ return r ( path ) ;
104
99
} )
105
- . then ( function ( body ) {
106
- var reply = JSON . parse ( body ) ;
100
+ . then ( res => res . json ( ) )
101
+ . then ( function ( reply ) {
107
102
var keys = Object . keys ( reply ) ;
108
103
expect ( keys ) . toContain ( "target" ) ;
109
104
expect ( reply . target ) . toEqual ( url ) ;
110
105
} )
106
+ . catch ( done . fail )
111
107
. then ( done ) ;
112
108
} ) ;
113
109
114
110
it ( "GET /api/routes[/path] fetches a single route (404 if missing)" , function ( done ) {
115
- r ( { path : "/path" } )
116
- . then ( ( body ) => {
117
- done . fail ( "Expected a 404" ) ;
118
- } )
119
- . catch ( ( error ) => {
120
- expect ( error . statusCode ) . toEqual ( 404 ) ;
111
+ r ( "/path" )
112
+ . then ( ( res ) => {
113
+ expect ( res . status ) . toEqual ( 404 ) ;
121
114
} )
122
115
. then ( done ) ;
123
116
} ) ;
@@ -126,11 +119,11 @@ describe("API Tests", function () {
126
119
var port = 8998 ;
127
120
var target = "http://127.0.0.1:" + port ;
128
121
129
- r ( {
122
+ r ( "/user/foo" , {
130
123
method : 'POST' ,
131
- path : "/user/foo" ,
132
124
body : JSON . stringify ( { target : target } ) ,
133
125
} )
126
+ . then ( res => res . text ( ) )
134
127
. then ( ( body ) => {
135
128
expect ( body ) . toEqual ( "" ) ;
136
129
} )
@@ -139,17 +132,18 @@ describe("API Tests", function () {
139
132
expect ( route . target ) . toEqual ( target ) ;
140
133
expect ( typeof route . last_activity ) . toEqual ( "object" ) ;
141
134
} )
135
+ . catch ( done . fail )
142
136
. then ( done ) ;
143
137
} ) ;
144
138
145
139
it ( "POST /api/routes[/foo%20bar] handles URI escapes" , function ( done ) {
146
140
var port = 8998 ;
147
141
var target = "http://127.0.0.1:" + port ;
148
- r ( {
142
+ r ( "/user/foo%40bar" , {
149
143
method : "POST" ,
150
- path : "/user/foo%40bar" ,
151
144
body : JSON . stringify ( { target : target } ) ,
152
145
} )
146
+ . then ( res => res . text ( ) )
153
147
. then ( ( body ) => {
154
148
expect ( body ) . toEqual ( "" ) ;
155
149
} )
@@ -168,10 +162,11 @@ describe("API Tests", function () {
168
162
it ( "POST /api/routes creates a new root route" , function ( done ) {
169
163
var port = 8998 ;
170
164
var target = "http://127.0.0.1:" + port ;
171
- r ( {
165
+ r ( '' , {
172
166
method : "POST" ,
173
167
body : JSON . stringify ( { target : target } ) ,
174
168
} )
169
+ . then ( res => res . text ( ) )
175
170
. then ( ( body ) => {
176
171
expect ( body ) . toEqual ( "" ) ;
177
172
return proxy . _routes . get ( "/" ) ;
@@ -192,17 +187,17 @@ describe("API Tests", function () {
192
187
. addTarget ( proxy , path , port , null , null )
193
188
. then ( ( ) => proxy . _routes . get ( path ) )
194
189
. then ( ( route ) => expect ( route . target ) . toEqual ( target ) )
195
- . then ( ( ) => r . del ( apiUrl + path ) )
190
+ . then ( ( ) => r ( path , { url : apiUrl , method : "DELETE" } ) )
191
+ . then ( res => res . text ( ) )
196
192
. then ( ( body ) => expect ( body ) . toEqual ( "" ) )
197
193
. then ( ( ) => proxy . _routes . get ( path ) )
198
194
. then ( ( deletedRoute ) => expect ( deletedRoute ) . toBe ( undefined ) )
199
195
. then ( done ) ;
200
196
} ) ;
201
197
202
198
it ( "GET /api/routes?inactiveSince= with bad value returns a 400" , function ( done ) {
203
- r ( { path : "?inactiveSince=endoftheuniverse" } )
204
- . then ( ( ) => done . fail ( "Expected 400" ) )
205
- . catch ( ( err ) => expect ( err . statusCode ) . toEqual ( 400 ) )
199
+ r ( "?inactiveSince=endoftheuniverse" )
200
+ . then ( ( res ) => expect ( res . status ) . toEqual ( 400 ) )
206
201
. then ( done ) ;
207
202
} ) ;
208
203
@@ -240,8 +235,7 @@ describe("API Tests", function () {
240
235
var seen = 0 ;
241
236
var doReq = function ( i ) {
242
237
var t = tests [ i ] ;
243
- return r ( { path : "?inactiveSince=" + t . since . toISOString ( ) } ) . then ( function ( body ) {
244
- var routes = JSON . parse ( body ) ;
238
+ return r ( "?inactiveSince=" + t . since . toISOString ( ) ) . then ( res => res . json ( ) ) . then ( function ( routes ) {
245
239
var routeKeys = Object . keys ( routes ) ;
246
240
var expectedKeys = Object . keys ( t . expected ) ;
247
241
0 commit comments