Skip to content

Commit

Permalink
Merge pull request #4 from s3u/master
Browse files Browse the repository at this point in the history
Use encodeURIComponent in stead of encodeURI
  • Loading branch information
prabhakhar committed Nov 2, 2011
2 parents e775c7e + 60b4537 commit 5293087
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions modules/uri-template/lib/uri-template.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions modules/uri-template/peg/uri-template.peg
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ URITemplate = c:( literal / expression )* {
var j;
if(str.constructor === Array) {
for(j = 0; j < str.length; j++) {
str[j] = str[j] + (encode ? encodeURI(val) : val);
str[j] = str[j] + (encode ? encodeURIComponent(val) : val);
}
}
else {
str = str + (encode ? encodeURI(val) : val);
str = str + (encode ? encodeURIComponent(val) : val);
}
return str;
}
Expand Down
22 changes: 17 additions & 5 deletions modules/uri-template/test/uri-template-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module.exports = {
p1: 'v1',
p2: ['v2-1', 'v2-2']
});
test.equals(uri, 'http://www.subbu.org?p1=v1&p2=v2-1,v2-2');
test.equals(uri, 'http://www.subbu.org?p1=v1&p2=v2-1%2Cv2-2');
test.done();
},

Expand Down Expand Up @@ -232,7 +232,7 @@ module.exports = {
});
test.ok(_.isArray(uri), 'Expected an array');
test.equals(uri.length, 2, 'Expected two URIs');
test.equals(uri[0], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-1,v3-2');
test.equals(uri[0], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-1%2Cv3-2');
test.equals(uri[1], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-3');
test.done();
},
Expand All @@ -247,8 +247,8 @@ module.exports = {
});
test.ok(_.isArray(uri), 'Expected an array');
test.equals(uri.length, 3, 'Expected two URIs');
test.equals(uri[0], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-1,v3-2');
test.equals(uri[1], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-3,v3-4');
test.equals(uri[0], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-1%2Cv3-2');
test.equals(uri[1], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-3%2Cv3-4');
test.equals(uri[2], 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-5');
test.done();
},
Expand All @@ -261,7 +261,19 @@ module.exports = {
p2: 'v2',
p3: ['v3-1', 'v3-2', 'v3-3']
});
test.equals(uri, 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-1,v3-2,v3-3');
test.equals(uri, 'http://www.subbu.org?p1=v1&p2=v2&p3=v3-1%2Cv3-2%2Cv3-3');
test.done();
},

'encode': function(test) {
var str = "http://www.foo.com?p1={p1}&p2={p2}&p3={5|p3}";
var template = uriTemplate.parse(str);
var uri = template.format({
p1: 'this is a value',
p2: 'this+is+a+value',
p3: 'this/is/another/value'
});
test.equals(uri, 'http://www.foo.com?p1=this%20is%20a%20value&p2=this%2Bis%2Ba%2Bvalue&p3=this%2Fis%2Fanother%2Fvalue');
test.done();
}
};

0 comments on commit 5293087

Please sign in to comment.