|
| 1 | +local urlencode = require 'urlencode' |
| 2 | +print ('VERSION: ' .. urlencode._VERSION) |
| 3 | + |
| 4 | +local unreserved = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_.~-' |
| 5 | + |
| 6 | +local test_cases = {} |
| 7 | + |
| 8 | +table.insert(test_cases, {input = '', encoded = '', comment = 'empty string'}) |
| 9 | +table.insert(test_cases, {input = ' ', encoded = '+', comment = 'U+0020 SPACE'}) |
| 10 | +table.insert(test_cases, {input = unreserved, encoded = unreserved, comment = 'unreserved characters'}) |
| 11 | +table.insert(test_cases, {input = 'å', encoded = '%C3%A5', comment = 'U+00E5 in native encoding'}) |
| 12 | +table.insert(test_cases, {input = "hello こんにちは", encoded = 'hello+%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF', comment = 'with multi-byte characters (Japanese)'}) |
| 13 | +table.insert(test_cases, {input = 'ttテeeストstst', encoded = 'tt%E3%83%86ee%E3%82%B9%E3%83%88stst', comment = 'native string'}) |
| 14 | + |
| 15 | + |
| 16 | +for cc = 0x01, 0x1F do |
| 17 | + local c = string.char(cc) |
| 18 | + if not unreserved:find(c, 0, true) then |
| 19 | + local code = '%' .. string.format("%02X", cc) |
| 20 | + local comment = string.format("ordinal %d", cc) |
| 21 | + table.insert(test_cases, { |
| 22 | + input = c, encoded = code, comment = comment |
| 23 | + }) |
| 24 | + end |
| 25 | +end |
| 26 | + |
| 27 | +for cc = 0x21, 0xFF do |
| 28 | + local c = string.char(cc) |
| 29 | + if not unreserved:find(c, 0, true) then |
| 30 | + local code = '%' .. string.format("%02X", cc) |
| 31 | + local comment = string.format("ordinal %d", cc) |
| 32 | + table.insert(test_cases, { |
| 33 | + input = c, encoded = code, comment = comment |
| 34 | + }) |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +for i, c in ipairs(test_cases) do |
| 39 | + local encoded = urlencode.encode_url(c.input) |
| 40 | + assert(encoded == c.encoded, 'enc: ' .. c.comment) |
| 41 | + assert(urlencode.decode_url(encoded) == c.input, 'dec: ' .. c.comment) |
| 42 | +end |
| 43 | + |
| 44 | +do |
| 45 | + local encoded = urlencode.encode_url(string.char(0x00)) |
| 46 | + assert(encoded == '') |
| 47 | + assert(urlencode.decode_url(encoded) == '') |
| 48 | +end |
| 49 | + |
| 50 | +do |
| 51 | + local encoded = urlencode.encode_url('test test' .. string.char(0x00) .. ' test') |
| 52 | + assert(encoded == 'test+test') |
| 53 | + assert(urlencode.decode_url(encoded) == 'test test') |
| 54 | +end |
| 55 | + |
| 56 | +print 'OK' |
| 57 | + |
0 commit comments