Skip to content

Commit e795f7c

Browse files
fix(parser): handler heading without value
1 parent 29bee11 commit e795f7c

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

lua/rest-nvim/parser/init.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ local function parse_headers(req_node, source, context)
6969
local header_nodes = req_node:field("header")
7070
for _, node in ipairs(header_nodes) do
7171
local key = assert(get_node_field_text(node, "name", source))
72-
local value = assert(get_node_field_text(node, "value", source))
73-
key = expand_variables(key, context)
74-
value = expand_variables(value, context)
75-
key = string.lower(key)
76-
table.insert(headers[key], value)
72+
local value = get_node_field_text(node, "value", source)
73+
key = expand_variables(key, context):lower()
74+
if value then
75+
value = expand_variables(value, context)
76+
table.insert(headers[key], value)
77+
else
78+
headers[key] = {}
79+
end
7780
end
7881
return setmetatable(headers, nil)
7982
end
@@ -385,7 +388,7 @@ function parser.parse(node, source, ctx)
385388

386389
---@type string?
387390
local content_type
388-
if headers["content-type"] then
391+
if headers["content-type"] and #headers["content-type"] > 0 then
389392
content_type = headers["content-type"][1]:match("([^;]+)")
390393
end
391394
local body

spec/parser/http_parser_spec.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ HOST: localhost:8000
7272
local req = assert(parser.parse(req_node, source))
7373
assert.same("http://localhost:8000/some/path", req.url)
7474
end)
75+
it("parse request with headers", function ()
76+
local source = [[
77+
http://example.com/api
78+
X-Header1: value1
79+
X-Header2:
80+
X-Header1: value2
81+
]]
82+
local _, tree = utils.ts_parse_source(source)
83+
local req_node = assert(tree:root():child(0))
84+
assert.same({
85+
url = "http://example.com/api",
86+
method = "GET",
87+
headers = {
88+
["x-header1"] = { "value1", "value2" },
89+
["x-header2"] = {},
90+
},
91+
handlers = {},
92+
cookies = {},
93+
}, parser.parse(req_node, source))
94+
end)
7595

7696
describe("parse body", function()
7797
it("json body", function()

0 commit comments

Comments
 (0)