Skip to content

Commit

Permalink
Tests: Extract some of the obscure strings to named vars
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 18, 2018
1 parent df77dfd commit afb4a16
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,43 +582,48 @@ test('parse()', function (t) {
st.end();
});

var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
var urlEncodedOSlashInUtf8 = '%C3%B8';
var urlEncodedNumCheckmark = '%26%2310003%3B';
var urlEncodedNumSmiley = '%26%239786%3B';

t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
st.deepEqual(qs.parse('utf8=%E2%9C%93&%C3%B8=%C3%B8', { utf8Sentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { utf8Sentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
st.end();
});

t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
st.deepEqual(qs.parse('utf8=%26%2310003%3B&%C3%B8=%C3%B8', { utf8Sentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { utf8Sentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
st.end();
});

t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
st.deepEqual(qs.parse('utf8=foo&%C3%B8=%C3%B8', { utf8Sentinel: true, charset: 'utf-8' }), { ø: 'ø' });
st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { utf8Sentinel: true, charset: 'utf-8' }), { ø: 'ø' });
st.end();
});

t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
st.deepEqual(qs.parse('utf8=%E2%9C%93&%C3%B8=%C3%B8', { utf8Sentinel: true }), { ø: 'ø' });
st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { utf8Sentinel: true }), { ø: 'ø' });
st.end();
});

t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
st.deepEqual(qs.parse('utf8=%26%2310003%3B&%C3%B8=%C3%B8', { utf8Sentinel: true }), { 'ø': 'ø' });
st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { utf8Sentinel: true }), { 'ø': 'ø' });
st.end();
});

t.test('interprets numeric entities in iso-8859-1 when the interpretNumericEntities option is given', function (st) {
st.deepEqual(qs.parse('foo=%26%239786%3B', { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
st.end();
});

t.test('does not interpret numeric entities in iso-8859-1 when the interpretNumericEntities option is not given', function (st) {
st.deepEqual(qs.parse('foo=%26%239786%3B', { charset: 'iso-8859-1' }), { foo: '☺' });
st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '☺' });
st.end();
});

t.test('does not interpret numeric entities when the charset is utf-8, even when the interpretNumericEntities option is given', function (st) {
st.deepEqual(qs.parse('foo=%26%239786%3B', { charset: 'utf-8', interpretNumericEntities: true }), { foo: '☺' });
st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '☺' });
st.end();
});

Expand Down

0 comments on commit afb4a16

Please sign in to comment.