✨ (kanunu): 接入努努书坊,频道与分类浏览、按章阅读、整本缓存
Release / release (push) Successful in 14s

This commit is contained in:
garfield committed 2026-12-02 09:17:14 +08:00
commit f50b9e7711
23 files changed
+2346

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
--[[--
moonsource_kanunu.client 离线用例:站内路径、GBK 转码、Cloudflare 拦截如实报错、取消。
@module tests.moonsource_kanunu.client_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 Stubs = require("support.stubs")
-- 脚本化 http.request:按序消费 script;header 从 res.headers 取值。
local calls, script = {}, {}
package.preload["http.request"] = function()
return {
header = function(res, name)
return res and res.headers and res.headers[name:lower()] or nil
end,
get = function(url, opts, cb)
calls[#calls + 1] = {
url = url,
headers = opts and opts.headers or {},
allow_redirects = opts and opts.allow_redirects,
}
local request = calls[#calls]
local step = table.remove(script, 1) or { body = "" }
local handle = { cancel = function() request.cancelled = true end }
request.cb = function(body, err, res)
if request.cancelled then return end
cb(body, err, res)
end
if step.delayed then return handle end
if step.err then
request.cb(nil, step.err, nil)
elseif step.code and step.code ~= 200 then
request.cb(nil, "HTTP " .. step.code, { code = step.code, headers = step.headers or {} })
else
request.cb(step.body, nil, { code = 200, headers = step.headers or {} })
end
return handle
end,
}
end
local Client = require("moonsource_kanunu.client")
local function run(client, path)
local out_body, out_err
client:get(path, function(body, err)
out_body, out_err = body, err
end)
return out_body, out_err
end
-- GBK 页面按响应头转码。
do
local gbk = "GBK " .. string.char(0xD2, 0xB3, 0xC3, 0xE6, 0xA3, 0xBA)
.. string.char(0xC5, 0xAC, 0xC5, 0xAC, 0xCA, 0xE9, 0xB7, 0xBB)
script = { { body = gbk, headers = { ["content-type"] = "text/html; charset=gbk" } } }
calls = {}
local body, err = run(Client.new(), "/book8/")
Assert.is_nil(err)
Assert.eq(calls[1].url, "https://www.kanunu8.com/book8/")
Assert.is_true(calls[1].headers["User-Agent"]:find("Mozilla", 1, true) ~= nil, "带浏览器 UA")
Assert.is_true(calls[1].allow_redirects, "跟随 301/302,缺斜杠的目录页也能取到")
Assert.eq(body, "GBK 页面:努努书坊")
end
-- 没有响应头声明时按页面 meta 判定(分类页与书页的 charset 声明不一致)。
do
local gbk = '<meta http-equiv="Content-Type" content="text/html; charset=gb2312">'
.. string.char(0xC4, 0xE3, 0xBA, 0xC3)
script = { { body = gbk } }
local body = run(Client.new(), "/wuxia/")
Assert.eq(body:sub(-6), "你好", "meta 声明也转码")
end
-- UTF-8 页面原样返回(genres.html 是 UTF-8)。
do
script = { { body = "三体(全三册)", headers = { ["content-type"] = "text/html; charset=utf-8" } } }
Assert.eq(run(Client.new(), "/genres.html"), "三体(全三册)")
end
-- Cloudflare 拦截:如实报错,不重试、不伪造空列表。
do
script = { { code = 403 }, { body = "不该被请求" } }
calls = {}
local body, err = run(Client.new(), "/files/chinese/")
Assert.is_nil(body)
Assert.eq(err, "努努书坊把这次请求当成了爬虫,请稍后再试")
Assert.eq(#calls, 1, "被拦截不重试")
end
-- 瞬时断连:错误原样透传。
do
script = { { err = "SSL_ERROR_SYSCALL" } }
local body, err = run(Client.new(), "/book8/")
Assert.is_nil(body)
Assert.eq(err, "SSL_ERROR_SYSCALL")
end
-- 章页的站内绝对 URL 原样使用。
do
script = { { body = "章" } }
calls = {}
run(Client.new(), "https://www.kanunu8.com/book6/santi_1/11359.html")
Assert.eq(calls[1].url, "https://www.kanunu8.com/book6/santi_1/11359.html")
end
-- 外站地址不出门。
do
calls = {}
local body, err
Client.new():get("https://evil.test/x.html", function(value, failure)
body, err = value, failure
end)
Stubs.flush()
Assert.is_nil(body)
Assert.eq(err, "响应无效")
Assert.eq(#calls, 0, "外站不发请求")
end
-- 取消后不接受迟到正文。
do
script = { { delayed = true } }
calls = {}
local client, completed = Client.new(), 0
local handle = client:get("/book8/", function() completed = completed + 1 end)
handle.cancel()
Assert.is_true(calls[1].cancelled)
calls[1].cb("迟到", nil, { code = 200, headers = {} })
Assert.eq(completed, 0)
end