Skip to content

Commit

Permalink
Throw if the charset is not one of the two supported values or undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 27, 2018
1 parent 228bf8e commit 8deaab7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ var parseValues = function parseQueryStringValues(str, options) {
var skipIndex = -1; // Keep track of where the utf8 sentinel was found
var i;

if (charset !== undefined && charset !== 'utf-8' && charset !== 'iso-8859-1') {
throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined');
}
if (options.utf8Sentinel) {
for (i = 0; i < parts.length; ++i) {
if (parts[i].indexOf('utf8=') === 0) {
Expand Down
7 changes: 7 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@ test('parse()', function (t) {
st.end();
});

t.test('throws if an invalid charset is specified', function (st) {
st['throws'](function () {
qs.parse('a=b', { charset: 'foobar' });
}, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
st.end();
});

t.test('parses an iso-8859-1 string if asked to', function (st) {
st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
st.end();
Expand Down

0 comments on commit 8deaab7

Please sign in to comment.