feat(vim.net): custom request() headers #38837

Problem
Cannot specify headers in vim.net.request() call.

Solution
Support opts.headers in vim.net.request opts.
This commit is contained in:
Ellison
2026-04-10 10:55:57 -03:00
committed by GitHub
parent 6d43869aa0
commit 45f50d238a
4 changed files with 102 additions and 0 deletions

View File

@@ -11,6 +11,15 @@ local function assert_404_error(err)
)
end
local function assert_wrong_headers(expected_err, header)
local err = t.pcall_err(exec_lua, [[
return vim.net.request('https://example.com', {
headers = ]] .. header .. [[,
}, function() end)
]])
t.matches(expected_err, err)
end
describe('vim.net.request', function()
before_each(function()
n:clear()
@@ -163,4 +172,55 @@ describe('vim.net.request', function()
t.eq(false, rv.modified)
t.eq(true, rv.zipfile)
end)
it('accepts custom headers', function()
t.skip(skip_integ, 'NVIM_TEST_INTEG not set: skipping network integration test')
local headers = exec_lua([[
local done = false
local result
vim.net.request("https://httpbingo.org/headers", {
headers = {
Authorization = "Bearer test-token",
['X-Custom-Header'] = "custom-value",
['Empty'] = '',
},
}, function(err, res)
if err then
result = { error = err }
else
result = vim.json.decode(res.body).headers
end
done = true
end)
vim.wait(2000, function() return done end)
return result
]])
assert.is_table(headers)
-- httpbingo.org/request returns each header as a list in the returned value
t.eq(headers.Authorization[1], 'Bearer test-token', 'Expected Authorization header')
t.eq(headers['X-Custom-Header'][1], 'custom-value', 'Expected X-Custom-Header')
t.eq(headers['Empty'][1], '', 'Expected Empty header')
end)
it('validation', function()
assert_wrong_headers('opts.headers: expected table, got number', '123')
assert_wrong_headers('headers keys and values must be strings', "{ [123] = 'value' }")
assert_wrong_headers('headers keys and values must be strings', '{ Header = 123 }')
assert_wrong_headers(
'header keys must not start with @ or end with : and ;',
"{ ['Header:'] = 'value' }"
)
assert_wrong_headers(
'header keys must not start with @ or end with : and ;',
"{ ['Header;'] = 'value' }"
)
assert_wrong_headers(
'header keys must not start with @ or end with : and ;',
"{ ['@filename'] = '' }"
)
end)
end)