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