11"use strict" ;
2- var q = require ( "q" ) ;
3- var expect = require ( 'expect' ) ;
2+ const q = require ( "q" ) ;
3+ import expect from 'expect' ;
44
55/**
66 * Construct a mock HTTP backend, heavily inspired by Angular.js.
@@ -9,23 +9,25 @@ var expect = require('expect');
99function HttpBackend ( ) {
1010 this . requests = [ ] ;
1111 this . expectedRequests = [ ] ;
12- var self = this ;
12+ const self = this ;
1313 // the request function dependency that the SDK needs.
1414 this . requestFn = function ( opts , callback ) {
15- var realReq = new Request ( opts . method , opts . uri , opts . body , opts . qs ) ;
16- realReq . callback = callback ;
17- console . log ( "HTTP backend received request: %s %s" , opts . method , opts . uri ) ;
18- self . requests . push ( realReq ) ;
15+ const req = new Request ( opts , callback ) ;
16+ console . log ( "HTTP backend received request: " + req ) ;
17+ self . requests . push ( req ) ;
1918
20- var abort = function ( ) {
21- var idx = self . requests . indexOf ( realReq ) ;
19+ const abort = function ( ) {
20+ const idx = self . requests . indexOf ( req ) ;
2221 if ( idx >= 0 ) {
23- console . log ( "Aborting HTTP request: %s %s" , opts . method , opts . uri ) ;
22+ console . log ( "Aborting HTTP request: %s %s" , opts . method ,
23+ opts . uri ) ;
2424 self . requests . splice ( idx , 1 ) ;
25+ req . callback ( "aborted" ) ;
2526 }
26- }
27+ } ;
28+
2729 return {
28- abort : abort
30+ abort : abort ,
2931 } ;
3032 } ;
3133}
@@ -34,43 +36,61 @@ HttpBackend.prototype = {
3436 * Respond to all of the requests (flush the queue).
3537 * @param {string } path The path to flush (optional) default: all.
3638 * @param {integer } numToFlush The number of things to flush (optional), default: all.
37- * @return {Promise } resolved when there is nothing left to flush.
39+ * @param {integer= } waitTime The time (in ms) to wait for a request to happen.
40+ * default: 5
41+ *
42+ * @return {Promise } resolves when there is nothing left to flush, with the
43+ * number of requests flushed
3844 */
39- flush : function ( path , numToFlush ) {
40- var defer = q . defer ( ) ;
41- var self = this ;
42- var flushed = 0 ;
43- var triedWaiting = false ;
45+ flush : function ( path , numToFlush , waitTime ) {
46+ const defer = q . defer ( ) ;
47+ const self = this ;
48+ let flushed = 0 ;
49+ let triedWaiting = false ;
50+ if ( waitTime === undefined ) {
51+ waitTime = 5 ;
52+ }
4453 console . log (
45- "HTTP backend flushing... (path=%s numToFlush=%s)" , path , numToFlush
54+ "HTTP backend flushing... (path=" + path
55+ + " numToFlush=" + numToFlush
56+ + " waitTime=" + waitTime
57+ + ")" ,
4658 ) ;
47- var tryFlush = function ( ) {
59+ const tryFlush = function ( ) {
4860 // if there's more real requests and more expected requests, flush 'em.
4961 console . log (
50- " trying to flush queue => reqs=%s expected=%s [%s]" ,
51- self . requests . length , self . expectedRequests . length , path
62+ " trying to flush queue => reqs=[" + self . requests
63+ + "] expected=[" + self . expectedRequests
64+ + "]" ,
5265 ) ;
5366 if ( self . _takeFromQueue ( path ) ) {
5467 // try again on the next tick.
55- console . log ( " flushed. Trying for more. [%s]" , path ) ;
5668 flushed += 1 ;
5769 if ( numToFlush && flushed === numToFlush ) {
58- console . log ( " [%s] Flushed assigned amount: %s" , path , numToFlush ) ;
59- defer . resolve ( ) ;
60- }
61- else {
70+ console . log ( " Flushed assigned amount:" , numToFlush ) ;
71+ defer . resolve ( flushed ) ;
72+ } else {
73+ console . log ( " flushed. Trying for more." ) ;
6274 setTimeout ( tryFlush , 0 ) ;
6375 }
64- }
65- else if ( flushed === 0 && ! triedWaiting ) {
76+ } else if ( flushed === 0 && ! triedWaiting ) {
6677 // we may not have made the request yet, wait a generous amount of
6778 // time before giving up.
68- setTimeout ( tryFlush , 5 ) ;
79+ console . log (
80+ " nothing to flush yet; waiting " + waitTime +
81+ "ms for requests." )
82+ setTimeout ( tryFlush , waitTime ) ;
6983 triedWaiting = true ;
70- }
71- else {
72- console . log ( " no more flushes. [%s]" , path ) ;
73- defer . resolve ( ) ;
84+ } else {
85+ if ( flushed === 0 ) {
86+ console . log ( " nothing to flush; giving up" ) ;
87+ } else {
88+ console . log (
89+ " no more flushes after flushing" , flushed ,
90+ "requests" ,
91+ ) ;
92+ }
93+ defer . resolve ( flushed ) ;
7494 }
7595 } ;
7696
@@ -85,14 +105,19 @@ HttpBackend.prototype = {
85105 * @return {boolean } true if something was resolved.
86106 */
87107 _takeFromQueue : function ( path ) {
88- var req = null ;
89- var i , j ;
90- var matchingReq , expectedReq , testResponse = null ;
108+ let req = null ;
109+ let i ;
110+ let j ;
111+ let matchingReq = null ;
112+ let expectedReq = null ;
113+ let testResponse = null ;
91114 for ( i = 0 ; i < this . requests . length ; i ++ ) {
92115 req = this . requests [ i ] ;
93116 for ( j = 0 ; j < this . expectedRequests . length ; j ++ ) {
94117 expectedReq = this . expectedRequests [ j ] ;
95- if ( path && path !== expectedReq . path ) { continue ; }
118+ if ( path && path !== expectedReq . path ) {
119+ continue ;
120+ }
96121 if ( expectedReq . method === req . method &&
97122 req . path . indexOf ( expectedReq . path ) !== - 1 ) {
98123 if ( ! expectedReq . data || ( JSON . stringify ( expectedReq . data ) ===
@@ -114,12 +139,12 @@ HttpBackend.prototype = {
114139 }
115140 testResponse = matchingReq . response ;
116141 console . log ( " responding to %s" , matchingReq . path ) ;
117- var body = testResponse . body ;
142+ let body = testResponse . body ;
118143 if ( Object . prototype . toString . call ( body ) == "[object Function]" ) {
119144 body = body ( req . path , req . data ) ;
120145 }
121146 req . callback (
122- testResponse . err , testResponse . response , body
147+ testResponse . err , testResponse . response , body ,
123148 ) ;
124149 matchingReq = null ;
125150 }
@@ -134,23 +159,20 @@ HttpBackend.prototype = {
134159 * Makes sure that the SDK hasn't sent any more requests to the backend.
135160 */
136161 verifyNoOutstandingRequests : function ( ) {
137- var firstOutstandingReq = this . requests [ 0 ] || { } ;
162+ const firstOutstandingReq = this . requests [ 0 ] || { } ;
138163 expect ( this . requests . length ) . toEqual ( 0 ,
139164 "Expected no more HTTP requests but received request to " +
140- firstOutstandingReq . path
165+ firstOutstandingReq . path ,
141166 ) ;
142167 } ,
143168
144169 /**
145170 * Makes sure that the test doesn't have any unresolved requests.
146171 */
147172 verifyNoOutstandingExpectation : function ( ) {
148- var firstOutstandingExpectation = this . expectedRequests [ 0 ] || { } ;
149- expect ( this . expectedRequests . length ) . toEqual (
150- 0 ,
151- "Expected to see HTTP request for "
152- + firstOutstandingExpectation . method
153- + " " + firstOutstandingExpectation . path
173+ const firstOutstandingExpectation = this . expectedRequests [ 0 ] || { } ;
174+ expect ( this . expectedRequests . length ) . toEqual ( 0 ,
175+ "Expected to see HTTP request for " + firstOutstandingExpectation . path ,
154176 ) ;
155177 } ,
156178
@@ -162,22 +184,36 @@ HttpBackend.prototype = {
162184 * @return {Request } An expected request.
163185 */
164186 when : function ( method , path , data ) {
165- var pendingReq = new Request ( method , path , data ) ;
187+ const pendingReq = new ExpectedRequest ( method , path , data ) ;
166188 this . expectedRequests . push ( pendingReq ) ;
167189 return pendingReq ;
168- }
190+ } ,
169191} ;
170192
171- function Request ( method , path , data , queryParams ) {
193+ /**
194+ * Represents the expectation of a request.
195+ *
196+ * <p>Includes the conditions to be matched against, the checks to be made,
197+ * and the response to be returned.
198+ *
199+ * @constructor
200+ * @param {string } method
201+ * @param {string } path
202+ * @param {object? } data
203+ */
204+ function ExpectedRequest ( method , path , data ) {
172205 this . method = method ;
173206 this . path = path ;
174207 this . data = data ;
175- this . queryParams = queryParams ;
176- this . callback = null ;
177208 this . response = null ;
178209 this . checks = [ ] ;
179210}
180- Request . prototype = {
211+
212+ ExpectedRequest . prototype = {
213+ toString : function ( ) {
214+ return this . method + " " + this . path
215+ } ,
216+
181217 /**
182218 * Execute a check when this request has been satisfied.
183219 * @param {Function } fn The function to execute.
@@ -198,10 +234,10 @@ Request.prototype = {
198234 this . response = {
199235 response : {
200236 statusCode : code ,
201- headers : { }
237+ headers : { } ,
202238 } ,
203239 body : data ,
204- err : null
240+ err : null ,
205241 } ;
206242 } ,
207243
@@ -214,14 +250,62 @@ Request.prototype = {
214250 this . response = {
215251 response : {
216252 statusCode : code ,
217- headers : { }
253+ headers : { } ,
218254 } ,
219255 body : null ,
220- err : err
256+ err : err ,
221257 } ;
222258 } ,
223259} ;
224260
261+ /**
262+ * Represents a request made by the app.
263+ *
264+ * @constructor
265+ * @param {object } opts opts passed to request()
266+ * @param {function } callback
267+ */
268+ function Request ( opts , callback ) {
269+ this . opts = opts ;
270+ this . callback = callback ;
271+
272+ Object . defineProperty ( this , 'method' , {
273+ get : function ( ) {
274+ return opts . method ;
275+ } ,
276+ } ) ;
277+
278+ Object . defineProperty ( this , 'path' , {
279+ get : function ( ) {
280+ return opts . uri ;
281+ } ,
282+ } ) ;
283+
284+ Object . defineProperty ( this , 'data' , {
285+ get : function ( ) {
286+ return opts . body ;
287+ } ,
288+ } ) ;
289+
290+ Object . defineProperty ( this , 'queryParams' , {
291+ get : function ( ) {
292+ return opts . qs ;
293+ } ,
294+ } ) ;
295+
296+ Object . defineProperty ( this , 'headers' , {
297+ get : function ( ) {
298+ return opts . headers || { } ;
299+ } ,
300+ } ) ;
301+ }
302+
303+ Request . prototype = {
304+ toString : function ( ) {
305+ return this . method + " " + this . path ;
306+ } ,
307+ } ;
308+
225309/**
226310 * The HttpBackend class.
227311 */
0 commit comments