UNPKG

1.52 kBJavaScriptView Raw
1
2/**
3 * Module dependencies.
4 */
5
6var Agent = require('superagent').agent;
7var methods = require('methods');
8var http = require('http');
9var Test = require('./test');
10
11/**
12 * Expose `Agent`.
13 */
14
15module.exports = TestAgent;
16
17/**
18 * Initialize a new `TestAgent`.
19 *
20 * @param {Function|Server} app
21 * @param {Object} options
22 * @api public
23 */
24
25function TestAgent(app, options) {
26 if (!(this instanceof TestAgent)) return new TestAgent(app, options);
27 if (typeof app === 'function') app = http.createServer(app); // eslint-disable-line no-param-reassign
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 * Inherits from `Agent.prototype`.
39 */
40
41Object.setPrototypeOf(TestAgent.prototype, Agent.prototype);
42
43// set a host name
44TestAgent.prototype.host = function(host) {
45 this._host = host;
46 return this;
47};
48
49// override HTTP verb methods
50methods.forEach(function(method) {
51 TestAgent.prototype[method] = function(url, fn) { // eslint-disable-line no-unused-vars
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
67TestAgent.prototype.del = TestAgent.prototype.delete;