1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var Agent = require('superagent').agent;
|
7 | var methods = require('methods');
|
8 | var http = require('http');
|
9 | var Test = require('./test');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = TestAgent;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function TestAgent(app, options) {
|
26 | if (!(this instanceof TestAgent)) return new TestAgent(app, options);
|
27 | if (typeof app === 'function') app = http.createServer(app);
|
28 | if (options) {
|
29 | this._ca = options.ca;
|
30 | this._key = options.key;
|
31 | this._cert = options.cert;
|
32 | }
|
33 | Agent.call(this);
|
34 | this.app = app;
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | Object.setPrototypeOf(TestAgent.prototype, Agent.prototype);
|
42 |
|
43 |
|
44 | TestAgent.prototype.host = function(host) {
|
45 | this._host = host;
|
46 | return this;
|
47 | };
|
48 |
|
49 |
|
50 | methods.forEach(function(method) {
|
51 | TestAgent.prototype[method] = function(url, fn) {
|
52 | var req = new Test(this.app, method.toUpperCase(), url, this._host);
|
53 | req.ca(this._ca);
|
54 | req.cert(this._cert);
|
55 | req.key(this._key);
|
56 |
|
57 | req.on('response', this._saveCookies.bind(this));
|
58 | req.on('redirect', this._saveCookies.bind(this));
|
59 | req.on('redirect', this._attachCookies.bind(this, req));
|
60 | this._attachCookies(req);
|
61 | this._setDefaults(req);
|
62 |
|
63 | return req;
|
64 | };
|
65 | });
|
66 |
|
67 | TestAgent.prototype.del = TestAgent.prototype.delete;
|