Files

77 lines
3.7 KiB
Lua

--[[--
moonsource_legadorule.rule / url 离线用例:各规则模式、@put/@get、正则替换、JS 拒绝、URL 补全与构造。
@module tests.legadorule.rule_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch
local Assert = require("support.assert")
local Rule = require("moonsource_legadorule.rule")
local Url = require("moonsource_legadorule.url")
local Unsupported = require("moonsource_legadorule.unsupported")
local HTML = '<div class="lis"><ul><li><a href="/b/1">书一</a><span>甲</span></li><li><a href="/b/2">书二</a></li></ul></div>'
-- 默认 JSoup 模式:列表、文本、绝对链接、下标、正则替换。
do
local an = Rule.new():setContent(HTML, "http://x.com/")
local items = an:getElements(".lis li")
Assert.len(items, 2)
local item = Rule.new():setContent(items[1], "http://x.com/")
Assert.eq(item:getString("a@text"), "书一")
Assert.eq(item:getString("a@href", nil, true), "http://x.com/b/1")
Assert.eq(an:getString("li.1@a@text"), "书二")
Assert.eq(an:getString("li.0@a@text##书##本"), "本一")
Assert.eq(an:getString("tag.a!0@text"), "书二")
end
-- XPath 与 JsonPath。
do
local an = Rule.new():setContent(HTML, "http://x.com/")
Assert.len(an:getElements("//li"), 2)
Assert.eq(an:getString("//li[2]/a/text()"), "书二")
local js = Rule.new():setContent('{"a":{"list":[{"n":"x","v":1},{"n":"y","v":2}],"t":"hi"}}', "http://x.com/")
Assert.eq(js:getString("$.a.t"), "hi")
Assert.eq(js:getString("$.a.list[-1].v"), "2")
local rows = js:getElements("$.a.list[?(@.v>1)]")
Assert.len(rows, 1)
Assert.eq(Rule.new():setContent(rows[1]):getString("$.n"), "y")
Assert.eq(js:getString("$..n"), "x\ny")
end
-- 正则列表模式与 $n 组引用;@put / @get。
do
local an = Rule.new():setContent("a1:一;a2:二;", "http://x.com/")
local rows = an:getElements(":a(\\d):(.)")
Assert.len(rows, 2)
Assert.eq(Rule.new():setContent(rows[2]):getString("$1-$2"), "2-二")
local put = Rule.new():setContent(HTML)
put:getString("li.0@a@text@put:{k:\"li.1@a@text\"}")
Assert.eq(put:get("k"), "书二")
end
-- JS / WebView 规则一律拒绝。
do
Assert.errors(function() Rule.new():setContent(HTML):getString("@js:result") end)
local ok, err = pcall(Rule.new():setContent(HTML).getString, Rule.new():setContent(HTML), "<js>1</js>")
Assert.is_false(ok)
Assert.eq(Unsupported.message(err), Unsupported.MESSAGE)
end
-- URL 补全与构造。
do
Assert.eq(Url.absolute("https://a.com/x/y.html", "../z.html"), "https://a.com/z.html")
Assert.eq(Url.absolute("https://a.com/x/", "//b.com/p"), "https://b.com/p")
Assert.eq(Url.absolute("https://a.com/x/", "javascript:void(0)"), "")
local spec = Url.build('/s,{"body":"kw={{key}}&p={{page}}","method":"POST"}', { sourceUrl = "https://a.com/", key = "你好", page = 2 })
Assert.eq(spec.url, "https://a.com/s")
Assert.eq(spec.method, "POST")
Assert.eq(spec.body, "kw=%E4%BD%A0%E5%A5%BD&p=2")
Assert.eq(Url.build("/search/{{key}}", { sourceUrl = "https://a.com/", key = "我的 书" }).url,
"https://a.com/search/%E6%88%91%E7%9A%84%20%E4%B9%A6")
Assert.eq(Url.build("/s/%E6%88%91?q=我", { sourceUrl = "https://a.com/" }).url, "https://a.com/s/%E6%88%91?q=%E6%88%91")
Assert.eq(Url.build("/l/<a,b,c>.html", { sourceUrl = "https://a.com/", page = 9 }).url, "https://a.com/l/c.html")
Assert.errors(function() Url.build('/s,{"webView":true}', { sourceUrl = "https://a.com/" }) end)
Assert.errors(function() Url.build("/s?q={{java.get('x')}}", { sourceUrl = "https://a.com/" }) end)
end