Skip to content

Modify _performSecureRequest to not always set the content-type heade… #333

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

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, vers
throw new Error("Un-supported signature method: " + signatureMethod )
this._signatureMethod= signatureMethod;
this._nonceSize= nonceSize || 32;
this._contentType = 'application/x-www-form-urlencoded';
this._headers= customHeaders || {"Accept" : "*/*",
"Connection" : "close",
"User-Agent" : "Node authentication"}
Expand Down Expand Up @@ -309,7 +310,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, extra_params);

if( !post_content_type ) {
post_content_type= "application/x-www-form-urlencoded";
post_content_type= this._contentType;
}
var parsedUrl= URL.parse( url, false );
if( parsedUrl.protocol == "http:" && !parsedUrl.port ) parsedUrl.port= 80;
Expand Down Expand Up @@ -452,6 +453,9 @@ exports.OAuth.prototype.setClientOptions= function(options) {

this._clientOptions= mergedOptions;
};
exports.OAuth.prototype.setContentType= function(contentType) {
this._contentType = contentType;
};

exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback) {
var extraParams= {};
Expand Down
43 changes: 42 additions & 1 deletion tests/oauthtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,48 @@ vows.describe('OAuth').addBatch({
oa._createClient= op;
}
}
}
},
'Setting content-type on OAuth object should set content-type on request to value passed to setContentType': function(oa) {
var op= oa._createClient;
var opContentType = oa._contentType;
var expectedContentType = 'application/json';
oa.setContentType(expectedContentType);
var createClientHeaders = null;
try {
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
createClientHeaders = headers;
return {
write: function(post_body){
}
};
};
oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar,1,2"});
assert.equal(createClientHeaders['Content-Type'], expectedContentType);
}
finally {
oa._createClient= op;
oa._contentType = opContentType;
}
},
'Not setting content-type on OAuth object should set content-type on request to application/x-www-form-urlencoded': function(oa) {
var createClientHeaders = null;
var op= oa._createClient;
try {
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
createClientHeaders = headers;
return {
write: function(post_body){
}
};
};
oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar,1,2"});
assert.equal(createClientHeaders['Content-Type'], 'application/x-www-form-urlencoded');
}
finally {
oa.setContentType(null);
oa._createClient= op;
}
}
},
'When performing a secure' : {
topic: new OAuth("http://foo.com/RequestToken",
Expand Down