test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var hmacsign = require('./index').hmacsign
  2. , assert = require('assert')
  3. , qs = require('querystring')
  4. ;
  5. // Tests from Twitter documentation https://dev.twitter.com/docs/auth/oauth
  6. var reqsign = hmacsign('POST', 'https://api.twitter.com/oauth/request_token',
  7. { oauth_callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11'
  8. , oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g'
  9. , oauth_nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk'
  10. , oauth_signature_method: 'HMAC-SHA1'
  11. , oauth_timestamp: '1272323042'
  12. , oauth_version: '1.0'
  13. }, "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98")
  14. console.log(reqsign)
  15. console.log('8wUi7m5HFQy76nowoCThusfgB+Q=')
  16. assert.equal(reqsign, '8wUi7m5HFQy76nowoCThusfgB+Q=')
  17. var accsign = hmacsign('POST', 'https://api.twitter.com/oauth/access_token',
  18. { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g'
  19. , oauth_nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8'
  20. , oauth_signature_method: 'HMAC-SHA1'
  21. , oauth_token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc'
  22. , oauth_timestamp: '1272323047'
  23. , oauth_verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY'
  24. , oauth_version: '1.0'
  25. }, "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98", "x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA")
  26. console.log(accsign)
  27. console.log('PUw/dHA4fnlJYM6RhXk5IU/0fCc=')
  28. assert.equal(accsign, 'PUw/dHA4fnlJYM6RhXk5IU/0fCc=')
  29. var upsign = hmacsign('POST', 'http://api.twitter.com/1/statuses/update.json',
  30. { oauth_consumer_key: "GDdmIQH6jhtmLUypg82g"
  31. , oauth_nonce: "oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y"
  32. , oauth_signature_method: "HMAC-SHA1"
  33. , oauth_token: "819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw"
  34. , oauth_timestamp: "1272325550"
  35. , oauth_version: "1.0"
  36. , status: 'setting up my twitter 私のさえずりを設定する'
  37. }, "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98", "J6zix3FfA9LofH0awS24M3HcBYXO5nI1iYe8EfBA")
  38. console.log(upsign)
  39. console.log('yOahq5m0YjDDjfjxHaXEsW9D+X0=')
  40. assert.equal(upsign, 'yOahq5m0YjDDjfjxHaXEsW9D+X0=')
  41. // example in rfc5849
  42. var params = qs.parse('b5=%3D%253D&a3=a&c%40=&a2=r%20b' + '&' + 'c2&a3=2+q')
  43. params.oauth_consumer_key = '9djdj82h48djs9d2'
  44. params.oauth_token = 'kkk9d7dh3k39sjv7'
  45. params.oauth_nonce = '7d8f3e4a'
  46. params.oauth_signature_method = 'HMAC-SHA1'
  47. params.oauth_timestamp = '137131201'
  48. var rfc5849sign = hmacsign('POST', 'http://example.com/request',
  49. params, "j49sk3j29djd", "dh893hdasih9")
  50. console.log(rfc5849sign)
  51. console.log('r6/TJjbCOr97/+UU0NsvSne7s5g=')
  52. assert.equal(rfc5849sign, 'r6/TJjbCOr97/+UU0NsvSne7s5g=')