diff --git a/lib/agent.js b/lib/agent.js index 16c9560a..ad540470 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -29,6 +29,7 @@ function TestAgent(app, options) { this._ca = options.ca; this._key = options.key; this._cert = options.cert; + this._prefix = options.prefix; } Agent.call(this); this.app = app; @@ -49,7 +50,8 @@ TestAgent.prototype.host = function(host) { // override HTTP verb methods methods.forEach(function(method) { TestAgent.prototype[method] = function(url, fn) { // eslint-disable-line no-unused-vars - var req = new Test(this.app, method.toUpperCase(), url, this._host); + // TODO: support prefix on actual urls (with a domain or protocal) + var req = new Test(this.app, method.toUpperCase(), (this._prefix || '') + url, this._host); req.ca(this._ca); req.cert(this._cert); req.key(this._key); diff --git a/test/supertest.js b/test/supertest.js index dfa90a7c..e8b72b6c 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -891,6 +891,24 @@ describe('agent.host(host)', function () { }); }); +describe('request.agent(app, {prefix})', function () { + it('should apply prefix', function(done) { + var app = express(); + var agent = request.agent(app, { prefix: '/api' }); + app.use('/api/something', function(err, res) { + res.send(); + }); + + agent + .get('/dummy') + .expect(404, 'Cannot GET /api/dummy\n'); + + agent + .get('/something') + .expect(200, done); + }); +}); + describe('. works as expected', function () { it('.delete should work', function (done) { const app = express();