net_spec.lua (2432B)
1 local n = require('test.functional.testnvim')() 2 local t = require('test.testutil') 3 local skip_integ = os.getenv('NVIM_TEST_INTEG') ~= '1' 4 5 local exec_lua = n.exec_lua 6 7 local function assert_404_error(err) 8 assert( 9 err:lower():find('404') or err:find('22'), 10 'Expected HTTP 404 or exit code 22, got: ' .. tostring(err) 11 ) 12 end 13 14 describe('vim.net.request', function() 15 before_each(function() 16 n:clear() 17 end) 18 19 it('fetches a URL into memory (async success)', function() 20 t.skip(skip_integ, 'NVIM_TEST_INTEG not set: skipping network integration test') 21 local content = exec_lua([[ 22 local done = false 23 local result 24 local M = require('vim.net') 25 26 M.request("https://httpbingo.org/anything", { retry = 3 }, function(err, body) 27 assert(not err, err) 28 result = body.body 29 done = true 30 end) 31 32 vim.wait(2000, function() return done end) 33 return result 34 ]]) 35 36 assert( 37 content and content:find('"url"%s*:%s*"https://httpbingo.org/anything"'), 38 'Expected response body to contain the correct URL' 39 ) 40 end) 41 42 it("detects filetype, sets 'nomodified'", function() 43 t.skip(skip_integ, 'NVIM_TEST_INTEG not set: skipping network integration test') 44 45 local rv = exec_lua([[ 46 vim.cmd('runtime! plugin/nvim/net.lua') 47 vim.cmd('runtime! filetype.lua') 48 -- github raw dump of a small lua file in the neovim repo 49 vim.cmd('edit https://raw.githubusercontent.com/neovim/neovim/master/runtime/syntax/tutor.lua') 50 vim.wait(2000, function() return vim.bo.filetype ~= '' end) 51 -- wait for buffer to have content 52 vim.wait(2000, function() return vim.fn.wordcount().bytes > 0 end) 53 vim.wait(2000, function() return vim.bo.modified == false end) 54 return { vim.bo.filetype, vim.bo.modified } 55 ]]) 56 57 t.eq('lua', rv[1]) 58 t.eq(false, rv[2], 'Expected buffer to be unmodified for remote content') 59 end) 60 61 it('calls on_response with error on 404 (async failure)', function() 62 t.skip(skip_integ, 'NVIM_TEST_INTEG not set: skipping network integration test') 63 local err = exec_lua([[ 64 local done = false 65 local result 66 local M = require('vim.net') 67 68 M.request("https://httpbingo.org/status/404", {}, function(e, _) 69 result = e 70 done = true 71 end) 72 73 vim.wait(2000, function() return done end) 74 return result 75 ]]) 76 77 assert_404_error(err) 78 end) 79 end)