Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added request.agent(app) support #54

Merged
merged 3 commits into from

3 participants

@Arxisos

I added request.agent(app) support.
See following test:

describe('request.agent(app)', function(){
  var app = express();

  app.use(express.cookieParser());

  app.get('/', function(req, res){
    res.cookie('cookie', 'hey');
    res.send();
  });

  app.get('/return', function(req, res){
    if(req.cookies.cookie) res.send(req.cookies.cookie);
    else res.send(':(')
  });

  var agent = request.agent(app);

  it('should save cookies', function(done){
    agent
    .get('/')
    .expect('set-cookie', 'cookie=hey; Path=/', done);
  })

  it('should send cookies', function(done){
    agent
    .get('/return')
    .expect('hey', done);
  })
})
@Arxisos

I added another commit which exposes Test to enable easier extensibility (ie custom expectations, ...).

@yields

+1

@tj tj merged commit c6d2fad into visionmedia:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 14, 2013
  1. @Arxisos

    Updated express dependency

    Arxisos authored
  2. @Arxisos
Commits on Mar 20, 2013
  1. @Arxisos
This page is out of date. Refresh to see the latest.
Showing with 96 additions and 1 deletion.
  1. +12 −0 index.js
  2. +53 −0 lib/agent.js
  3. +1 −1  package.json
  4. +30 −0 test/supertest.js
View
12 index.js
@@ -32,3 +32,15 @@ module.exports = function(app){
return obj;
};
+
+/**
+ * Expose `Test`
+ */
+
+module.exports.Test = Test;
+
+/**
+ * Expose the agent function
+ */
+
+module.exports.agent = require('./lib/agent');
View
53 lib/agent.js
@@ -0,0 +1,53 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Agent = require('superagent').agent
+ , methods = require('methods')
+ , http = require('http')
+ , Test = require('./test');
+
+/**
+ * Expose `Agent`.
+ */
+
+module.exports = TestAgent;
+
+/**
+ * Initialize a new `TestAgent`.
+ *
+ * @param {Function|Server} app
+ * @api public
+ */
+
+function TestAgent(app){
+ if (!(this instanceof TestAgent)) return new TestAgent(app);
+ if ('function' == typeof app) app = http.createServer(app);
+ Agent.call(this);
+ this.app = app;
+}
+
+/**
+ * Inherits from `Agent.prototype`.
+ */
+
+TestAgent.prototype.__proto__ = Agent.prototype;
+
+// override HTTP verb methods
+
+methods.forEach(function(method){
+ var name = 'delete' == method ? 'del' : method;
+
+ method = method.toUpperCase();
+ TestAgent.prototype[name] = function(url, fn){
+ var req = new Test(this.app, method, url);
+
+ req.on('response', this.saveCookies.bind(this));
+ req.on('redirect', this.saveCookies.bind(this));
+ req.on('redirect', this.attachCookies.bind(this, req));
+ this.attachCookies(req);
+
+ return req;
+ };
+});
View
2  package.json
@@ -13,7 +13,7 @@
"devDependencies": {
"mocha": "*",
"should": "*",
- "express": "3.0.0beta4"
+ "express": "3.1.0"
},
"keywords": [
"superagent",
View
30 test/supertest.js
@@ -369,3 +369,33 @@ describe('request(app)', function(){
})
})
})
+
+describe('request.agent(app)', function(){
+ var app = express();
+
+ app.use(express.cookieParser());
+
+ app.get('/', function(req, res){
+ res.cookie('cookie', 'hey');
+ res.send();
+ });
+
+ app.get('/return', function(req, res){
+ if(req.cookies.cookie) res.send(req.cookies.cookie);
+ else res.send(':(')
+ });
+
+ var agent = request.agent(app);
+
+ it('should save cookies', function(done){
+ agent
+ .get('/')
+ .expect('set-cookie', 'cookie=hey; Path=/', done);
+ })
+
+ it('should send cookies', function(done){
+ agent
+ .get('/return')
+ .expect('hey', done);
+ })
+})
Something went wrong with that request. Please try again.