Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from tomhughes/gh-pages
Browse files Browse the repository at this point in the history
Encode parameters correctly when building an OAuth base string
  • Loading branch information
tmcw committed Apr 27, 2013
2 parents f771a01 + 866a010 commit c66a366
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ var ohauth = {};

ohauth.qsString = function(obj) {
return Object.keys(obj).sort().map(function(key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(obj[key]);
return ohauth.percentEncode(key) + '=' +
ohauth.percentEncode(obj[key]);
}).join('&');
};

ohauth.stringQs = function(str) {
return str.split('&').reduce(function(obj, pair){
var parts = pair.split('=');
obj[parts[0]] = (null === parts[1]) ?
obj[decodeURIComponent(parts[0])] = (null === parts[1]) ?
'' : decodeURIComponent(parts[1]);
return obj;
}, {});
Expand Down
6 changes: 6 additions & 0 deletions test/ohauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ describe('ohauth', function() {
it('turns an object into a querystring', function() {
expect(ohauth.qsString({ foo: 1 })).to.eql('foo=1');
});
it('escapes special characters', function() {
expect(ohauth.qsString({ "!'*()": "!'*()" })).to.eql('%21%27%2A%28%29=%21%27%2A%28%29');
});
});

describe('#stringQs', function() {
it('turns a querystring into an object', function() {
expect(ohauth.stringQs('foo=1')).to.eql({ foo: 1 });
});
it('handles special characters', function() {
expect(ohauth.stringQs('%21%27%2A%28%29=%21%27%2A%28%29')).to.eql({ "!'*()": "!'*()" });
});
});

describe('#nonce', function() {
Expand Down

0 comments on commit c66a366

Please sign in to comment.