Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added prefix option for agents #465

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/agent.js
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions test/supertest.js
Expand Up @@ -891,6 +891,24 @@ describe('agent.host(host)', function () {
});
});

describe('request.agent(app, {prefix})', function () {
it('should apply prefix', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you provide a successful test (e.g. with 200 status code) as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I can't believe I didn't do that already!

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('.<http verb> works as expected', function () {
it('.delete should work', function (done) {
const app = express();
Expand Down