Provides a test-case for #160
This commit is contained in:
parent
e599d29787
commit
1eda281512
|
@ -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 ) {
|
||||
|
|
|
@ -125,13 +125,66 @@ 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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
|
|
Loading…
Reference in New Issue