Skip to content

Commit

Permalink
jshttp#5 ignore escape error and return original value better than th…
Browse files Browse the repository at this point in the history
…row the error.
  • Loading branch information
fengmk2 committed Oct 29, 2012
1 parent e86c424 commit 619f6d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ var parse = function(str) {
return obj;
};

var encode = encodeURIComponent;
var decode = decodeURIComponent;
var encode = function(val) {
try {
return encodeURIComponent(val);
} catch (e) {
return val;
}
};
var decode = function(val) {
try {
return decodeURIComponent(val);
} catch (e) {
return val;
}
};

module.exports.serialize = serialize;
module.exports.parse = parse;
3 changes: 3 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ test('escaping', function() {
cookie.parse('email=%20%22%2c%3b%2f'));
});

test('ignore escaping error and return original value', function() {
assert.deepEqual({ foo: '%1', bar: 'bar' }, cookie.parse('foo=%1;bar=bar'));
});
5 changes: 5 additions & 0 deletions test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ test('escaping', function() {
assert.deepEqual('cat=%2B%20', cookie.serialize('cat', '+ '));
});

test('ignore escaping error and return original value', function() {
var s = String.fromCharCode(0xDFFF);
assert.deepEqual('cat=' + s, cookie.serialize('cat', s));
});

test('parse->serialize', function() {

assert.deepEqual({ cat: 'foo=123&name=baz five' }, cookie.parse(
Expand Down

0 comments on commit 619f6d7

Please sign in to comment.