Skip to content

Commit 5bcaa10

Browse files
Add case-insensitive table lookups, and use for host and content-type. (#236)
A new utility function called `key` takes a table and a key. It loops through the table items, comparing the each key with the given search key in a case-insensitive manner. Return the first matching key; otherwise, return the given search key. This allows operations like these to be performed: ```lua local t = {s=37, S=73} t[utils.key(t,'a')] = 1 -- Insert a:1 t == {s=37,S=73,a=1} y = t[utils.key(t,'a')] -- Find 'a' y == 1 z = t[utils.key(t,'A')] -- Find 'A' (same as 'a') z == 1 m = t[utils.key(t,'b')] -- Show 'b' is missing. m == nil k = utils.key(t,'s') -- Which 's/S' is first? k is indeterminate l = t['s'] -- Get 's' and 'S' l = 37 u = t['S'] -- in the usual manner. u = 73 t[utils.key(t,'a')] = nil -- Delete 'a' t == {s=37, S=73} ``` As implied by the `k = ` example above, lua associative tables are unordered, so there's no guarantee that 's' (or 'S') is the first one to be found. In this context, that shouldn't be too much of an issue. Use this new function to find the 'Host' and 'Content-Type' headers' values no matter in what case they were defined.
1 parent 8b62563 commit 5bcaa10

6 files changed

Lines changed: 23 additions & 22 deletions

File tree

lua/rest-nvim/curl/init.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,9 @@ local function create_callback(curl_cmd, opts)
103103
return
104104
end
105105
local res_bufnr = M.get_or_create_buf()
106-
local content_type = nil
107-
108-
-- get content type
109-
for _, header in ipairs(res.headers) do
110-
if string.lower(header):find("^content%-type") then
111-
content_type = header:match("application/([-a-z]+)") or header:match("text/(%l+)")
112-
break
113-
end
106+
local content_type = res.headers[utils.key(res.headers,'content-type')]
107+
if content_type then
108+
content_type = content_type:match("application/([-a-z]+)") or content_type:match("text/(%l+)")
114109
end
115110

116111
if script_str ~= nil then

lua/rest-nvim/init.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,7 @@ local function splice_body(headers, payload)
101101
else
102102
lines = payload.body_tpl
103103
end
104-
local content_type = ""
105-
for key, val in pairs(headers) do
106-
if string.lower(key) == "content-type" then
107-
content_type = val
108-
break
109-
end
110-
end
104+
local content_type = headers[utils.key(headers,"content-type")] or ""
111105
local has_json = content_type:find("application/[^ ]*json")
112106

113107
local body = ""

lua/rest-nvim/request/init.lua

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,9 @@ M.buf_get_request = function(bufnr, curpos)
286286

287287
local curl_args, body_start = get_curl_args(bufnr, headers_end, end_line)
288288

289-
if headers["host"] ~= nil then
290-
headers["host"] = headers["host"]:gsub("%s+", "")
291-
headers["host"] = string.gsub(headers["host"], "%s+", "")
292-
parsed_url.url = headers["host"] .. parsed_url.url
293-
headers["host"] = nil
294-
end
289+
local host = headers[utils.key(headers,"host")] or ""
290+
parsed_url.url = host:gsub("%s+", "") .. parsed_url.url
291+
headers[utils.key(headers,"host")] = nil
295292

296293
local body = get_body(bufnr, body_start, end_line)
297294

lua/rest-nvim/utils/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ M.get_file_variables = function()
107107
end
108108
return variables
109109
end
110+
110111
-- Gets the variables from the currently selected env_file
111112
M.get_env_variables = function()
112113
local variables = {}
@@ -286,6 +287,19 @@ M.has_value = function(tbl, str)
286287
return false
287288
end
288289

290+
-- key returns the provided table's key that matches the given case-insensitive pattern.
291+
-- if not found, return the given key.
292+
-- @param tbl Table to iterate over
293+
-- @param key The key to be searched in the table
294+
M.key = function(tbl, key)
295+
for tbl_key, _ in pairs(tbl) do
296+
if string.lower(tbl_key) == string.lower(key) then
297+
return tbl_key
298+
end
299+
end
300+
return key
301+
end
302+
289303
-- tbl_to_str recursively converts the provided table into a json string
290304
-- @param tbl Table to convert into a String
291305
-- @param json If the string should use a key:value syntax

tests/get_with_host.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
###
22

33
GET /api/users?page=5
4-
Host: https://reqres.in
4+
host: https://reqres.in
55

66
###
77

tests/main_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe("rest testing framework", function()
66
assert(rest.run_file("tests/basic_get.http", opts) == true)
77
assert(rest.run_file("tests/post_json_form.http", opts) == true)
88
assert(rest.run_file("tests/post_create_user.http", opts) == true)
9+
assert(rest.run_file("tests/get_with_host.http", opts) == true)
910
assert(rest.run_file("tests/put_update_user.http", opts) == true)
1011
assert(rest.run_file("tests/patch_update_user.http", opts) == true)
1112
assert(rest.run_file("tests/delete.http", opts) == true)

0 commit comments

Comments
 (0)