Debian Codegraph @

mdurl/test/encode.js

raw
1
2 'use strict';
3
4
5 var equals = require('assert').strictEqual;
6 var encode = require('../').encode;
7
8 describe('encode', function() {
9 it('should encode percent', function() {
10 equals(encode('%%%'), '%25%25%25');
11 });
12
13 it('should encode control chars', function() {
14 equals(encode('\r\n'), '%0D%0A');
15 });
16
17 it('should not encode parts of an url', function() {
18 equals(encode('?#'), '?#');
19 });
20
21 it('should not encode []^ - commonmark tests', function() {
22 equals(encode('[]^'), '%5B%5D%5E');
23 });
24
25 it('should encode spaces', function() {
26 equals(encode('my url'), 'my%20url');
27 });
28
29 it('should encode unicode', function() {
30 equals(encode('φου'), '%CF%86%CE%BF%CF%85');
31 });
32
33 it('should encode % if it doesn\'t start a valid escape seq', function() {
34 equals(encode('%FG'), '%25FG');
35 });
36
37 it('should preserve non-utf8 encoded characters', function() {
38 equals(encode('%00%FF'), '%00%FF');
39 });
40
41 it('should encode characters on the cache borders', function() {
42 // protects against off-by-one in cache implementation
43 equals(encode('\x00\x7F\x80'), '%00%7F%C2%80');
44 });
45
46 describe('arguments', function() {
47 it('encode(string, unescapedSet)', function() {
48 equals(encode('!@#$', '@$'), '%21@%23$');
49 });
50
51 it('encode(string, keepEscaped=true)', function() {
52 equals(encode('%20%2G', true), '%20%252G');
53 });
54
55 it('encode(string, keepEscaped=false)', function() {
56 equals(encode('%20%2G', false), '%2520%252G');
57 });
58
59 it('encode(string, unescapedSet, keepEscaped)', function() {
60 equals(encode('!@%25', '@', false), '%21@%2525');
61 });
62 });
63
64 describe('surrogates', function() {
65 it('bad surrogates (high)', function() {
66 equals(encode('\uD800foo'), '%EF%BF%BDfoo');
67 equals(encode('foo\uD800'), 'foo%EF%BF%BD');
68 });
69
70 it('bad surrogates (low)', function() {
71 equals(encode('\uDD00foo'), '%EF%BF%BDfoo');
72 equals(encode('foo\uDD00'), 'foo%EF%BF%BD');
73 });
74
75 it('valid one', function() {
76 equals(encode('\uD800\uDD00'), '%F0%90%84%80');
77 });
78 });
79 });