소스 검색

Improve the Lua test: Test 'require' and escape all characters correctly

bel 11 년 전
부모
커밋
b7760846b5
2개의 변경된 파일69개의 추가작업 그리고 10개의 파일을 삭제
  1. 37 0
      test/html_esc.lua
  2. 32 10
      test/page4.lua

+ 37 - 0
test/html_esc.lua

@@ -0,0 +1,37 @@
+htmlEscape = {    "☺", "☻", "♥", "♦", "♣", "♠", "•", -- ASCII 1-7 (symbols for control characters, see code page 437)
+      "◘", "○", "◙", "♂", "♀", "♪", "♫", "☼", -- ASCII 8-15
+      "►", "◄", "↕", "‼", "¶", "§", "▬", "↨", -- ASCII 16-23
+      "↑", "↓", "↨", "←", "∟", "→", "▲", "▼", -- ASCII 24-31
+      " ",        "!",        """,   "#",        "$",        "%",        "&",    "'",        -- ASCII 32-39
+      "(",        ")",        "*",        "+",        ",",        "-",        ".",        "/",        -- ASCII 40-47
+      "0",        "1",        "2",        "3",        "4",        "5",        "6",        "7",        -- ASCII 48-55
+      "8",        "9",        ":",        ";",        "<",     "=",        ">",     "?",        -- ASCII 56-63
+      "@",        "A",        "B",        "C",        "D",        "E",        "F",        "G",        -- ASCII 64-71
+      "H",        "I",        "J",        "K",        "L",        "M",        "N",        "O",        -- ASCII 72-79
+      "P",        "Q",        "R",        "S",        "T",        "U",        "V",        "W",        -- ASCII 80-87
+      "X",        "Y",        "Z",        "[",        "\\",       "]",        "^",        "_",        -- ASCII 88-95
+      "`",        "a",        "b",        "c",        "d",        "e",        "f",        "g",        -- ASCII 96-103
+      "h",        "i",        "j",        "k",        "l",        "m",        "n",        "o",        -- ASCII 104-111
+      "p",        "q",        "r",        "s",        "t",        "u",        "v",        "w",        -- ASCII 112-119
+      "x",        "y",        "z",        "{",        "|",        "}",        "~",        "⌂", -- ASCII 120-127
+      "Ç", "ü",   "é", "â",  "ä",   "à", "å",  "ç", -- 128-135 (dos code page 850)
+      "ê",  "ë",   "è", "ï",   "î",  "ì", "Ä",   "Å",  -- 136-143
+      "É", "æ",  "Æ",  "ô",  "ö",   "ò", "û",  "ù", -- 144-151
+      "ÿ",   "Ö",   "Ü",   "ø", "£", "Ø", "×", "ƒ", -- 152-159
+      "á", "í", "ó", "ú", "ñ", "Ñ", "ª", "º", -- 160-167
+      "¿", "®", "¬", "½", "¼", "¡", "«", "»", -- 168-175
+      "░", "▒", "▓", "│", "┤", "Á", "Â",  "À", -- 176-183
+      "©", "╣", "║", "╗", "╝", "¢",   "¥", "┐", -- 184-191
+      "└", "┴", "┬", "├", "─", "┼", "ã", "Ã", -- 192-199
+      "╚", "╔", "╩", "╦", "╠", "═", "╬", "¤", -- 200-207
+      "ð",    "Ð",    "Ê",  "Ë",   "È", "ı", "Í", "Î",  -- 208-215
+      "Ï",   "┘", "┌", "█", "▄", "¦", "Ì", "▀", -- 216-223
+      "Ó", "ß",  "Ô",  "Ò", "õ", "Õ", "µ", "þ",  -- 224-231
+      "Þ",  "Ú", "Û",  "Ù", "ý", "Ý", "¯", "´", -- 232-239
+      "≡",  "±", "‗", "¾", "¶", "§", "÷", "¸", -- 240-247
+      "°", "¨", "·", "¹", "³", "²", "■", "□",  -- 248-255 (use empty box for 255)
+};
+htmlEscape[0] = "·" -- in this table, we use a 8 bit character set, where every has a different graphical representation
+
+-- the conversion table should work as a convertion function for strings as well
+setmetatable(htmlEscape, {__call = function (tab,str) return string.gsub(str, ".", function (c) return tab[c:byte()] end) end})

+ 32 - 10
test/page4.lua

@@ -64,6 +64,29 @@ else
 end
 mg.write("\r\n")
 
+-- test 'require' of other Lua scripts
+mg.write("require test\r\n")
+script_path = mg.request_info.script_name:match("(.*)page%d*.lua")
+if type(script_path)=='string' then
+    package.path = script_path .. "?.lua;" .. package.path
+    mg.write("  Lua search path: " .. package.path .. "\r\n")
+    require "html_esc"
+    if htmlEscape then
+      for i=0,15 do
+        mg.write("  ")
+        for j=0,15 do
+            mg.write(tostring(htmlEscape[16*i+j]))
+        end
+        mg.write("\r\n")
+      end
+    else
+      mg.write("  'require' test failed\r\n")
+    end
+else
+    mg.write("  name match failed\r\n")
+end
+mg.write("\r\n")
+
 -- url_encode
 mg.write("URL encode/decode test:\r\n")
 if mg.url_encode("") == "" then
@@ -72,25 +95,24 @@ else
     mg.write("  Error: url_encode of empty string NOT OK\r\n")
 end
 raw_string = [[ !"#$%&'()*+,-./0123456789:;<=>?@]]
-mg.write("  original string: " .. raw_string .. "\r\n")
+mg.write("  original string: " .. htmlEscape(raw_string) .. "\r\n")
 url_string = mg.url_encode(raw_string):upper()
 ref_string = "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40" -- from http://www.w3schools.com/tags/ref_urlencode.asp
-mg.write("  mg-url:        " .. url_string .. "\r\n")
-mg.write("  reference url: " .. ref_string .. "\r\n")
+mg.write("  mg-url:        " .. htmlEscape(url_string) .. "\r\n")
+mg.write("  reference url: " .. htmlEscape(ref_string) .. "\r\n")
 dec_url_string = mg.url_decode(url_string)
 dec_ref_string = mg.url_decode(ref_string)
-mg.write("  decoded mg-url:        " .. dec_url_string .. "\r\n")
-mg.write("  decoded reference url: " .. dec_ref_string .. "\r\n")
+mg.write("  decoded mg-url:        " .. htmlEscape(dec_url_string) .. "\r\n")
+mg.write("  decoded reference url: " .. htmlEscape(dec_ref_string) .. "\r\n")
 dec_url_string = mg.url_decode(url_string, false)
 dec_ref_string = mg.url_decode(ref_string, false)
-mg.write("  decoded mg-url:        " .. dec_url_string .. "\r\n")
-mg.write("  decoded reference url: " .. dec_ref_string .. "\r\n")
+mg.write("  decoded mg-url:        " .. htmlEscape(dec_url_string) .. "\r\n")
+mg.write("  decoded reference url: " .. htmlEscape(dec_ref_string) .. "\r\n")
 dec_url_string = mg.url_decode(url_string, true)
 dec_ref_string = mg.url_decode(ref_string, true)
-mg.write("  decoded mg-url:        " .. dec_url_string .. "\r\n")
-mg.write("  decoded reference url: " .. dec_ref_string .. "\r\n")
+mg.write("  decoded mg-url:        " .. htmlEscape(dec_url_string) .. "\r\n")
+mg.write("  decoded reference url: " .. htmlEscape(dec_ref_string) .. "\r\n")
 mg.write("\r\n")
 
-
 -- end of page
 mg.write("</pre>\r\n</body>\r\n</html>\r\n")