Skip to content

Commit

Permalink
feat: Updated lua/hurl/main.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Oct 28, 2023
1 parent 7b4b23d commit 3c084c7
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,53 @@ function M.setup()
end, { nargs = '*', range = true })
end

-- Add unit tests for the `request` function
function M.test_request()
-- Test successful request
local opts = { 'http://example.com' }
request(opts, function(response)
assert(response.status == 200, 'Expected status code 200')
assert(response.headers['content-type'] == 'text/html', 'Expected content-type to be text/html')
assert(response.body:find('<html>') ~= nil, 'Expected response body to contain <html>')
end)

-- Test failed request
local opts = { 'http://nonexistent' }
request(opts, function(response)
assert(response.status == 404, 'Expected status code 404')
assert(response.headers['content-type'] == 'text/plain', 'Expected content-type to be text/plain')
assert(response.body == 'Not Found', 'Expected response body to be "Not Found"')
end)

-- Test JSON response
local opts = { 'http://api.example.com/data' }
request(opts, function(response)
assert(response.status == 200, 'Expected status code 200')
assert(response.headers['content-type'] == 'application/json', 'Expected content-type to be application/json')
local json = vim.fn.json_decode(response.body)
assert(json ~= nil, 'Failed to decode JSON response')
assert(json.name == 'John Doe', 'Expected name to be "John Doe"')
end)

-- Test HTML response
local opts = { 'http://example.com' }
request(opts, function(response)
assert(response.status == 200, 'Expected status code 200')
assert(response.headers['content-type'] == 'text/html', 'Expected content-type to be text/html')
assert(response.body:find('<html>') ~= nil, 'Expected response body to contain <html>')
end)
end

return M

function M.setup()
util.create_cmd('HurlRunner', function(opts)
if opts.range ~= 0 then
run_selection(opts.fargs)
else
run_current_file(opts.fargs)
end
end, { nargs = '*', range = true })
end

return M

0 comments on commit 3c084c7

Please sign in to comment.