Skip to content

Commit

Permalink
Add test for manual built oauth_body_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
simov committed Apr 22, 2015
1 parent adb3eb7 commit 3f55a40
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tests/test-oauth.js
Expand Up @@ -6,6 +6,7 @@ var oauth = require('oauth-sign')
, path = require('path')
, request = require('../index')
, tape = require('tape')
, crypto = require('crypto')

function getSignature(r) {
var sign
Expand Down Expand Up @@ -528,7 +529,33 @@ tape('body transport_method with prexisting body params', function(t) {
})
})

tape('body_hash integrity check', function(t) {
tape('body_hash manual built', function(t) {
function buildBodyHash (body) {
var shasum = crypto.createHash('sha1')
shasum.update(body || '')
var sha1 = shasum.digest('hex')
return new Buffer(sha1).toString('base64')
}

var json = {foo: 'bar'}
var r = request.post(
{ url: 'http://example.com'
, oauth:
{ consumer_secret: 'consumer_secret'
, body_hash: buildBodyHash(JSON.stringify(json))
}
, json: json
})

process.nextTick(function() {
var body_hash = r.headers.Authorization.replace(/.*oauth_body_hash="([^"]+)".*/, '$1')
t.equal('YTVlNzQ0ZDAxNjQ1NDBkMzNiMWQ3ZWE2MTZjMjhmMmZhOTdlNzU0YQ%3D%3D', body_hash)
r.abort()
t.end()
})
})

tape('body_hash automatic built', function(t) {
var r = request.post(
{ url: 'http://example.com'
, oauth:
Expand Down

0 comments on commit 3f55a40

Please sign in to comment.