Skip to content

Commit c5094a8

Browse files
Matthieu Coudronteto
Matthieu Coudron
authored andcommitted
test: programmatic run of .http file
We want to be able to run a whole .http file
1 parent 84e8208 commit c5094a8

File tree

4 files changed

+88
-38
lines changed

4 files changed

+88
-38
lines changed

lua/rest-nvim/init.lua

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local request = require("rest-nvim.request")
22
local config = require("rest-nvim.config")
33
local curl = require("rest-nvim.curl")
4+
local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" })
45

56
local rest = {}
67
local Opts = {}
@@ -23,8 +24,36 @@ rest.run = function(verbose)
2324
return rest.run_request(result, verbose)
2425
end
2526

26-
rest.run_request = function(req, verbose)
27+
-- run will retrieve the required request information from the current buffer
28+
-- and then execute curl
29+
-- @param string filename to load
30+
-- @param opts table
31+
-- 1. keep_going boolean keep running even when last request failed
32+
rest.run_file = function(filename, opts)
33+
log.info("Running file :" .. filename)
34+
local new_buf = vim.api.nvim_create_buf(false, false)
35+
36+
vim.api.nvim_win_set_buf(0, new_buf)
37+
vim.cmd.edit(filename)
38+
local last_line = vim.fn.line("$")
39+
40+
-- reset cursor position
41+
vim.fn.cursor(1, 1)
42+
local curpos = vim.fn.getcurpos()
43+
while curpos[2] <= last_line do
44+
local ok, req = request.buf_get_request(new_buf, curpos)
45+
if ok then
46+
-- request.print_request(req)
47+
curpos[2] = req.end_line + 1
48+
rest.run_request(req, opts)
49+
else
50+
return false, req
51+
end
52+
end
53+
return true
54+
end
2755

56+
rest.run_request = function(req, opts)
2857
local result = req
2958
Opts = {
3059
method = result.method:lower(),
@@ -35,13 +64,13 @@ rest.run_request = function(req, verbose)
3564
raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
3665
or result.raw,
3766
body = result.body,
38-
dry_run = verbose or false,
67+
dry_run = opts.verbose or false,
3968
bufnr = result.bufnr,
4069
start_line = result.start_line,
4170
end_line = result.end_line,
4271
}
4372

44-
if not verbose then
73+
if not opts.verbose then
4574
LastOpts = Opts
4675
end
4776

@@ -56,6 +85,7 @@ rest.run_request = function(req, verbose)
5685
"[rest.nvim] Failed to perform the request.\nMake sure that you have entered the proper URL and the server is running.\n\nTraceback: "
5786
.. req_err
5887
)
88+
return false, req_err
5989
end
6090
end
6191

lua/rest-nvim/request/init.lua

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ local function get_curl_args(bufnr, headers_end, end_line)
145145

146146
if line_content:find("^ *%-%-?[a-zA-Z%-]+") then
147147
local lc = vim.split(line_content, " ")
148-
local x = ""
148+
local x = ""
149149

150150
for i, y in ipairs(lc) do
151151
x = x .. y
@@ -174,17 +174,25 @@ end
174174
-- of the current request and returns the linenumber of this request line.
175175
-- The current request is defined as the next request line above the cursor
176176
-- @param bufnr The buffer nummer of the .http-file
177-
local function start_request()
178-
return vim.fn.search("^GET\\|^POST\\|^PUT\\|^PATCH\\|^DELETE", "cbn", 1)
177+
-- @param linenumber (number) From which line to start looking
178+
local function start_request(bufnr, linenumber)
179+
log.debug("Searching pattern starting from " .. linenumber)
180+
181+
local oldlinenumber = linenumber
182+
utils.move_cursor(bufnr, linenumber)
183+
184+
local res = vim.fn.search("^GET\\|^POST\\|^PUT\\|^PATCH\\|^DELETE", "cn")
185+
-- restore cursor position
186+
utils.move_cursor(bufnr, oldlinenumber)
187+
188+
return res
179189
end
180190

181191
-- end_request will find the next request line (e.g. POST http://localhost:8081/foo)
182192
-- and returns the linenumber before this request line or the end of the buffer
183193
-- @param bufnr The buffer nummer of the .http-file
184-
local function end_request(bufnr)
194+
local function end_request(bufnr, linenumber)
185195
-- store old cursor position
186-
local curpos = vim.fn.getcurpos()
187-
local linenumber = curpos[2]
188196
local oldlinenumber = linenumber
189197

190198
-- start searching for next request from the next line
@@ -245,18 +253,19 @@ M.get_current_request = function()
245253
end
246254

247255
-- buf_get_request returns a table with all the request settings
248-
-- @param bufnr the buffer number
256+
-- @param bufnr (number|nil) the buffer number
249257
-- @param curpos the cursor position
250258
-- @return (boolean, request or string)
251259
M.buf_get_request = function(bufnr, curpos)
252260
curpos = curpos or vim.fn.getcurpos()
253261
bufnr = bufnr or vim.api.nvim_win_get_buf(0)
254262

255-
local start_line = start_request()
263+
local start_line = start_request(bufnr, curpos[2])
264+
256265
if start_line == 0 then
257266
return false, "No request found"
258267
end
259-
local end_line = end_request(bufnr)
268+
local end_line = end_request(bufnr, start_line)
260269

261270
local parsed_url = parse_url(vim.fn.getline(start_line))
262271

@@ -284,17 +293,28 @@ M.buf_get_request = function(bufnr, curpos)
284293
utils.move_cursor(bufnr, curpos[2], curpos[3])
285294
end
286295

287-
return true, {
288-
method = parsed_url.method,
289-
url = parsed_url.url,
290-
http_version = parsed_url.http_version,
291-
headers = headers,
292-
raw = curl_args,
293-
body = body,
294-
bufnr = bufnr,
295-
start_line = start_line,
296-
end_line = end_line,
297-
}
296+
return true,
297+
{
298+
method = parsed_url.method,
299+
url = parsed_url.url,
300+
http_version = parsed_url.http_version,
301+
headers = headers,
302+
raw = curl_args,
303+
body = body,
304+
bufnr = bufnr,
305+
start_line = start_line,
306+
end_line = end_line,
307+
}
308+
end
309+
310+
M.print_request = function(req)
311+
local str = [[
312+
version: ]] .. req.url .. [[\n
313+
method: ]] .. req.method .. [[\n
314+
start_line: ]] .. tostring(req.start_line) .. [[\n
315+
end_line: ]] .. tostring(req.end_line) .. [[\n
316+
]]
317+
print(str)
298318
end
299319

300320
local select_ns = vim.api.nvim_create_namespace("rest-nvim")

lua/rest-nvim/utils/init.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ M.get_variables = function()
5050
local variables = {}
5151

5252
-- If there is a line at the beginning with @ first
53-
if vim.fn.search('^@', 'cn') > 0 then
53+
if vim.fn.search("^@", "cn") > 0 then
5454
-- Read all lines of the file
55-
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true);
55+
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
5656

5757
-- For each line
5858
for _, line in pairs(lines) do
5959
-- Get the name and value form lines that starts with @
60-
local name, val = line:match("^@([%w!@#$%^&*-_+?~]+)%s*=%s*([^=]+)");
60+
local name, val = line:match("^@([%w!@#$%^&*-_+?~]+)%s*=%s*([^=]+)")
6161
if name then
6262
-- Add to variables
63-
variables[name] = val;
63+
variables[name] = val
6464
end
6565
end
6666
end
@@ -94,7 +94,7 @@ M.get_variables = function()
9494
-- Add that into the variable
9595
-- I.E if @url={{path}}:{{port}}/{{source}}
9696
-- Substitue in path, port and source
97-
variables[name] = variables[name]:gsub('{{' .. oname .. '}}', ovalue);
97+
variables[name] = variables[name]:gsub("{{" .. oname .. "}}", ovalue)
9898
end
9999
end
100100
end

tests/main_spec.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
local rest = require('rest-nvim')
2-
local v = vim
1+
local rest = require("rest-nvim")
32

43
describe("rest testing framework", function()
5-
6-
it('test create users', function()
7-
8-
v.api.nvim_cmd({cmd='edit', args = {'tests/post_create_user.http'}}, {})
9-
10-
-- first argument is for verbosity
11-
rest.run(false)
12-
end)
4+
it("test create users", function()
5+
local opts = { keep_going = true, verbose = true }
6+
assert(rest.run_file("tests/basic_get.http", opts) == true)
7+
assert(rest.run_file("tests/post_json_form.http", opts) == true)
8+
assert(rest.run_file("tests/post_create_user.http", opts) == true)
9+
assert(rest.run_file("tests/put_update_user.http", opts) == true)
10+
assert(rest.run_file("tests/patch_update_user.http", opts) == true)
11+
assert(rest.run_file("tests/delete.http", opts) == true)
12+
end)
1313
end)

0 commit comments

Comments
 (0)