diff options
| author | Ellison <ellisonleao@gmail.com> | 2026-04-10 10:55:57 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-10 09:55:57 -0400 |
| commit | 45f50d238a78314fd575b5a25dd28f14b730cb5b (patch) | |
| tree | 8e53768b440bb8bafb91c0118689d4c0a1dd1cc6 /test/functional/lua | |
| parent | 6d43869aa0ca8b8795aa91bcc5415dd4f2e2722d (diff) | |
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.
Diffstat (limited to 'test/functional/lua')
| -rw-r--r-- | test/functional/lua/net_spec.lua | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/functional/lua/net_spec.lua b/test/functional/lua/net_spec.lua index 2057ce3331..d8fe345a80 100644 --- a/test/functional/lua/net_spec.lua +++ b/test/functional/lua/net_spec.lua @@ -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) |
