agent.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Module dependencies.
  3. */
  4. var Agent = require('superagent').agent
  5. , methods = require('methods')
  6. , http = require('http')
  7. , Test = require('./test');
  8. /**
  9. * Expose `Agent`.
  10. */
  11. module.exports = TestAgent;
  12. /**
  13. * Initialize a new `TestAgent`.
  14. *
  15. * @param {Function|Server} app
  16. * @param {Object} options
  17. * @api public
  18. */
  19. function TestAgent(app, options){
  20. if (!(this instanceof TestAgent)) return new TestAgent(app, options);
  21. if ('function' == typeof app) app = http.createServer(app);
  22. if (options) this._ca = options.ca;
  23. Agent.call(this);
  24. this.app = app;
  25. }
  26. /**
  27. * Inherits from `Agent.prototype`.
  28. */
  29. TestAgent.prototype.__proto__ = Agent.prototype;
  30. // override HTTP verb methods
  31. methods.forEach(function(method){
  32. TestAgent.prototype[method] = function(url, fn){
  33. var req = new Test(this.app, method.toUpperCase(), url);
  34. req.ca(this._ca);
  35. req.on('response', this.saveCookies.bind(this));
  36. req.on('redirect', this.saveCookies.bind(this));
  37. req.on('redirect', this.attachCookies.bind(this, req));
  38. this.attachCookies(req);
  39. return req;
  40. };
  41. });
  42. TestAgent.prototype.del = TestAgent.prototype.delete;