summaryrefslogtreecommitdiffstats
path: root/test/lua/unit/url.lua
blob: 6d09fcb3f31bb72ca3dbab9e92fb81b721060cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- URL parser tests

context("URL check functions", function()
  local mpool = require("rspamd_mempool")
  local url = require("rspamd_url")
  local logger = require("rspamd_logger")
  local ffi = require("ffi")
  ffi.cdef[[
  void rspamd_url_init (const char *tld_file);
  unsigned ottery_rand_range(unsigned top);
  ]]
  
  local pool = mpool.create()
  local test_dir = string.gsub(debug.getinfo(1).source, "^@(.+/)[^/]+$", "%1")

  ffi.C.rspamd_url_init(string.format('%s/%s', test_dir, "test_tld.dat"))

  test("Extract urls from text", function()
    local cases = {
      {"test.com text", {"test.com", nil}},
      {"test.com. text", {"test.com", nil}},
      {"mailto:A.User@example.com text", {"example.com", "A.User"}},
      {"http://Тест.Рф:18 text", {"тест.рф", nil}},
      {"http://user:password@тест2.РФ:18 text", {"тест2.рф", "user"}},
      {"somebody@example.com", {"example.com", "somebody"}},
      {"https://127.0.0.1/abc text", {"127.0.0.1", nil}},
      {"https://127.0.0.1 text", {"127.0.0.1", nil}},
      {"https://[::1]:1", {"::1", nil}},
      {"https://user:password@[::1]:1", {"::1", nil}},
      {"https://user:password@[::1]", {"::1", nil}},
      {"https://user:password@[::1]/1", {"::1", nil}},
    }

    for _,c in ipairs(cases) do
      local res = url.create(pool, c[1])
      
      assert_not_nil(res, "cannot parse " .. c[1])
      local t = res:to_table()
      --local s = logger.slog("%1 -> %2", c[1], t)
      --print(s)
      assert_not_nil(t, "cannot convert to table " .. c[1])
      assert_equal(c[2][1], t['host'])
      
      if c[2][2] then
        assert_equal(c[2][2], t['user'])
      end
    end
  end)
  
  pool:destroy()
end)