Skip to content

Commit 2f2be3d

Browse files
Split unrelated assertions in separate tests for cookie value encoding
Several assertions in a single test is not useful, if one test fails it makes the others fail too. Each test is expected to be declared in its own "test" call, qunit doesn't provide any way to filter a single assertion.
1 parent 5bb0e05 commit 2f2be3d

File tree

1 file changed

+70
-56
lines changed

1 file changed

+70
-56
lines changed

test/encoding.js

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,200 @@
1-
module('encoding', lifecycle);
1+
module('cookie-value encoding', lifecycle);
22

3-
test('Handling quotes in the cookie value for read and write', function () {
4-
expect(3);
5-
6-
Cookies.set('quote', '"');
7-
strictEqual(Cookies.get('quote'), '"', 'should print the quote character');
8-
9-
Cookies.set('without-last', '"content');
10-
strictEqual(Cookies.get('without-last'), '"content', 'should print the quote character');
11-
12-
Cookies.set('without-first', 'content"');
13-
strictEqual(Cookies.get('without-first'), 'content"', 'should print the quote character');
3+
test('cookie value with double quotes', function () {
4+
expect(1);
5+
Cookies.set('c', '"');
6+
strictEqual(Cookies.get('c'), '"', 'should print the quote character');
147
});
158

16-
test('RFC 6265 - reading cookie-octet enclosed in DQUOTE', function () {
9+
test('cookie value with double quotes in the left', function () {
1710
expect(1);
18-
document.cookie = 'c="v"';
19-
strictEqual(Cookies.get('c'), 'v', 'should decode the quotes');
11+
Cookies.set('c', '"content');
12+
strictEqual(Cookies.get('c'), '"content', 'should print the quote character');
2013
});
2114

22-
test('RFC 6265 - unallowed characters in cookie value', function () {
23-
expect(9);
15+
test('cookie value with double quotes in the right', function () {
16+
expect(1);
17+
Cookies.set('c', 'content"');
18+
strictEqual(Cookies.get('c'), 'content"', 'should print the quote character');
19+
});
2420

25-
Cookies.set('whitespace', ' ');
26-
strictEqual(Cookies.get('whitespace'), ' ', 'should handle the whitespace character');
27-
strictEqual(document.cookie, 'whitespace=%20', 'whitespace is not allowed, need to encode');
28-
Cookies.remove('whitespace');
21+
test('RFC 6265 - character not allowed in the cookie value " "', function () {
22+
expect(2);
23+
Cookies.set('c', ' ');
24+
strictEqual(Cookies.get('c'), ' ', 'should handle the whitespace character');
25+
strictEqual(document.cookie, 'c=%20', 'whitespace is not allowed, need to encode');
26+
});
2927

30-
Cookies.set('comma', ',');
31-
strictEqual(Cookies.get('comma'), ',', 'should handle the comma character');
32-
strictEqual(document.cookie, 'comma=%2C', 'comma is not allowed, need to encode');
33-
Cookies.remove('comma');
28+
test('RFC 6265 - character not allowed in the cookie value ","', function () {
29+
expect(2);
30+
Cookies.set('c', ',');
31+
strictEqual(Cookies.get('c'), ',', 'should handle the comma character');
32+
strictEqual(document.cookie, 'c=%2C', 'comma is not allowed, need to encode');
33+
});
3434

35-
Cookies.set('semicolon', ';');
36-
strictEqual(Cookies.get('semicolon'), ';', 'should handle the semicolon character');
37-
strictEqual(document.cookie, 'semicolon=%3B', 'semicolon is not allowed, need to encode');
38-
Cookies.remove('semicolon');
35+
test('RFC 6265 - character not allowed in the cookie value ";"', function () {
36+
expect(2);
37+
Cookies.set('c', ';');
38+
strictEqual(Cookies.get('c'), ';', 'should handle the semicolon character');
39+
strictEqual(document.cookie, 'c=%3B', 'semicolon is not allowed, need to encode');
40+
});
3941

40-
Cookies.set('backslash', '\\');
41-
strictEqual(Cookies.get('backslash'), '\\', 'should handle the backslash character');
42-
strictEqual(document.cookie, 'backslash=%5C', 'backslash is not allowed, need to encode');
43-
Cookies.remove('backslash');
42+
test('RFC 6265 - character not allowed in the cookie value "\\"', function () {
43+
expect(2);
44+
Cookies.set('c', '\\');
45+
strictEqual(Cookies.get('c'), '\\', 'should handle the backslash character');
46+
strictEqual(document.cookie, 'c=%5C', 'backslash is not allowed, need to encode');
47+
});
4448

45-
Cookies.set('multiple', '" ,;\\" ,;\\');
46-
strictEqual(Cookies.get('multiple'), '" ,;\\" ,;\\', 'should handle multiple special characters');
47-
Cookies.remove('multiple');
49+
test('RFC 6265 - characters not allowed in the cookie value should be replaced globally', function () {
50+
expect(2);
51+
Cookies.set('c', ';;');
52+
strictEqual(Cookies.get('c'), ';;', 'should handle multiple not allowed characters');
53+
strictEqual(document.cookie, 'c=%3B%3B', 'should replace multiple not allowed characters');
4854
});
4955

50-
test('RFC 6265 - sharp is allowed in cookie value', function () {
56+
test('RFC 6265 - character allowed in the cookie value "#"', function () {
5157
expect(2);
5258
Cookies.set('c', '#');
5359
strictEqual(Cookies.get('c'), '#', 'should handle the sharp character');
5460
strictEqual(document.cookie, 'c=#', 'sharp is allowed, should not encode');
5561
});
5662

57-
test('RFC 6265 - dollar sign is allowed in cookie value', function () {
63+
test('RFC 6265 - character allowed in the cookie value "$"', function () {
5864
expect(2);
5965
Cookies.set('c', '$');
6066
strictEqual(Cookies.get('c'), '$', 'should handle the dollar sign character');
6167
strictEqual(document.cookie, 'c=$', 'dollar sign is allowed, should not encode');
6268
});
6369

64-
test('RFC 6265 - percent is allowed in cookie value', function () {
70+
test('RFC 6265 - character allowed in the cookie value "%"', function () {
6571
expect(2);
6672
Cookies.set('c', '%');
6773
strictEqual(Cookies.get('c'), '%', 'should handle the percent character');
68-
strictEqual(document.cookie, 'c=%25', 'percent is allowed, but encode to escape');
74+
strictEqual(document.cookie, 'c=%25', 'percent is allowed, but need to be escaped');
6975
});
7076

71-
test('RFC 6265 - ampersand is allowed in cookie value', function () {
77+
test('RFC 6265 - character allowed in the cookie value "&"', function () {
7278
expect(2);
7379
Cookies.set('c', '&');
7480
strictEqual(Cookies.get('c'), '&', 'should handle the ampersand character');
7581
strictEqual(document.cookie, 'c=&', 'ampersand is allowed, should not encode');
7682
});
7783

7884
// github.com/carhartl/jquery-cookie/pull/62
79-
test('RFC 6265 - plus is allowed in cookie value', function () {
85+
test('RFC 6265 - character allowed in the cookie value "+"', function () {
8086
expect(2);
8187
Cookies.set('c', '+');
8288
strictEqual(Cookies.get('c'), '+', 'should handle the plus character');
8389
strictEqual(document.cookie, 'c=+', 'plus is allowed, should not encode');
8490
});
8591

86-
test('RFC 6265 - colon is allowed in cookie value', function () {
92+
test('RFC 6265 - character allowed in the cookie value ":"', function () {
8793
expect(2);
8894
Cookies.set('c', ':');
8995
strictEqual(Cookies.get('c'), ':', 'should handle the colon character');
9096
strictEqual(document.cookie, 'c=:', 'colon is allowed, should not encode');
9197
});
9298

93-
test('RFC 6265 - less-than is allowed in cookie value', function () {
99+
test('RFC 6265 - character allowed in the cookie value "<"', function () {
94100
expect(2);
95101
Cookies.set('c', '<');
96102
strictEqual(Cookies.get('c'), '<', 'should handle the less-than character');
97103
strictEqual(document.cookie, 'c=<', 'less-than is allowed, should not encode');
98104
});
99105

100-
test('RFC 6265 - greater-than is allowed in cookie value', function () {
106+
test('RFC 6265 - character allowed in the cookie value ">"', function () {
101107
expect(2);
102108
Cookies.set('c', '>');
103109
strictEqual(Cookies.get('c'), '>', 'should handle the greater-than character');
104110
strictEqual(document.cookie, 'c=>', 'greater-than is allowed, should not encode');
105111
});
106112

107-
test('RFC 6265 - equal sign is allowed in cookie value', function () {
113+
test('RFC 6265 - character allowed in the cookie value "="', function () {
108114
expect(2);
109115
Cookies.set('c', '=');
110116
strictEqual(Cookies.get('c'), '=', 'should handle the equal sign character');
111117
strictEqual(document.cookie, 'c==', 'equal sign is allowed, should not encode');
112118
});
113119

114-
test('RFC 6265 - slash is allowed in cookie value', function () {
120+
test('RFC 6265 - character allowed in the cookie value "/"', function () {
115121
expect(2);
116122
Cookies.set('c', '/');
117123
strictEqual(Cookies.get('c'), '/', 'should handle the slash character');
118124
strictEqual(document.cookie, 'c=/', 'slash is allowed, should not encode');
119125
});
120126

121-
test('RFC 6265 - question mark is allowed in cookie value', function () {
127+
test('RFC 6265 - character allowed in the cookie value "?"', function () {
122128
expect(2);
123129
Cookies.set('c', '?');
124130
strictEqual(Cookies.get('c'), '?', 'should handle the question mark character');
125131
strictEqual(document.cookie, 'c=?', 'question mark is allowed, should not encode');
126132
});
127133

128-
test('RFC 6265 - at is allowed in cookie value', function () {
134+
test('RFC 6265 - character allowed in the cookie value "@"', function () {
129135
expect(2);
130136
Cookies.set('c', '@');
131137
strictEqual(Cookies.get('c'), '@', 'should handle the at character');
132138
strictEqual(document.cookie, 'c=@', 'at is allowed, should not encode');
133139
});
134140

135-
test('RFC 6265 - opening square bracket is allowed in cookie value', function () {
141+
test('RFC 6265 - character allowed in the cookie value "["', function () {
136142
expect(2);
137143
Cookies.set('c', '[');
138144
strictEqual(Cookies.get('c'), '[', 'should handle the opening square bracket character');
139145
strictEqual(document.cookie, 'c=[', 'opening square bracket is allowed, should not encode');
140146
});
141147

142-
test('RFC 6265 - closing square bracket is allowed in cookie value', function () {
148+
test('RFC 6265 - character allowed in the cookie value "]"', function () {
143149
expect(2);
144150
Cookies.set('c', ']');
145151
strictEqual(Cookies.get('c'), ']', 'should handle the closing square bracket character');
146152
strictEqual(document.cookie, 'c=]', 'closing square bracket is allowed, should not encode');
147153
});
148154

149-
test('RFC 6265 - caret is allowed in cookie value', function () {
155+
test('RFC 6265 - character allowed in the cookie value "^"', function () {
150156
expect(2);
151157
Cookies.set('c', '^');
152158
strictEqual(Cookies.get('c'), '^', 'should handle the caret character');
153159
strictEqual(document.cookie, 'c=^', 'caret is allowed, should not encode');
154160
});
155161

156-
test('RFC 6265 - grave accent is allowed in cookie value', function () {
162+
test('RFC 6265 - character allowed in the cookie value "`"', function () {
157163
expect(2);
158164
Cookies.set('c', '`');
159165
strictEqual(Cookies.get('c'), '`', 'should handle the grave accent character');
160166
strictEqual(document.cookie, 'c=`', 'grave accent is allowed, should not encode');
161167
});
162168

163-
test('RFC 6265 - opening curly bracket is allowed in cookie value', function () {
169+
test('RFC 6265 - character allowed in the cookie value "{"', function () {
164170
expect(2);
165171
Cookies.set('c', '{');
166172
strictEqual(Cookies.get('c'), '{', 'should handle the opening curly bracket character');
167173
strictEqual(document.cookie, 'c={', 'opening curly bracket is allowed, should not encode');
168174
});
169175

170-
test('RFC 6265 - closing curly bracket is allowed in cookie value', function () {
176+
test('RFC 6265 - character allowed in the cookie value "}"', function () {
171177
expect(2);
172178
Cookies.set('c', '}');
173179
strictEqual(Cookies.get('c'), '}', 'should handle the closing curly bracket character');
174180
strictEqual(document.cookie, 'c=}', 'closing curly bracket is allowed, should not encode');
175181
});
176182

177-
test('RFC 6265 - pipe is allowed in cookie value', function () {
183+
test('RFC 6265 - character allowed in the cookie value "|"', function () {
178184
expect(2);
179185
Cookies.set('c', '|');
180186
strictEqual(Cookies.get('c'), '|', 'should handle the pipe character');
181187
strictEqual(document.cookie, 'c=|', 'pipe is allowed, should not encode');
182188
});
183189

190+
test('RFC 6265 - characters allowed in the cookie value should not be encoded globally', function () {
191+
expect(1);
192+
Cookies.set('c', '{{');
193+
strictEqual(document.cookie, 'c={{', 'should not encode all the character occurrences');
194+
});
195+
196+
module('cookie-name encoding', lifecycle);
197+
184198
test('RFC 6265 - unallowed characters in cookie-name', function () {
185199
expect(38);
186200

0 commit comments

Comments
 (0)