From bd902366240f40d51aa6b1957746019283896c21 Mon Sep 17 00:00:00 2001 From: ciaranj Date: Sun, 31 Oct 2010 22:28:17 +0000 Subject: [PATCH] Added some tests to cover the existing behaviours before a refactor --- tests/oauth.js | 178 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 1 deletion(-) diff --git a/tests/oauth.js b/tests/oauth.js index dd48947..e9ef9a7 100644 --- a/tests/oauth.js +++ b/tests/oauth.js @@ -101,5 +101,181 @@ vows.describe('OAuth').addBatch({ assert.equal(oa._buildAuthorizationHeaders(parameters), 'OAuth oauth_timestamp="1234567",oauth_nonce="ABCDEF",oauth_version="1.0",oauth_signature_method="HMAC-SHA1",oauth_consumer_key="asdasdnm2321b3"'); Array.prototype.toString = _toString; } - } + }, + 'When performing the Secure Request' : { + topic: new OAuth("http://foo.com/RequestToken", + "http://foo.com/AccessToken", + "anonymous", "anonymous", + "1.0A", "http://foo.com/callback", "HMAC-SHA1"), + 'using the POST method' : { + 'Any passed extra_params should form part of the POST body': function(oa) { + var post_body_written= false; + var _oldRequest= oa.request; + oa.request= function(method, path, headers) { + return { + write: function(post_body) { + post_body_written= true; + assert.equal(post_body,"FOO"); + } + } + } + // oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar"}); + oa.request= _oldRequest; + assert.equal(post_body_written, true); + } + } +// exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) { + }, + 'When performing a secure' : { + topic: new OAuth("http://foo.com/RequestToken", + "http://foo.com/AccessToken", + "anonymous", "anonymous", + "1.0A", "http://foo.com/callback", "HMAC-SHA1"), + 'POST' : { + 'if no callback is passed' : { + 'it should return a request object': function(oa) { + var request= oa.post("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain") + assert.isObject(request); + assert.equal(request.method, "POST"); + request.end(); + } + }, + 'if a callback is passed' : { + "it should call the internal request's end method and return nothing": function(oa) { + var callbackCalled= false; + var op= oa._createClient; + oa._createClient= function() { + return { + request: function(method, path, headers) { + return { + write: function(){}, + socket: {addListener: function(){}}, + addListener: function() {}, + end: function() { + callbackCalled= true; + } + } + } + } + } + var request= oa.post("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain", function(e,d){}) + assert.equal(callbackCalled, true); + assert.isUndefined(request); + oa._createClient= op; + } + }, + 'if the post_body is not a string' : { + "It should be url encoded and the content type set to be x-www-form-urlencoded" : function(oa) { + var op= oa._createClient; + var callbackCalled= false; + oa._createClient= function() { + return { + request: function(method, path, headers) { + assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded") + return { + socket: {addListener: function(){}}, + write: function(data) { + callbackCalled= true; + assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); + }, + addListener: function() {}, + end: function() {} + } + } + } + } + var request= oa.post("http://foo.com/blah", "token", "token_secret", {"foo":"1,2,3", "bar":"1+2"}) + assert.equal(callbackCalled, true); + oa._createClient= op; + } + }, + 'if the post_body is a string' : { + "and no post_content_type is specified" : { + "It should be written as is, with a content length specified, and the encoding should be set to be x-www-form-urlencoded" : function(oa) { + var op= oa._createClient; + var callbackCalled= false; + oa._createClient= function() { + return { + request: function(method, path, headers) { + assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded"); + assert.equal(headers["Content-length"], 23); + return { + socket: {addListener: function(){}}, + write: function(data) { + callbackCalled= true; + assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); + }, + addListener: function() {}, + end: function() {} + } + } + } + } + var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2") + assert.equal(callbackCalled, true); + oa._createClient= op; + } + }, + "and a post_content_type is specified" : { + "It should be written as is, with a content length specified, and the encoding should be set to be as specified" : function(oa) { + var op= oa._createClient; + var callbackCalled= false; + oa._createClient= function() { + return { + request: function(method, path, headers) { + assert.equal(headers["Content-Type"], "unicorn/encoded"); + assert.equal(headers["Content-length"], 23); + return { + socket: {addListener: function(){}}, + write: function(data) { + callbackCalled= true; + assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); + }, + addListener: function() {}, + end: function() {} + } + } + } + } + var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2", "unicorn/encoded") + assert.equal(callbackCalled, true); + oa._createClient= op; + } + } + } + }, + 'GET' : { + 'if no callback is passed' : { + 'it should return a request object': function(oa) { + var request= oa.get("http://foo.com/blah", "token", "token_secret") + assert.isObject(request); + assert.equal(request.method, "GET"); + request.end(); + } + }, + 'if a callback is passed' : { + "it should call the internal request's end method and return nothing": function(oa) { + var callbackCalled= false; + var op= oa._createClient; + oa._createClient= function() { + return { + request: function(method, path, headers) { + return { + socket: {addListener: function(){}}, + addListener: function() {}, + end: function() { + callbackCalled= true; + } + } + } + } + } + var request= oa.get("http://foo.com/blah", "token", "token_secret", function(e,d) {}) + assert.equal(callbackCalled, true); + assert.isUndefined(request); + oa._createClient= op; + } + }, + } + } }).export(module);