Skip to content

Commit 6baf574

Browse files
PhilipSkinnerPhilip Skinner
authored andcommitted
Adding ability to specify an agent for OAuth2 requests
1 parent ec6a1b2 commit 6baf574

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/oauth2.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, access
1515
this._authMethod= "Bearer";
1616
this._customHeaders = customHeaders || {};
1717
this._useAuthorizationHeaderForGET= false;
18-
}
18+
19+
//our agent
20+
this._agent = undefined;
21+
};
22+
23+
// Allows you to set an agent to use instead of the default HTTP or
24+
// HTTPS agents. Useful when dealing with your own certificates.
25+
exports.OAuth2.prototype.setAgent = function(agent) {
26+
this._agent = agent;
27+
};
1928

2029
// This 'hack' method is required for sites that don't use
2130
// 'access_token' as the name of the access token (for requests).
@@ -129,6 +138,11 @@ exports.OAuth2.prototype._executeRequest= function( http_library, options, post_
129138

130139
var result= "";
131140

141+
//set the agent on the request options
142+
if (this._agent) {
143+
options.agent = this._agent;
144+
}
145+
132146
var request = http_library.request(options);
133147
request.on('response', function (response) {
134148
response.on("data", function (chunk) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{ "name" : "oauth"
22
, "description" : "Library for interacting with OAuth 1.0, 1.0A, 2 and Echo. Provides simplified client access and allows for construction of more complex apis and OAuth providers."
3-
, "version" : "0.9.14"
3+
, "version" : "0.9.15"
44
, "directories" : { "lib" : "./lib" }
55
, "main" : "index.js"
66
, "author" : "Ciaran Jessup <[email protected]>"

tests/oauth2tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,19 @@ vows.describe('OAuth2').addBatch({
286286
oa.get("", {});
287287
}
288288
}
289+
},
290+
'When specifying an agent, that agent is passed to the HTTP request method' : {
291+
topic : new OAuth2('clientId', 'clientSecret', undefined, undefined, undefined, undefined),
292+
'When calling _executeRequest': {
293+
'we whould see the agent being put into the options' : function(oa) {
294+
oa.setAgent('awesome agent');
295+
oa._executeRequest({
296+
request : function(options, cb) {
297+
assert.equal(options.agent, 'awesome agent');
298+
return new DummyRequest(new DummyResponse(200));
299+
}
300+
}, {}, null, function() {});
301+
}
302+
}
289303
}
290304
}).export(module);

0 commit comments

Comments
 (0)