2016-04-13 09:15:04 +00:00
|
|
|
"use strict";
|
2017-05-18 11:18:57 +00:00
|
|
|
const q = require("q");
|
|
|
|
import expect from 'expect';
|
2016-04-13 09:15:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a mock HTTP backend, heavily inspired by Angular.js.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function HttpBackend() {
|
|
|
|
this.requests = [];
|
|
|
|
this.expectedRequests = [];
|
2017-05-18 11:18:57 +00:00
|
|
|
const self = this;
|
2016-04-13 09:15:04 +00:00
|
|
|
// the request function dependency that the SDK needs.
|
|
|
|
this.requestFn = function(opts, callback) {
|
2017-05-18 11:18:57 +00:00
|
|
|
const req = new Request(opts, callback);
|
2017-06-14 15:06:39 +00:00
|
|
|
console.log(`${Date.now()} HTTP backend received request: ${req}`);
|
2017-05-18 11:18:57 +00:00
|
|
|
self.requests.push(req);
|
2016-04-13 09:15:04 +00:00
|
|
|
|
2017-05-18 11:18:57 +00:00
|
|
|
const abort = function() {
|
|
|
|
const idx = self.requests.indexOf(req);
|
2016-04-13 09:15:04 +00:00
|
|
|
if (idx >= 0) {
|
2017-05-18 11:18:57 +00:00
|
|
|
console.log("Aborting HTTP request: %s %s", opts.method,
|
|
|
|
opts.uri);
|
2016-04-13 09:15:04 +00:00
|
|
|
self.requests.splice(idx, 1);
|
2017-05-18 11:18:57 +00:00
|
|
|
req.callback("aborted");
|
2016-04-13 09:15:04 +00:00
|
|
|
}
|
2017-05-18 11:18:57 +00:00
|
|
|
};
|
|
|
|
|
2016-04-13 09:15:04 +00:00
|
|
|
return {
|
2017-05-18 11:18:57 +00:00
|
|
|
abort: abort,
|
2016-04-13 09:15:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
HttpBackend.prototype = {
|
|
|
|
/**
|
|
|
|
* Respond to all of the requests (flush the queue).
|
|
|
|
* @param {string} path The path to flush (optional) default: all.
|
|
|
|
* @param {integer} numToFlush The number of things to flush (optional), default: all.
|
2017-05-18 11:18:57 +00:00
|
|
|
* @param {integer=} waitTime The time (in ms) to wait for a request to happen.
|
|
|
|
* default: 5
|
|
|
|
*
|
|
|
|
* @return {Promise} resolves when there is nothing left to flush, with the
|
|
|
|
* number of requests flushed
|
2016-04-13 09:15:04 +00:00
|
|
|
*/
|
2017-05-18 11:18:57 +00:00
|
|
|
flush: function(path, numToFlush, waitTime) {
|
|
|
|
const defer = q.defer();
|
|
|
|
const self = this;
|
|
|
|
let flushed = 0;
|
|
|
|
let triedWaiting = false;
|
|
|
|
if (waitTime === undefined) {
|
|
|
|
waitTime = 5;
|
|
|
|
}
|
2017-06-14 15:06:39 +00:00
|
|
|
|
|
|
|
function log(msg) {
|
|
|
|
console.log(`${Date.now()} flush[${path || ''}]: ${msg}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
log("HTTP backend flushing... (path=" + path
|
2017-05-18 12:37:23 +00:00
|
|
|
+ " numToFlush=" + numToFlush
|
|
|
|
+ " waitTime=" + waitTime
|
|
|
|
+ ")",
|
2016-04-13 09:15:04 +00:00
|
|
|
);
|
2017-05-18 11:18:57 +00:00
|
|
|
const tryFlush = function() {
|
2016-04-13 09:15:04 +00:00
|
|
|
// if there's more real requests and more expected requests, flush 'em.
|
2017-06-14 15:06:39 +00:00
|
|
|
log(` trying to flush => reqs=[${self.requests}] ` +
|
|
|
|
`expected=[${self.expectedRequests}]`,
|
2016-04-13 09:15:04 +00:00
|
|
|
);
|
|
|
|
if (self._takeFromQueue(path)) {
|
|
|
|
// try again on the next tick.
|
|
|
|
flushed += 1;
|
|
|
|
if (numToFlush && flushed === numToFlush) {
|
2017-06-14 15:06:39 +00:00
|
|
|
log(`Flushed assigned amount: ${numToFlush}`);
|
2017-05-18 11:18:57 +00:00
|
|
|
defer.resolve(flushed);
|
|
|
|
} else {
|
2017-06-14 15:06:39 +00:00
|
|
|
log(` flushed. Trying for more.`);
|
2016-04-13 09:15:04 +00:00
|
|
|
setTimeout(tryFlush, 0);
|
|
|
|
}
|
2017-05-18 11:18:57 +00:00
|
|
|
} else if (flushed === 0 && !triedWaiting) {
|
2016-04-13 09:15:04 +00:00
|
|
|
// we may not have made the request yet, wait a generous amount of
|
|
|
|
// time before giving up.
|
2017-06-14 15:06:39 +00:00
|
|
|
log(` nothing to flush yet; waiting for requests.`);
|
2017-05-18 11:18:57 +00:00
|
|
|
setTimeout(tryFlush, waitTime);
|
2016-04-13 09:15:04 +00:00
|
|
|
triedWaiting = true;
|
2017-05-18 11:18:57 +00:00
|
|
|
} else {
|
2017-05-18 12:37:23 +00:00
|
|
|
if (flushed === 0) {
|
2017-06-14 15:06:39 +00:00
|
|
|
log("nothing to flush; giving up");
|
2017-05-18 12:37:23 +00:00
|
|
|
} else {
|
2017-06-14 15:06:39 +00:00
|
|
|
log(`no more flushes after flushing ${flushed} requests`);
|
2017-05-18 12:37:23 +00:00
|
|
|
}
|
2017-05-18 11:18:57 +00:00
|
|
|
defer.resolve(flushed);
|
2016-04-13 09:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
setTimeout(tryFlush, 0);
|
|
|
|
|
|
|
|
return defer.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to resolve requests/expected requests.
|
|
|
|
* @param {string} path The path to flush (optional) default: all.
|
|
|
|
* @return {boolean} true if something was resolved.
|
|
|
|
*/
|
|
|
|
_takeFromQueue: function(path) {
|
2017-05-18 11:18:57 +00:00
|
|
|
let req = null;
|
|
|
|
let i;
|
|
|
|
let j;
|
|
|
|
let matchingReq = null;
|
|
|
|
let expectedReq = null;
|
|
|
|
let testResponse = null;
|
2016-04-13 09:15:04 +00:00
|
|
|
for (i = 0; i < this.requests.length; i++) {
|
|
|
|
req = this.requests[i];
|
|
|
|
for (j = 0; j < this.expectedRequests.length; j++) {
|
|
|
|
expectedReq = this.expectedRequests[j];
|
2017-05-18 11:18:57 +00:00
|
|
|
if (path && path !== expectedReq.path) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-13 09:15:04 +00:00
|
|
|
if (expectedReq.method === req.method &&
|
|
|
|
req.path.indexOf(expectedReq.path) !== -1) {
|
|
|
|
if (!expectedReq.data || (JSON.stringify(expectedReq.data) ===
|
|
|
|
JSON.stringify(req.data))) {
|
|
|
|
matchingReq = expectedReq;
|
|
|
|
this.expectedRequests.splice(j, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchingReq) {
|
|
|
|
// remove from request queue
|
|
|
|
this.requests.splice(i, 1);
|
|
|
|
i--;
|
|
|
|
|
|
|
|
for (j = 0; j < matchingReq.checks.length; j++) {
|
|
|
|
matchingReq.checks[j](req);
|
|
|
|
}
|
|
|
|
testResponse = matchingReq.response;
|
2017-06-14 15:06:39 +00:00
|
|
|
console.log(`${Date.now()} responding to ${matchingReq.path}`);
|
2017-05-18 11:18:57 +00:00
|
|
|
let body = testResponse.body;
|
2016-04-13 09:15:04 +00:00
|
|
|
if (Object.prototype.toString.call(body) == "[object Function]") {
|
|
|
|
body = body(req.path, req.data);
|
|
|
|
}
|
|
|
|
req.callback(
|
2017-05-18 11:18:57 +00:00
|
|
|
testResponse.err, testResponse.response, body,
|
2016-04-13 09:15:04 +00:00
|
|
|
);
|
|
|
|
matchingReq = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (testResponse) { // flushed something
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure that the SDK hasn't sent any more requests to the backend.
|
|
|
|
*/
|
|
|
|
verifyNoOutstandingRequests: function() {
|
2017-05-18 11:18:57 +00:00
|
|
|
const firstOutstandingReq = this.requests[0] || {};
|
2016-04-13 09:15:04 +00:00
|
|
|
expect(this.requests.length).toEqual(0,
|
|
|
|
"Expected no more HTTP requests but received request to " +
|
2017-05-18 11:18:57 +00:00
|
|
|
firstOutstandingReq.path,
|
2016-04-13 09:15:04 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure that the test doesn't have any unresolved requests.
|
|
|
|
*/
|
|
|
|
verifyNoOutstandingExpectation: function() {
|
2017-05-18 11:18:57 +00:00
|
|
|
const firstOutstandingExpectation = this.expectedRequests[0] || {};
|
|
|
|
expect(this.expectedRequests.length).toEqual(0,
|
|
|
|
"Expected to see HTTP request for " + firstOutstandingExpectation.path,
|
2016-04-13 09:15:04 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an expected request.
|
|
|
|
* @param {string} method The HTTP method
|
|
|
|
* @param {string} path The path (which can be partial)
|
|
|
|
* @param {Object} data The expected data.
|
|
|
|
* @return {Request} An expected request.
|
|
|
|
*/
|
|
|
|
when: function(method, path, data) {
|
2017-05-18 11:18:57 +00:00
|
|
|
const pendingReq = new ExpectedRequest(method, path, data);
|
2016-04-13 09:15:04 +00:00
|
|
|
this.expectedRequests.push(pendingReq);
|
|
|
|
return pendingReq;
|
2017-05-18 11:18:57 +00:00
|
|
|
},
|
2016-04-13 09:15:04 +00:00
|
|
|
};
|
|
|
|
|
2017-05-18 11:18:57 +00:00
|
|
|
/**
|
|
|
|
* Represents the expectation of a request.
|
|
|
|
*
|
|
|
|
* <p>Includes the conditions to be matched against, the checks to be made,
|
|
|
|
* and the response to be returned.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} path
|
|
|
|
* @param {object?} data
|
|
|
|
*/
|
|
|
|
function ExpectedRequest(method, path, data) {
|
2016-04-13 09:15:04 +00:00
|
|
|
this.method = method;
|
|
|
|
this.path = path;
|
|
|
|
this.data = data;
|
|
|
|
this.response = null;
|
|
|
|
this.checks = [];
|
|
|
|
}
|
2017-05-18 11:18:57 +00:00
|
|
|
|
|
|
|
ExpectedRequest.prototype = {
|
2017-05-18 12:37:23 +00:00
|
|
|
toString: function() {
|
|
|
|
return this.method + " " + this.path
|
|
|
|
},
|
|
|
|
|
2016-04-13 09:15:04 +00:00
|
|
|
/**
|
|
|
|
* Execute a check when this request has been satisfied.
|
|
|
|
* @param {Function} fn The function to execute.
|
|
|
|
* @return {Request} for chaining calls.
|
|
|
|
*/
|
|
|
|
check: function(fn) {
|
|
|
|
this.checks.push(fn);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond with the given data when this request is satisfied.
|
|
|
|
* @param {Number} code The HTTP status code.
|
|
|
|
* @param {Object|Function} data The HTTP JSON body. If this is a function,
|
|
|
|
* it will be invoked when the JSON body is required (which should be returned).
|
|
|
|
*/
|
|
|
|
respond: function(code, data) {
|
|
|
|
this.response = {
|
|
|
|
response: {
|
|
|
|
statusCode: code,
|
2017-05-18 11:18:57 +00:00
|
|
|
headers: {},
|
2016-04-13 09:15:04 +00:00
|
|
|
},
|
|
|
|
body: data,
|
2017-05-18 11:18:57 +00:00
|
|
|
err: null,
|
2016-04-13 09:15:04 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fail with an Error when this request is satisfied.
|
|
|
|
* @param {Number} code The HTTP status code.
|
|
|
|
* @param {Error} err The error to throw (e.g. Network Error)
|
|
|
|
*/
|
|
|
|
fail: function(code, err) {
|
|
|
|
this.response = {
|
|
|
|
response: {
|
|
|
|
statusCode: code,
|
2017-05-18 11:18:57 +00:00
|
|
|
headers: {},
|
2016-04-13 09:15:04 +00:00
|
|
|
},
|
|
|
|
body: null,
|
2017-05-18 11:18:57 +00:00
|
|
|
err: err,
|
2016-04-13 09:15:04 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-05-18 11:18:57 +00:00
|
|
|
/**
|
|
|
|
* Represents a request made by the app.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {object} opts opts passed to request()
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
function Request(opts, callback) {
|
|
|
|
this.opts = opts;
|
|
|
|
this.callback = callback;
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'method', {
|
|
|
|
get: function() {
|
|
|
|
return opts.method;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'path', {
|
|
|
|
get: function() {
|
|
|
|
return opts.uri;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'data', {
|
|
|
|
get: function() {
|
|
|
|
return opts.body;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'queryParams', {
|
|
|
|
get: function() {
|
|
|
|
return opts.qs;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'headers', {
|
|
|
|
get: function() {
|
|
|
|
return opts.headers || {};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Request.prototype = {
|
|
|
|
toString: function() {
|
|
|
|
return this.method + " " + this.path;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-04-13 09:15:04 +00:00
|
|
|
/**
|
|
|
|
* The HttpBackend class.
|
|
|
|
*/
|
|
|
|
module.exports = HttpBackend;
|