Skip to content

Commit

Permalink
Merge pull request ladjs#29 from vesln/remote-servers
Browse files Browse the repository at this point in the history
Add support for remote servers
  • Loading branch information
tj committed Oct 15, 2012
2 parents d4cedb0 + 3acea53 commit e70506d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/test.js
Original file line number Diff line number Diff line change
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
18 changes: 18 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit e70506d

Please sign in to comment.