PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/suites/ConsumerSpec.js

http://github.com/bytespider/jsOAuth
JavaScript | 129 lines | 88 code | 29 blank | 12 comment | 0 complexity | c9c463c7cff50ea0d0b43b9d49e8ee9e MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. module('OAuth Consumer');
  2. test('Basics', function () {
  3. ok(window.OAuth !== undefined, 'Global object exists');
  4. });
  5. test('toHeaderString', function () {
  6. var params, realm_param, realm_key, realm_val;
  7. params = {
  8. realm: 'http://www.example.com',
  9. consumerKey: 'consumerkey',
  10. consumerSecret: 'consumersecret'
  11. };
  12. realm_param = toHeaderString(params).match(/^(realm)="(.+?)"/);
  13. realm_key = realm_param[1];
  14. realm_val = realm_param[2];
  15. notStrictEqual(realm_param, null, "'realm' is first parameter");
  16. equal(realm_val, 'http://www.example.com', "'realm' value is not encoded");
  17. });
  18. test('toSignatureBaseString', function () {
  19. /**
  20. * Generate a signature base string for the request
  21. *
  22. * @param method {string} ['GET', 'POST', 'PUT', ...]
  23. * @param url {string} A valid http(s) url
  24. * @param header_params A key value paired object of additional headers
  25. * @param query_params {object} A key value paired object of data
  26. * example: {'q':'foobar'}
  27. * for GET this will append a query string
  28. function toSignatureBaseString(method, url, header_params, query_params) {
  29. */
  30. var s = toSignatureBaseString("GET", "http://www.example.com", {"h1":"v1", "h1-2":"v2"}, {"q1":"v1", "q1-2":"v2"});
  31. equals(s, "GET&http%3A%2F%2Fwww.example.com&h1%3Dv1%26h1-2%3Dv2%26q1%3Dv1%26q1-2%3Dv2");
  32. });
  33. test('Output URL Encode', function () {
  34. equals(OAuth.urlEncode(''), '', 'Output test 1');
  35. equals(OAuth.urlEncode("\t\r\n $ & < > ? ; # : = , \" ' ~ + %"), '%09%0D%0A%20%24%20%26%20%3C%20%3E%20%3F%20%3B%20%23%20%3A%20%3D%20%2C%20%22%20%27%20~%20%2B%20%25', 'Output test 2');
  36. equals(OAuth.urlEncode('ß'), '%C3%9F', 'Output test 3');
  37. equals(OAuth.urlEncode('ü'), '%C3%BC', 'Output test 4');
  38. });
  39. asyncTest("OAuth URL query params and data request", function() {
  40. var oauth = OAuth({
  41. enablePrivilege: false,
  42. consumerKey: 'ba9df9055c77f338',
  43. consumerSecret: '846ffe1ec3b18989e73fe7fff833'
  44. });
  45. oauth.request({
  46. url: 'http://oauth-sandbox.sevengoslings.net/two_legged?param1=parameter',
  47. data: {
  48. status: 'test'
  49. },
  50. success: function (data) {
  51. start();
  52. ok(data.text.search('SUCCESS! This is a 2-legged call from the `jsOAuth2` consumer which was made by `bytespider`.'), 'Request success');
  53. //equals(data.text, 'SUCCESS! This is a 2-legged call from the `jsOAuth2` consumer which was made by `bytespider`.', 'Request success');
  54. }
  55. });
  56. });
  57. asyncTest("OAuth 2-Legged Request", function() {
  58. var oauth = OAuth({
  59. enablePrivilege: false,
  60. consumerKey: 'ba9df9055c77f338',
  61. consumerSecret: '846ffe1ec3b18989e73fe7fff833'
  62. });
  63. oauth.get('http://oauth-sandbox.sevengoslings.net/two_legged', function (data) {
  64. start();
  65. ok(data.text.search('SUCCESS! This is a 2-legged call from the `jsOAuth2` consumer which was made by `bytespider`.'), 'Request success');
  66. });
  67. });
  68. var oauth = OAuth({
  69. enablePrivilege: false,
  70. consumerKey: 'ba9df9055c77f338',
  71. consumerSecret: '846ffe1ec3b18989e73fe7fff833',
  72. realm: 'http://oauth-sandbox.sevengoslings.net',
  73. requestTokenUrl: 'http://oauth-sandbox.sevengoslings.net/request_token',
  74. authorizationUrl: 'http://oauth-sandbox.sevengoslings.net/authorize',
  75. accessTokenUrl: 'http://oauth-sandbox.sevengoslings.net/access_token'
  76. });
  77. asyncTest("OAuth Authorise", function() {
  78. oauth.fetchRequestToken(function (url) {
  79. var windowObjectReference = window.open(url, 'authorise');
  80. var mask = document.createElement('div');
  81. mask.setAttribute('id', 'mask');
  82. mask.innerHTML = '<div id="start-app" class="popup"><p>Once you have logged in and authorized this application with your browser, please enter the provided code and click the button below.</p><input type="text" id="verification" placeholder="enter code"><input id="start-app-button" type="button" value="Start application"></div>';
  83. document.body.appendChild(mask);
  84. var button = document.getElementById('start-app-button');
  85. button.onclick = function() {
  86. var code = document.getElementById('verification').value;
  87. oauth.setVerifier(code);
  88. oauth.fetchAccessToken(function(data){
  89. ok(true);
  90. start();
  91. }, function (data) {console.log(data)});
  92. };
  93. }, function (data) {console.log(data)});
  94. });
  95. asyncTest("OAuth 3-Legged Request", function() {
  96. oauth.get('http://oauth-sandbox.sevengoslings.net/three_legged', function (data) {
  97. start();
  98. ok(data.text.search('SUCCESS! This is a 3-legged call from the `jsOAuth2` consumer which was made by `bytespider`.'), 'Request success');
  99. });
  100. });