Skip to content

Commit

Permalink
Merge branch 'broox-oauth2_put'
Browse files Browse the repository at this point in the history
Conflicts:
	tests/oauth2.js
  • Loading branch information
ciaranj committed May 21, 2014
2 parents 9e9ff17 + 1eda281 commit 00ae962
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
18 changes: 12 additions & 6 deletions lib/oauth2.js
Expand Up @@ -49,19 +49,25 @@ exports.OAuth2.prototype.buildAuthHeader= function(token) {
return this._authMethod + ' ' + token;
};

exports.OAuth2.prototype._chooseHttpLibrary= function( parsedUrl ) {
var http_library= https;
// As this is OAUth2, we *assume* https unless told explicitly otherwise.
if( parsedUrl.protocol != "https:" ) {
http_library= http;
}
return http_library;
};

exports.OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) {

var http_library= https;
var creds = crypto.createCredentials({ });
var parsedUrl= URL.parse( url, true );
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) {
parsedUrl.port= 443;
}

// As this is OAUth2, we *assume* https unless told explicitly otherwise.
if( parsedUrl.protocol != "https:" ) {
http_library= http;
}
var http_library= this._chooseHttpLibrary( parsedUrl );


var realHeaders= {};
for( var key in this._customHeaders ) {
Expand Down Expand Up @@ -133,7 +139,7 @@ exports.OAuth2.prototype._executeRequest= function( http_library, options, post_
callback(e);
});

if( options.method == 'POST' && post_body ) {
if( (options.method == 'POST' || options.method == 'PUT') && post_body ) {
request.write(post_body);
}
request.end();
Expand Down
57 changes: 55 additions & 2 deletions tests/oauth2.js
Expand Up @@ -125,15 +125,68 @@ vows.describe('OAuth2').addBatch({
'Given an OAuth2 instance with clientId, clientSecret and customHeaders': {
topic: new OAuth2("clientId", "clientSecret", undefined, undefined, undefined,
{ 'SomeHeader': '123' }),
'When calling get': {
'When GETing': {
'we should see the custom headers mixed into headers property in options passed to http-library' : function(oa) {
oa._executeRequest= function( http_library, options, callback ) {
assert.equal(options.headers["SomeHeader"], "123");
};
oa.get("", {});
}
},
}
},
'Given an OAuth2 instance with a clientId and clientSecret': {
topic: new OAuth2("clientId", "clientSecret"),
'When POSTing': {
'we should see a given string being sent to the request' : function(oa) {
var bodyWritten= false;
oa._chooseHttpLibrary= function() {
return {
request: function(options) {
assert.equal(options.headers["Content-Type"], "text/plain");
assert.equal(options.headers["Content-Length"], 26);
assert.equal(options.method, "POST");
return {
end: function() {},
on: function() {},
write: function(body) {
bodyWritten= true;
assert.isNotNull(body);
assert.equal(body, "THIS_IS_A_POST_BODY_STRING")
}
}
}
};
}
oa._request("POST", "", {"Content-Type":"text/plain"}, "THIS_IS_A_POST_BODY_STRING");
assert.ok( bodyWritten );
}
},
'When PUTing': {
'we should see a given string being sent to the request' : function(oa) {
var bodyWritten= false;
oa._chooseHttpLibrary= function() {
return {
request: function(options) {
assert.equal(options.headers["Content-Type"], "text/plain");
assert.equal(options.headers["Content-Length"], 25);
assert.equal(options.method, "PUT");
return {
end: function() {},
on: function() {},
write: function(body) {
bodyWritten= true;
assert.isNotNull(body);
assert.equal(body, "THIS_IS_A_PUT_BODY_STRING")
}
}
}
};
}
oa._request("PUT", "", {"Content-Type":"text/plain"}, "THIS_IS_A_PUT_BODY_STRING");
assert.ok( bodyWritten );
}
}
},
'When the user passes in the User-Agent in customHeaders': {
topic: new OAuth2("clientId", "clientSecret", undefined, undefined, undefined,
{ 'User-Agent': '123Agent' }),
Expand Down

0 comments on commit 00ae962

Please sign in to comment.