---@module 'luassert'
require("spec.minimal_init")
local parser = require("rest-nvim.parser")
local utils = require("rest-nvim.utils")
local context = require("rest-nvim.context").Context
local logger = require("rest-nvim.logger")
local spy = require("luassert.spy")
local function open(path)
vim.cmd.edit(path)
return 0
end
---@diagnostic disable-next-line: duplicate-set-field
vim.notify = function() end
describe("http parser", function()
it("validate http parser", function()
assert.same("http", vim.treesitter.language.get_lang("http"))
end)
it("parse from http string", function()
local source = "GET https://github.com\n"
local _, tree = utils.ts_parse_source(source)
local req_node = assert(tree:root():child(0))
assert.same({
method = "GET",
url = "https://github.com",
headers = {},
cookies = {},
handlers = {},
}, parser.parse(req_node, source))
end)
it("parse from http file", function()
local source = open("spec/examples/basic_get.http")
local _, tree = utils.ts_parse_source(source)
local req_node = assert(tree:root():child(0))
assert.same({
name = "basic get statement",
method = "GET",
url = "https://api.github.com/users/boltlessengineer",
headers = {
["user-agent"] = { "neovim" },
},
cookies = {},
handlers = {},
}, parser.parse(req_node, source))
end)
it("capture all request names", function()
local source = [[
### first named request
GET http://localhost:80
### request separator that isn't a request name
###
# @name=second named request
# additional comments
GET http://localhost:80
]]
local names = parser.get_request_names(source)
assert.same({ "first named request", "second named request" }, names)
end)
it("parse request with host header", function()
local source = [[
GET /some/path
HOST: localhost:8000
]]
local _, tree = utils.ts_parse_source(source)
local req_node = assert(tree:root():child(0))
local req = assert(parser.parse(req_node, source))
assert.same("http://localhost:8000/some/path", req.url)
end)
it("parse request with headers", function()
local source = [[
http://example.com/api
X-Header1: value1
X-Header2:
X-Header1: value2
]]
local _, tree = utils.ts_parse_source(source)
local req_node = assert(tree:root():child(0))
assert.same({
url = "http://example.com/api",
method = "GET",
headers = {
["x-header1"] = { "value1", "value2" },
["x-header2"] = {},
},
handlers = {},
cookies = {},
}, parser.parse(req_node, source))
end)
describe("parse body", function()
it("json body", function()
local source = 'POST https://example.com\n\n{\n\t"blah": 1}\n'
local _, tree = utils.ts_parse_source(source)
local req_node = assert(tree:root():child(0))
assert.same({
method = "POST",
url = "https://example.com",
headers = {},
cookies = {},
handlers = {},
body = {
__TYPE = "json",
data = '{\n\t"blah": 1}',
},
}, parser.parse(req_node, source))
end)
it("invalid json body", function()
local source = 'POST https://example.com\n\n{\n\t"blah": 1\n'
local _, tree = utils.ts_parse_source(source)
local req_node = assert(tree:root():child(0))
local spy_log_warn = spy.on(logger, "warn")
parser.parse(req_node, source)
---@diagnostic disable-next-line: undefined-field
assert.spy(spy_log_warn).called_with("invalid json: '{\n\t\"blah\": 1'")
end)
it("parse xml", function()
local source = [[POST https://example.com
login
password
]]
local _, tree = utils.ts_parse_source(source)
local req_node = assert(tree:root():child(0))
assert.same({
method = "POST",
url = "https://example.com",
headers = {},
cookies = {},
handlers = {},
body = {
__TYPE = "xml",
data = [[
login
password
]],
},
}, parser.parse(req_node, source))
end)
it("parse invalid xml", function()
logger.info("hi")
local source = "POST https://example.com\n\n> path/to/file.json\n"
local _, tree = utils.ts_parse_source(source)
local node = assert(tree:root():child(0))
local req = assert(parser.parse(node, source))
assert.same(1, #req.handlers)
end)
-- TODO: update this testcase
-- it("create context from source", function()
-- local source = [[
-- @foo=bar
-- @bar=1234
-- @baz={{foo}} adsf
-- ]]
-- local ctx = parser.create_context(source)
-- assert.same({
-- foo = "bar",
-- bar = "1234",
-- baz = "bar adsf",
-- }, ctx.vars)
-- end)
end)