3
3
4
4
var util = require ( "../lib/testutil" ) ;
5
5
var extend = require ( "util" ) . _extend ;
6
- var request = require ( "request-promise-native" ) ;
7
6
var log = require ( "winston" ) ;
8
7
// disable logging during tests
9
8
log . remove ( log . transports . Console ) ;
@@ -23,12 +22,23 @@ describe("API Tests", function () {
23
22
proxy = newProxy ;
24
23
} )
25
24
. then ( function ( ) {
26
- r = request . defaults ( {
27
- method : "GET" ,
28
- headers : { Authorization : "token " + proxy . authToken } ,
29
- port : apiPort ,
30
- url : apiUrl ,
31
- } ) ;
25
+ r = ( options = { } ) => {
26
+ const path = options . path || '' ;
27
+ delete options . path ;
28
+ return fetch ( {
29
+ method : "GET" ,
30
+ headers : {
31
+ Authorization : `token ${ proxy . authToken } ` ,
32
+ } ,
33
+ url : `${ apiUrl } ${ path } ` ,
34
+ ...options ,
35
+ } ) . then ( ( res ) => {
36
+ if ( ! res . ok ) {
37
+ throw res ;
38
+ }
39
+ return res . text ( ) ; // return body
40
+ } ) ;
41
+ } ;
32
42
} )
33
43
. then ( function ( ) {
34
44
callback ( ) ;
@@ -74,7 +84,7 @@ describe("API Tests", function () {
74
84
} ) ;
75
85
76
86
it ( "GET /api/routes fetches the routing table" , function ( done ) {
77
- r ( apiUrl )
87
+ r ( )
78
88
. then ( function ( body ) {
79
89
var reply = JSON . parse ( body ) ;
80
90
var keys = Object . keys ( reply ) ;
@@ -90,7 +100,7 @@ describe("API Tests", function () {
90
100
proxy
91
101
. addRoute ( path , { target : url } )
92
102
. then ( function ( ) {
93
- return r ( apiUrl + path ) ;
103
+ return r ( { path } ) ;
94
104
} )
95
105
. then ( function ( body ) {
96
106
var reply = JSON . parse ( body ) ;
@@ -102,7 +112,7 @@ describe("API Tests", function () {
102
112
} ) ;
103
113
104
114
it ( "GET /api/routes[/path] fetches a single route (404 if missing)" , function ( done ) {
105
- r ( apiUrl + "/path" )
115
+ r ( { path : "/path" } )
106
116
. then ( ( body ) => {
107
117
done . fail ( "Expected a 404" ) ;
108
118
} )
@@ -116,8 +126,9 @@ describe("API Tests", function () {
116
126
var port = 8998 ;
117
127
var target = "http://127.0.0.1:" + port ;
118
128
119
- r . post ( {
120
- url : apiUrl + "/user/foo" ,
129
+ r ( {
130
+ method : 'POST' ,
131
+ path : "/user/foo" ,
121
132
body : JSON . stringify ( { target : target } ) ,
122
133
} )
123
134
. then ( ( body ) => {
@@ -134,8 +145,9 @@ describe("API Tests", function () {
134
145
it ( "POST /api/routes[/foo%20bar] handles URI escapes" , function ( done ) {
135
146
var port = 8998 ;
136
147
var target = "http://127.0.0.1:" + port ;
137
- r . post ( {
138
- url : apiUrl + "/user/foo%40bar" ,
148
+ r ( {
149
+ method : "POST" ,
150
+ path : "/user/foo%40bar" ,
139
151
body : JSON . stringify ( { target : target } ) ,
140
152
} )
141
153
. then ( ( body ) => {
@@ -156,8 +168,8 @@ describe("API Tests", function () {
156
168
it ( "POST /api/routes creates a new root route" , function ( done ) {
157
169
var port = 8998 ;
158
170
var target = "http://127.0.0.1:" + port ;
159
- r . post ( {
160
- url : apiUrl ,
171
+ r ( {
172
+ method : "POST" ,
161
173
body : JSON . stringify ( { target : target } ) ,
162
174
} )
163
175
. then ( ( body ) => {
@@ -188,7 +200,7 @@ describe("API Tests", function () {
188
200
} ) ;
189
201
190
202
it ( "GET /api/routes?inactiveSince= with bad value returns a 400" , function ( done ) {
191
- r . get ( apiUrl + "?inactiveSince=endoftheuniverse" )
203
+ r ( { path : "?inactiveSince=endoftheuniverse" } )
192
204
. then ( ( ) => done . fail ( "Expected 400" ) )
193
205
. catch ( ( err ) => expect ( err . statusCode ) . toEqual ( 400 ) )
194
206
. then ( done ) ;
@@ -228,7 +240,7 @@ describe("API Tests", function () {
228
240
var seen = 0 ;
229
241
var doReq = function ( i ) {
230
242
var t = tests [ i ] ;
231
- return r . get ( apiUrl + "?inactiveSince=" + t . since . toISOString ( ) ) . then ( function ( body ) {
243
+ return r ( { path : "?inactiveSince=" + t . since . toISOString ( ) } ) . then ( function ( body ) {
232
244
var routes = JSON . parse ( body ) ;
233
245
var routeKeys = Object . keys ( routes ) ;
234
246
var expectedKeys = Object . keys ( t . expected ) ;
0 commit comments