-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
98 lines (75 loc) · 2.44 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
94
95
96
97
98
var assign = require('object-assign'),
methods = require('methods'),
supertest = require('supertest'),
util = require('util'),
http = require('http'),
https = require('https'),
CookieAccess = require('cookiejar').CookieAccessInfo,
parse = require('url').parse;
function Session (app, options) {
// istanbul ignore if
if (!app) {
throw new Error('Session requires an `app`');
}
var url = app;
this.agent = supertest.agent(this.app, this.options);
if (typeof app === 'function') {
app = http.createServer(app);
}
if (app instanceof http.Server || app instanceof https.Server) {
url = supertest.Test.prototype.serverAddress(app, '/');
}
this.app = app;
this.url = parse(url);
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 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);
cookieAccessOptions = this.options.cookieAccess || {};
domain = cookieAccessOptions.domain || this.url.hostname;
path = cookieAccessOptions.path || this.url.path;
secure = !!cookieAccessOptions.secure || 'https:' == this.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);
}
return test;
};
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);
};