This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
93 lines (78 loc) · 2.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var methods = require("methods")
, PromiseBluebird = require("bluebird")
, supertest = require("supertest")
, util = require("util");
// Support SuperTest's historical `del` alias for `delete`
methods = methods.concat("del");
// Generate a SuperTest as Promised module that returns promise
// instances using the provided `Promise` constructor.
function makeModule(Promise) {
var out;
function toPromise() {
var self = this;
return new Promise(function (resolve, reject) {
self.end(function (err, res) {
if (err) {
// Attach response to error object so that details about the failure
// (e.g., the response body) can be accessed by a `.catch` handler
// (#30). Use `Object.defineProperty` so that `.response` is
// non-enumerable; otherwise, the error message is lost in the sea of
// the response object (#34).
Object.defineProperty(err, 'response', { value: res });
reject(err);
return;
}
resolve(res);
});
});
}
function then(onFulfilled, onRejected) {
return this.toPromise().then(onFulfilled, onRejected);
}
function _catch(onRejected) {
var promise = this.toPromise();
return promise.catch.apply(promise, arguments);
}
// Creates a new object that wraps `factory`, where each HTTP method
// (`get`, `post`, etc.) is overriden to inject a `then` method into
// the returned `Test` instance.
function wrap(factory, out) {
methods.forEach(function (method) {
out[method] = function () {
var test = factory[method].apply(this, arguments);
test.toPromise = toPromise;
test.then = then;
test.catch = _catch;
return test;
};
});
return out;
}
out = function () {
var request = supertest.apply(null, arguments);
return wrap(request, {});
}
out.agent = function () {
var self = this;
if (!(this instanceof out.agent)) {
self = Object.create(out.agent.prototype);
}
supertest.agent.apply(self, arguments);
return self;
}
util.inherits(out.agent, supertest.agent);
wrap(supertest.agent.prototype, out.agent.prototype);
return out;
}
// For backwards compatibility, we allow SuperTest as Promised to be
// used without an explicit `Promise` constructor. Pass these requests
// through to a default module that uses Bluebird promises.
var defaultModule = makeModule(PromiseBluebird);
module.exports = function (maybePromise) {
if (typeof maybePromise.resolve === 'function' &&
typeof maybePromise.reject === 'function') {
return makeModule(maybePromise);
}
return defaultModule.apply(null, arguments);
}
module.exports.agent = defaultModule.agent;