Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for remote servers #29

Merged
merged 2 commits into from

2 participants

@vesln

I've added support for testing remote servers. It's kinda convenient for black-box testing. Let me know what you think.

@tj tj merged commit e70506d into visionmedia:master
@tj
Owner
tj commented

thanks for the clean patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 15, 2012
  1. @vesln

    Add support for remote servers

    vesln authored
  2. @vesln

    Fix some issues with node 0.6

    vesln authored
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 5 deletions.
  1. +21 −5 lib/test.js
  2. +18 −0 test/supertest.js
View
26 lib/test.js
@@ -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
18 test/supertest.js
@@ -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();
Something went wrong with that request. Please try again.