Permalink
Browse files

Merge pull request #29 from vesln/remote-servers

Add support for remote servers
  • Loading branch information...
2 parents d4cedb0 + 3acea53 commit e70506dc7a465798433812cd4dabe07267b25322 @tj tj committed Oct 15, 2012
Showing with 39 additions and 5 deletions.
  1. +21 −5 lib/test.js
  2. +18 −0 test/supertest.js
View
@@ -37,11 +37,10 @@ function Test(app, method, path) {
this.redirects(0);
this.app = app;
this._fields = {};
- var addr = app.address();
- var portno = addr ? addr.port : port++;
- if (!addr) app.listen(portno);
- var protocol = app instanceof https.Server ? 'https' : 'http';
- this.url = protocol + '://127.0.0.1:' + portno + path;
+
+ this.url = 'string' === typeof app
+ ? app + path
+ : this.serverAddress(app, path);
}
/**
@@ -51,6 +50,23 @@ function Test(app, method, path) {
Test.prototype.__proto__ = Request.prototype;
/**
+ * Returns a URL, extracted from a server.
+ *
+ * @param {Server} app
+ * @param {String} path
+ * @returns {String} URL address
+ * @api private
+ */
+
+Test.prototype.serverAddress = function(app, path){
+ var addr = app.address();
+ var portno = addr ? addr.port : port++;
+ if (!addr) app.listen(portno);
+ var protocol = app instanceof https.Server ? 'https' : 'http';
+ return protocol + '://127.0.0.1:' + portno + path;
+};
+
+/**
* Expectations:
*
* .expect(200)
View
@@ -40,6 +40,24 @@ describe('request(app)', function(){
});
})
+ it('should work with remote server', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send('hey');
+ });
+
+ var server = app.listen(4001, function(){
+ request('http://localhost:4001')
+ .get('/')
+ .end(function(err, res){
+ res.should.have.status(200);
+ res.text.should.equal('hey');
+ done();
+ });
+ });
+ })
+
it('should work with a https server', function(done){
var app = express();

0 comments on commit e70506d

Please sign in to comment.