Repository: neovim/neovim
File: test/functional/lua/net_spec.lua (Lua)

Code before:
local n = require('test.functional.testnvim')()

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()
    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.text
        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()
    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

Review comment (suggestion):
I suppose this can be a followup, but mentioning before I forget:

we will need to define new `NVIM_TEST_INTEG` env var and document it here: https://github.com/neovim/neovim/blob/f731766474901e5e345e0ca630315ef69122e556/test/README.md?plain=1#L436

It should be disabled by default. When enabled, it enables these tests which make network calls. Can use `t.skip()` to guard these tests, or define something like `skip_integ` similar to :  https://github.com/neovim/neovim/blob/f731766474901e5e345e0ca630315ef69122e556/test/testutil.lua#L837

Code after:
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