91 lines
2.8 KiB
Lua
91 lines
2.8 KiB
Lua
--[[--
|
|
维基文库客户端:api.php、坏响应、首页 Portal 只拉一次。
|
|
|
|
@module tests.wikisource.client_spec
|
|
--]]
|
|
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, inject-field
|
|
|
|
local Assert = require("support.assert")
|
|
local JsonStub = require("support.json_stub")
|
|
package.loaded["json"] = nil
|
|
package.preload["json"] = function()
|
|
return { decode = JsonStub.decode, encode = JsonStub.encode }
|
|
end
|
|
local JSON = require("json")
|
|
local _ = require("l10n").get("moonsource_wikisource")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
local queued_tick = UIManager.nextTick
|
|
UIManager.nextTick = function(_, fn) fn() end
|
|
|
|
local calls, script = {}, {}
|
|
package.preload["http.request"] = function()
|
|
return {
|
|
get = function(url, _opts, cb)
|
|
calls[#calls + 1] = url
|
|
local step = table.remove(script, 1) or { body = "{}" }
|
|
if step.err then cb(nil, step.err) else cb(step.body) end
|
|
return { cancel = function() end }
|
|
end,
|
|
}
|
|
end
|
|
|
|
local Client = require("moonsource_wikisource.client")
|
|
|
|
local function restore()
|
|
UIManager.nextTick = queued_tick
|
|
end
|
|
|
|
do
|
|
calls, script = {}, { { body = JSON.encode({
|
|
parse = { title = "甲", text = "<p>文</p>" },
|
|
}) } }
|
|
local client = Client.new()
|
|
local page, err
|
|
client:parseAsync("甲/一", function(data, e) page, err = data, e end)
|
|
Assert.is_nil(err)
|
|
Assert.eq(page.title, "甲")
|
|
Assert.eq(#calls, 1)
|
|
Assert.is_true(calls[1]:find("action=parse", 1, true) ~= nil)
|
|
Assert.is_true(calls[1]:find("page=%E7%94%B2%2F%E4%B8%80", 1, true) ~= nil)
|
|
calls, script = {}, {}
|
|
local again
|
|
client:parseAsync("甲", function(data) again = data end)
|
|
Assert.eq(again.title, "甲")
|
|
Assert.eq(#calls, 0)
|
|
end
|
|
|
|
do
|
|
calls, script = {}, { { body = JSON.encode({
|
|
parse = { wikitext = "[[Portal:典籍]]" },
|
|
}) } }
|
|
local client = Client.new()
|
|
local first, second
|
|
client:portalsAsync(function(data) first = data end)
|
|
client:portalsAsync(function(data) second = data end)
|
|
Assert.eq(first[1].id, "Portal:典籍")
|
|
Assert.eq(second[1].id, "Portal:典籍")
|
|
Assert.eq(#calls, 1)
|
|
Assert.is_true(calls[1]:find("Wikisource%3A%E9%A6%96%E9%A1%B5", 1, true) ~= nil)
|
|
end
|
|
|
|
do
|
|
calls, script = {}, { { body = "<html>" }, { body = JSON.encode({ error = { code = "nosuchpage" } }) } }
|
|
local client = Client.new()
|
|
local err_html, err_api
|
|
client:searchAsync("甲", function(_data, e) err_html = e end)
|
|
client:searchAsync("乙", function(_data, e) err_api = e end)
|
|
Assert.eq(err_html, _("响应无效"))
|
|
Assert.eq(err_api, _("响应无效"))
|
|
Assert.is_true(calls[1]:find("srsearch=%E7%94%B2", 1, true) ~= nil)
|
|
end
|
|
|
|
do
|
|
local client = Client.new()
|
|
local err
|
|
client:searchAsync("", function(_data, e) err = e end)
|
|
Assert.eq(err, _("响应无效"))
|
|
end
|
|
|
|
restore()
|