forked from rjz/supertest-session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (73 loc) · 2.34 KB
/
index.js
File metadata and controls
93 lines (73 loc) · 2.34 KB
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 assign = require('object-assign'),
methods = require('methods'),
supertest = require('supertest'),
util = require('util'),
CookieAccess = require('cookiejar').CookieAccessInfo,
parse = require('url').parse;
function Session (app, options) {
// istanbul ignore if
if (!app) {
throw new Error('Session requires an `app`');
}
this.app = app;
this.options = options || {};
this.reset();
if (this.options.helpers instanceof Object) {
assign(this, this.options.helpers);
}
}
Object.defineProperty(Session.prototype, 'cookies', {
get: function () {
return this.agent.jar.getCookies(this.cookieAccess);
}
});
Session.prototype.reset = function () {
var url, cookieAccessOptions, domain, path, secure, script;
// Unset supertest-session options before forwarding options to superagent.
var agentOptions = assign({}, this.options, {
before: undefined,
cookieAccess: undefined,
destroy: undefined,
helpers: undefined
});
this.agent = supertest.agent(this.app, agentOptions);
url = parse(this.agent.get('').url);
cookieAccessOptions = this.options.cookieAccess || {};
domain = cookieAccessOptions.domain || url.hostname;
path = cookieAccessOptions.path || url.path;
secure = !!cookieAccessOptions.secure || 'https:' == url.protocol;
script = !!cookieAccessOptions.script || false;
this.cookieAccess = CookieAccess(domain, path, secure, script);
};
Session.prototype.destroy = function () {
if (this.options.destroy) {
this.options.destroy.call(this);
}
this.reset();
};
Session.prototype.request = function (meth, route) {
var test = this.agent[meth](route);
if (this.options.before) {
this.options.before.call(this, test);
}
test.exec = this.exec.bind(test);
return test;
};
Session.prototype.exec = function () {
return new Promise(function (resolve, reject) {
this.end(function (error, res) {
error ? reject(error) : resolve(res);
});
}.bind(this));
}
methods.forEach(function (m) {
Session.prototype[m] = function () {
var args = [].slice.call(arguments);
return this.request.apply(this, [m].concat(args));
};
});
Session.prototype.del = util.deprecate(Session.prototype.delete,
'supertest-session: Session.del is deprecated; please use Session.delete');
module.exports = function (app, options) {
return new Session(app, options);
};