context("HTML URL extraction", function() local rspamd_task = require("rspamd_task") local logger = require("rspamd_logger") test("Basic URL extraction from simple HTML", function() local msg = [[ From: test@example.com To: nobody@example.com Subject: test Content-Type: text/html
Click here ]] local res, task = rspamd_task.load_from_string(msg, rspamd_config) assert_true(res, "failed to load message") task:process_message() local urls = task:get_html_urls() assert_not_nil(urls, "should extract URLs") -- Check structure local found_url = false for part_id, url_list in pairs(urls) do assert_true(type(url_list) == "table", "URL list should be a table") for i, url_info in ipairs(url_list) do assert_not_nil(url_info.url, "should have url field") assert_not_nil(url_info.attr, "should have attr field") assert_not_nil(url_info.tag, "should have tag field") if url_info.url == "http://example.com/test" then assert_equal(url_info.attr, "href", "should be href attribute") assert_equal(url_info.tag, "a", "should be tag") found_url = true end end end assert_true(found_url, "should find the expected URL") task:destroy() end) test("Multiple URLs in same HTML part", function() local msg = [[ From: test@example.com To: nobody@example.com Subject: test Content-Type: text/html Link 1 Link 2
]]
local res, task = rspamd_task.load_from_string(msg, rspamd_config)
assert_true(res, "failed to load message")
task:process_message()
local urls = task:get_html_urls()
assert_not_nil(urls, "should extract URLs")
-- Count URLs
local url_count = 0
local found_urls = {}
for part_id, url_list in pairs(urls) do
for i, url_info in ipairs(url_list) do
url_count = url_count + 1
found_urls[url_info.url] = url_info
end
end
assert_equal(url_count, 3, "should have found 3 URLs")
assert_not_nil(found_urls["http://example.com/link1"], "should find link1")
assert_not_nil(found_urls["http://example.com/link2"], "should find link2")
assert_not_nil(found_urls["http://example.com/image.jpg"], "should find image")
-- Check attributes
assert_equal(found_urls["http://example.com/link1"].attr, "href")
assert_equal(found_urls["http://example.com/link1"].tag, "a")
assert_equal(found_urls["http://example.com/image.jpg"].attr, "src")
assert_equal(found_urls["http://example.com/image.jpg"].tag, "img")
task:destroy()
end)
test("Non-HTML parts return nil", function()
local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/plain
This is plain text with http://example.com/test
]]
local res, task = rspamd_task.load_from_string(msg, rspamd_config)
assert_true(res, "failed to load message")
task:process_message()
local urls = task:get_html_urls()
-- Should return nil for plain text
assert_nil(urls, "should return nil for non-HTML parts")
task:destroy()
end)
test("Empty HTML returns nil", function()
local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html
]]
local res, task = rspamd_task.load_from_string(msg, rspamd_config)
assert_true(res, "failed to load message")
task:process_message()
local urls = task:get_html_urls()
-- Should return nil for empty HTML
assert_nil(urls, "should return nil for empty HTML")
task:destroy()
end)
test("HTML without URLs returns nil", function()
local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html
Just some text without any links
]] local res, task = rspamd_task.load_from_string(msg, rspamd_config) assert_true(res, "failed to load message") task:process_message() local urls = task:get_html_urls() -- Should return nil when no URLs found assert_nil(urls, "should return nil when no URLs found") task:destroy() end) test("Data URI scheme is skipped", function() local msg = [[ From: test@example.com To: nobody@example.com Subject: test Content-Type: text/html