From 02aa70f2eb8d6b236bef98a647076a32d8a1dee5 Mon Sep 17 00:00:00 2001 From: Jonathan Knapp Date: Wed, 7 Mar 2018 10:36:07 -0800 Subject: [PATCH 1/2] Added prefix option for agents --- lib/agent.js | 4 +++- test/supertest.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/agent.js b/lib/agent.js index 5f16ae3d..d7bef5c5 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; @@ -43,7 +44,8 @@ TestAgent.prototype.__proto__ = Agent.prototype; // 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); + // TODO: support prefix on actual urls + var req = new Test(this.app, method.toUpperCase(), (this._prefix || '') + url); req.ca(this._ca); req.cert(this._cert); req.key(this._key); diff --git a/test/supertest.js b/test/supertest.js index c9a4927b..03590e86 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -815,6 +815,17 @@ describe('request.agent(app)', function() { }); }); +describe('request.agent(app, {prefix})', function() { + it('should apply prefix', function(done) { + var app = express(); + var agent = request.agent(app, { prefix: '/api' }); + + agent + .get('/dummy') + .expect(404, 'Cannot GET /api/dummy\n', done); + }); +}); + describe('. works as expected', function() { it('.delete should work', function (done) { var app = express(); From 653a977301e5cbb5497016a2fb19a32ce275d03d Mon Sep 17 00:00:00 2001 From: Jonathan Knapp Date: Fri, 18 May 2018 04:32:47 -0700 Subject: [PATCH 2/2] Add positive test case for prefix --- test/supertest.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/supertest.js b/test/supertest.js index 1f347850..723ede78 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -841,10 +841,17 @@ 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', done); + .expect(404, 'Cannot GET /api/dummy\n'); + + agent + .get('/something') + .expect(200, done); }); });