Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit b1abb67c0f14d14e83d970dbf253954c3766c935 @tj tj committed Jun 26, 2012
Showing with 173 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 Makefile
  3. +68 −0 index.js
  4. +30 −0 lib/methods.js
  5. +28 −0 package.json
  6. +39 −0 test/supertest.js
1 .gitignore
@@ -0,0 +1 @@
+node_modules
7 Makefile
@@ -0,0 +1,7 @@
+
+test:
+ @./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec
+
+.PHONY: test
68 index.js
@@ -0,0 +1,68 @@
+
+/**
+ * Module dependencies.
+ */
+
+var request = require('superagent')
+ , methods = require('./lib/methods')
+ , Request = request.Request
+ , http = require('http');
+
+module.exports = function(app){
+ if ('function' == typeof app) app = http.createServer(app);
+ var obj = {};
+
+ methods.forEach(function(method){
+ var name = 'delete' == method
+ ? 'del'
+ : method;
+
+ method = method.toUpperCase();
+ obj[name] = function(url){
+ return new Test(app, method, url);
+ };
+ });
+
+ return obj;
+};
+
+/**
+ * Initialize a new `Test` with the given `app`,
+ * request `method` and `path`.
+ *
+ * @param {Server} app
+ * @param {String} method
+ * @param {String} path
+ * @api public
+ */
+
+function Test(app, method, path) {
+ Request.call(this, method, path);
+ this.app = app;
+}
+
+/**
+ * Inherits from `Request.prototype`.
+ */
+
+Test.prototype.__proto__ = Request.prototype;
+
+/**
+ * Defer invoking superagent's `.end()` until
+ * the server is listening.
+ *
+ * @param {Function} fn
+ * @api public
+ */
+
+Test.prototype.end = function(fn){
+ var self = this;
+ var end = Request.prototype.end;
+ var app = this.app;
+ var addr = app.address();
+ if (!addr) return app.listen(0, function(){ self.end(fn); });
+ this.url = 'http://' + addr.address + ':' + addr.port + this.url;
+ end.call(this, fn);
+};
+
+
30 lib/methods.js
@@ -0,0 +1,30 @@
+
+/**
+ * HTTP methods supported by node.
+ */
+
+module.exports = [
+ 'get'
+ , 'post'
+ , 'put'
+ , 'head'
+ , 'delete'
+ , 'options'
+ , 'trace'
+ , 'copy'
+ , 'lock'
+ , 'mkcol'
+ , 'move'
+ , 'propfind'
+ , 'proppatch'
+ , 'unlock'
+ , 'report'
+ , 'mkactivity'
+ , 'checkout'
+ , 'merge'
+ , 'm-search'
+ , 'notify'
+ , 'subscribe'
+ , 'unsubscribe'
+ , 'patch'
+];
28 package.json
@@ -0,0 +1,28 @@
+{
+ "name": "supertest",
+ "version": "0.0.1",
+ "description": "Super-agent driven library for testing HTTP servers",
+ "main": "index.js",
+ "scripts": {
+ "test": "make test"
+ },
+ "dependencies": {
+ "superagent": "0.5.0"
+ },
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*",
+ "express": "3.0.0beta4"
+ },
+ "keywords": [
+ "superagent",
+ "request",
+ "tdd",
+ "bdd",
+ "http",
+ "test",
+ "testing"
+ ],
+ "author": "TJ Holowaychuk",
+ "license": "MIT"
+}
39 test/supertest.js
@@ -0,0 +1,39 @@
+
+var request = require('..')
+ , express = require('express');
+
+describe('request(app)', function(){
+ it('should fire up the app on an ephemeral port', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send('hey');
+ });
+
+ request(app)
+ .get('/')
+ .end(function(res){
+ res.should.have.status(200);
+ res.text.should.equal('hey');
+ done();
+ });
+ })
+
+ it('should work with an active server', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send('hey');
+ });
+
+ var server = app.listen(4000, function(){
+ request(server)
+ .get('/')
+ .end(function(res){
+ res.should.have.status(200);
+ res.text.should.equal('hey');
+ done();
+ });
+ });
+ })
+})

0 comments on commit b1abb67

Please sign in to comment.
Something went wrong with that request. Please try again.