Skip to content

Commit

Permalink
Add support for remote servers
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Oct 15, 2012
1 parent d4cedb0 commit f481cfb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
26 changes: 21 additions & 5 deletions lib/test.js
Expand Up @@ -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);
}

/**
Expand All @@ -50,6 +49,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:
*
Expand Down
20 changes: 19 additions & 1 deletion test/supertest.js
Expand Up @@ -35,7 +35,25 @@ describe('request(app)', function(){
.end(function(err, res){
res.should.have.status(200);
res.text.should.equal('hey');
done();
server.close(done)
});
});
})

it('should work with remote server', function(done){
var app = express();

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

var server = app.listen(4000, function(){
request('http://localhost:4000')
.get('/')
.end(function(err, res){
res.should.have.status(200);
res.text.should.equal('hey');
server.close(done)
});
});
})
Expand Down

0 comments on commit f481cfb

Please sign in to comment.