mirror of
https://github.com/neovim/neovim.git
synced 2025-12-15 19:05:40 +00:00
feat(net): vim.net.request(), :edit [url] #34140
Problem:
Nvim depends on netrw to download/request URL contents.
Solution:
- Add `vim.net.request()` as a thin curl wrapper:
- Basic GET with --silent, --show-error, --fail, --location, --retry
- Optional `opts.outpath` to save to a file
- Operates asynchronously. Pass an `on_response` handler to get the result.
- Add integ tests (requires NVIM_TEST_INTEG to be set) to test success
and 404 failure.
- Health check for missing `curl`.
- Handle `:edit https://…` using `vim.net.request()`.
API Usage:
1. Asynchronous request:
vim.net.request('https://httpbingo.org/get', { retry = 2 }, function(err, response)
if err then
print('Fetch failed:', err)
else
print('Got body of length:', #response.body)
end
end)
2. Download to file:
vim.net.request('https://httpbingo.org/get', { outpath = 'out_async.txt' }, function(err)
if err then print('Error:', err) end
end)
3. Remote :edit integration (in runtime/plugin/net.lua) fetches into buffer:
:edit https://httpbingo.org/get
This commit is contained in:
60
test/functional/lua/net_spec.lua
Normal file
60
test/functional/lua/net_spec.lua
Normal file
@@ -0,0 +1,60 @@
|
||||
local n = require('test.functional.testnvim')()
|
||||
local t = require('test.testutil')
|
||||
local skip_integ = os.getenv('NVIM_TEST_INTEG') ~= '1'
|
||||
|
||||
local exec_lua = n.exec_lua
|
||||
|
||||
local function assert_404_error(err)
|
||||
assert(
|
||||
err:lower():find('404') or err:find('22'),
|
||||
'Expected HTTP 404 or exit code 22, got: ' .. tostring(err)
|
||||
)
|
||||
end
|
||||
|
||||
describe('vim.net.request', function()
|
||||
before_each(function()
|
||||
n:clear()
|
||||
end)
|
||||
|
||||
it('fetches a URL into memory (async success)', function()
|
||||
t.skip(skip_integ, 'NVIM_TEST_INTEG not set: skipping network integration test')
|
||||
local content = exec_lua([[
|
||||
local done = false
|
||||
local result
|
||||
local M = require('vim.net')
|
||||
|
||||
M.request("https://httpbingo.org/anything", { retry = 3 }, function(err, body)
|
||||
assert(not err, err)
|
||||
result = body.body
|
||||
done = true
|
||||
end)
|
||||
|
||||
vim.wait(2000, function() return done end)
|
||||
return result
|
||||
]])
|
||||
|
||||
assert(
|
||||
content and content:find('"url"%s*:%s*"https://httpbingo.org/anything"'),
|
||||
'Expected response body to contain the correct URL'
|
||||
)
|
||||
end)
|
||||
|
||||
it('calls on_response with error on 404 (async failure)', function()
|
||||
t.skip(skip_integ, 'NVIM_TEST_INTEG not set: skipping network integration test')
|
||||
local err = exec_lua([[
|
||||
local done = false
|
||||
local result
|
||||
local M = require('vim.net')
|
||||
|
||||
M.request("https://httpbingo.org/status/404", {}, function(e, _)
|
||||
result = e
|
||||
done = true
|
||||
end)
|
||||
|
||||
vim.wait(2000, function() return done end)
|
||||
return result
|
||||
]])
|
||||
|
||||
assert_404_error(err)
|
||||
end)
|
||||
end)
|
||||
Reference in New Issue
Block a user