/spec/suites/ConsumerSpec.js

https://github.com/RobWC/jsOAuth · JavaScript · 108 lines · 84 code · 23 blank · 1 comment · 0 complexity · 6c62ce9940e1e826131420f5da9051a9 MD5 · raw file

  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('Output URL Encode', function () {
  19. equals(OAuth.urlEncode(''), '', 'Output test 1');
  20. equals(OAuth.urlEncode("\r\n $ & < > ? ; # : = , \" ' ~ + %"), '%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');
  21. equals(OAuth.urlEncode('ß'), '%C3%9F', 'Output test 3');
  22. equals(OAuth.urlEncode('ü'), '%C3%BC', 'Output test 4');
  23. });
  24. asyncTest("OAuth URL query params and data request", function() {
  25. var oauth = OAuth({
  26. enablePrivilege: false,
  27. consumerKey: 'ba9df9055c77f338',
  28. consumerSecret: '846ffe1ec3b18989e73fe7fff833'
  29. });
  30. oauth.request({
  31. url: 'http://oauth-sandbox.sevengoslings.net/two_legged?param1=parameter',
  32. data: {
  33. status: 'test'
  34. },
  35. success: function (data) {
  36. start();
  37. ok(data.text.search('SUCCESS! This is a 2-legged call from the `jsOAuth2` consumer which was made by `bytespider`.'), 'Request success');
  38. //equals(data.text, 'SUCCESS! This is a 2-legged call from the `jsOAuth2` consumer which was made by `bytespider`.', 'Request success');
  39. }
  40. });
  41. });
  42. asyncTest("OAuth 2-Legged Request", function() {
  43. var oauth = OAuth({
  44. enablePrivilege: false,
  45. consumerKey: 'ba9df9055c77f338',
  46. consumerSecret: '846ffe1ec3b18989e73fe7fff833'
  47. });
  48. oauth.get('http://oauth-sandbox.sevengoslings.net/two_legged', function (data) {
  49. start();
  50. ok(data.text.search('SUCCESS! This is a 2-legged call from the `jsOAuth2` consumer which was made by `bytespider`.'), 'Request success');
  51. });
  52. });
  53. var oauth = OAuth({
  54. enablePrivilege: false,
  55. consumerKey: 'ba9df9055c77f338',
  56. consumerSecret: '846ffe1ec3b18989e73fe7fff833',
  57. realm: 'http://oauth-sandbox.sevengoslings.net',
  58. requestTokenUrl: 'http://oauth-sandbox.sevengoslings.net/request_token',
  59. authorizationUrl: 'http://oauth-sandbox.sevengoslings.net/authorize',
  60. accessTokenUrl: 'http://oauth-sandbox.sevengoslings.net/access_token'
  61. });
  62. asyncTest("OAuth Authorise", function() {
  63. oauth.fetchRequestToken(function (url) {
  64. var windowObjectReference = window.open(url, 'authorise');
  65. var mask = document.createElement('div');
  66. mask.setAttribute('id', 'mask');
  67. 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>';
  68. document.body.appendChild(mask);
  69. var button = document.getElementById('start-app-button');
  70. button.onclick = function() {
  71. var code = document.getElementById('verification').value;
  72. oauth.setVerifier(code);
  73. oauth.fetchAccessToken(function(data){
  74. ok(true);
  75. start();
  76. }, function (data) {console.log(data)});
  77. };
  78. }, function (data) {console.log(data)});
  79. });
  80. asyncTest("OAuth 3-Legged Request", function() {
  81. oauth.get('http://oauth-sandbox.sevengoslings.net/three_legged', function (data) {
  82. start();
  83. ok(data.text.search('SUCCESS! This is a 3-legged call from the `jsOAuth2` consumer which was made by `bytespider`.'), 'Request success');
  84. });
  85. });