Files
Garfield 1dd1cccb68 ✨ (kmoe): 新增 Kmoe 漫画书源,按卷下载 EPUB 阅读
书城搜索与筛选、详情、卷列表、整卷 EPUB 下载与缓存、收藏书架同步。
支持邮箱密码、浏览器授权、粘贴 Cookie 登录;下载要求 UA 与登录时一致,
会话 UA 随 Cookie 一起保存。
2026-11-27 13:00:01 +08:00

174 lines
7.4 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
moonsource_kmoe.client 离线用例:跳转归类、公开接口不带账号 Cookie、登录取 Cookie、账号校验、收藏确认。
@module tests.moonsource_kmoe.client_spec
--]]
---@diagnostic disable: need-check-nil, undefined-field, inject-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
Stubs.install()
package.loaded["json"] = { decode = require("support.json_stub").decode }
local calls, script, expired = {}, {}, {}
local function header(res, name)
local v = res and res.headers and res.headers[name]
return type(v) == "table" and v[1] or v
end
package.preload["http.request"] = function()
local function send(method, url, body, opts, cb)
local jar = opts and opts.jar
calls[#calls + 1] = { method = method, url = url, body = body, cookie = jar and jar:header(url) or nil,
ua = opts and opts.headers and opts.headers["User-Agent"] }
local step = table.remove(script, 1) or { code = 200, body = "" }
if step.code ~= 200 then
cb(nil, "HTTP " .. step.code, { code = step.code, headers = step.headers or {} })
else
cb(step.body, nil, { code = 200, headers = step.headers or {} })
end
return { cancel = function() end }
end
return {
get = function(url, opts, cb) return send("GET", url, nil, opts, cb) end,
post = function(url, body, opts, cb) return send("POST", url, body, opts, cb) end,
header = header,
headerAll = function(res, name)
local v = res and res.headers and res.headers[name]
return type(v) == "table" and v or { v }
end,
}
end
package.preload["source.auth"] = function()
return { expire = function(id, msg) expired[#expired + 1] = id; return msg end }
end
local Client = require("moonsource_kmoe.client")
local function reset(steps)
calls, script, expired = {}, steps, {}
end
-- 书单是公开接口:登录着也不带账号 Cookie;分类、排序、页码进查询串。
do
reset({ { code = 200, body = [[{"totalpage":1,"nowpage":1,"data":[]}]] } })
local books, more
Client.new("VOLSKEY=k"):listAsync({ s = "CAT*幽默", by = "score", ["end"] = "連載", lang = "chn" }, 2,
function(b, _e, m) books, more = b, m end)
Assert.len(books, 0)
Assert.is_false(more)
Assert.is_nil(calls[1].cookie)
local url = calls[1].url
Assert.matches(url, "^https://mox%.moe/data_list%.php%?")
Assert.is_true(url:find("s=CAT%2A%E5%B9%BD%E9%BB%98", 1, true) ~= nil, url)
Assert.is_true(url:find("by=score", 1, true) ~= nil)
Assert.is_true(url:find("lang=chn", 1, true) ~= nil)
Assert.is_true(url:find("&p=2", 1, true) ~= nil)
end
-- 详情带登录 Cookie;没做手机验证 → 手机验证说明;登录失效 → 标记失效;未登录 → 请先登录。
do
reset({ { code = 302, headers = { Location = "https://mox.moe/m/regphone.php" } } })
local url, err
Client.new("VOLSKEY=k"):downUrlAsync("/getdownurl.php?b=1&v=2&mobi=2&vip=0&json=1", function(u, e) url, err = u, e end)
Assert.is_nil(url)
Assert.matches(err, "手機驗證")
Assert.eq(calls[1].cookie, "VOLSKEY=k")
reset({ { code = 302, headers = { Location = "https://mox.moe/login.php" } } })
Client.new("VOLSKEY=k"):detailAsync("abc", function(_d, e) err = e end)
Assert.matches(err, "重新登录")
Assert.eq(expired[1], "kmoe")
Assert.eq(calls[1].url, "https://mox.moe/m/c/abc.htm")
reset({})
Client.new(nil):downUrlAsync("/getdownurl.php", function(_u, e) err = e end)
Stubs.flush()
Assert.matches(err, "请先登录")
Assert.len(calls, 0, "没登录不发下载请求")
Assert.len(expired, 0)
reset({ { code = 200, body = [[{"url":"/dl/x.epub","name":"x.epub"}]] } })
Client.new("VOLSKEY=k"):downUrlAsync("/getdownurl.php", function(u) url = u end)
Assert.eq(url, "https://mox.moe/dl/x.epub")
end
-- 会话 UA:带了就逐字沿用,没带用默认电脑 UA;取下载地址 403 = UA 和登录时不一致。
do
reset({ { code = 200, body = "" }, { code = 200, body = "" } })
Client.new("VOLSKEY=k", "Browser/157"):detailAsync("abc", function() end)
Client.new("VOLSKEY=k"):detailAsync("abc", function() end)
Assert.eq(calls[1].ua, "Browser/157")
Assert.eq(calls[2].ua, Client.ua)
Assert.matches(Client.ua, "Macintosh")
reset({ { code = 403 } })
local url, err
Client.new("VOLSKEY=k"):downUrlAsync("/getdownurl.php", function(u, e) url, err = u, e end)
Assert.is_nil(url)
Assert.matches(err, "浏览器和现在不一致")
end
-- 卷列表:详情页不是登录态就不请求 data_book(匿名只会拿到空列表)。
do
reset({})
local err
Client.new("VOLSKEY=k"):volumesAsync({ logged_in = false, hash = "h", bookid = "1" }, function(_c, e) err = e end)
Stubs.flush()
Assert.len(calls, 0)
Assert.matches(err, "重新登录")
end
-- 密码登录:表单字段 email / passwd;m100 + Set-Cookie 才成功;失败带出站点原话。
do
reset({ { code = 200, body = [[{"ret":0,"msg":"","msgid":"m100"}]],
headers = { ["Set-Cookie"] = { "VOLSKEY=abc; path=/; domain=.mox.moe", "VLIBSID=s1; path=/" } } } })
local cookie, err
Client.loginAsync("[email protected]", "p&w", function(c, e) cookie, err = c, e end)
Assert.is_nil(err)
Assert.is_true(cookie:find("VOLSKEY=abc", 1, true) ~= nil)
Assert.is_true(cookie:find("VLIBSID=s1", 1, true) ~= nil)
Assert.eq(calls[1].method, "POST")
Assert.eq(calls[1].url, "https://mox.moe/login_act.php")
Assert.eq(calls[1].body, "email=a%40b.c&passwd=p%26w")
Assert.is_nil(calls[1].cookie)
reset({ { code = 200, body = [[{"ret":1,"msg":"郵箱帳號或密碼錯誤。","msgid":"e400"}]] } })
Client.loginAsync("[email protected]", "x", function(c, e) cookie, err = c, e end)
Assert.is_nil(cookie)
Assert.eq(err, "郵箱帳號或密碼錯誤。")
end
-- 账号校验:「我的」页里的 /u/<uin>/ 是账号;跳登录页是 Cookie 无效。
do
reset({ { code = 200, body = '<a href="https://mox.moe/u/123456/">主頁</a>' } })
local ok, err, account
Client.accountAsync("VOLSKEY=k", function(o, e, a) ok, err, account = o, e, a end)
Assert.is_true(ok)
Assert.eq(account.id, "123456")
Assert.eq(calls[1].cookie, "VOLSKEY=k")
reset({ { code = 302, headers = { Location = "https://mox.moe/login.php" } } })
Client.accountAsync("VOLSKEY=bad", function(o, e) ok, err = o, e end)
Assert.is_false(ok)
Assert.matches(err, "登录")
end
-- 收藏:先读详情拿数字书号,回包里有 c101 才算成功。
do
local detail = [[<b class='font_size_l'>有鬼</b><script>var uin = "1"; var bookid = "34410";</script>]]
reset({ { code = 200, body = detail }, { code = 200, body = '<script>parent.display_codeinfo( "c101", 1 );</script>' } })
local ok, err
Client.new("VOLSKEY=k"):addToShelfAsync("de34d9", function(o, e) ok, err = o, e end)
Assert.is_true(ok)
Assert.eq(calls[2].url, "https://mox.moe/book_follow.php")
Assert.eq(calls[2].body, "follow_act=1&follow_bookid=34410&follow_type=0")
reset({ { code = 200, body = detail }, { code = 200, body = '<script>parent.display_codeinfo( "e999", 1 );</script>' } })
Client.new("VOLSKEY=k"):removeFromShelfAsync("de34d9", function(o, e) ok, err = o, e end)
Assert.is_nil(ok)
Assert.not_nil(err)
Assert.is_true(calls[2].body:find("follow_act=0", 1, true) ~= nil)
end