Skip to content

Commit 3c13242

Browse files
fix: set host in the context of port
1 parent 6af0022 commit 3c13242

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

lua/rest-nvim/parser/init.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,17 @@ function parser.get_cursor_request_node()
152152
return node
153153
end
154154

155+
---@param source Source
155156
---@return TSNode[]
156-
function parser.get_all_request_node()
157-
local source = 0
157+
function parser.get_all_request_nodes(source)
158158
local _, tree = utils.ts_parse_source(source)
159-
local reqs = {}
159+
local result = {}
160160
for node, _ in tree:root():iter_children() do
161-
if node:type() == "request" then
162-
table.insert(reqs, node)
161+
if node:type() == "section" then
162+
table.insert(result, node)
163163
end
164164
end
165-
return reqs
165+
return result
166166
end
167167

168168
---@return TSNode?
@@ -321,9 +321,17 @@ function parser.parse(node, source, ctx)
321321
end
322322

323323
local headers = parse_headers(req_node, source, ctx)
324-
-- HACK: check if url doesn't have host
325324
if headers["host"] and vim.startswith(url, "/") then
326-
url = "http://" ..headers["host"][1]..url
325+
local host = headers["host"][1]
326+
if not host:match("^https?://") then
327+
local port = host:match(":(%d%d+)$")
328+
local protocol = "http://"
329+
if not port or port == "443" then
330+
protocol = "https://"
331+
end
332+
host = protocol .. host
333+
end
334+
url = host..url
327335
table.remove(headers["host"], 1)
328336
end
329337
---@type rest.Request

lua/rest-nvim/request.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ end
153153

154154
---run all requests in current file with same context
155155
function M.run_all()
156-
local reqs = parser.get_all_request_node()
156+
local reqs = parser.get_all_request_nodes()
157157
local ctx = parser.create_context(0)
158158
for _, req_node in ipairs(reqs) do
159159
local req = parser.parse(req_node, 0, ctx)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### host with non-secure port
2+
GET /api
3+
HOST: example.com:8080
4+
5+
### host with secure port
6+
GET /api
7+
HOST: example.com:443
8+
9+
### host with protocol
10+
GET /api
11+
HOST: http://example.com
12+
13+
### host without protocol
14+
GET /api
15+
HOST: example.com

spec/examples_spec.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,38 @@ describe("multi-line-url", function ()
1717
local _, tree = utils.ts_parse_source(source)
1818
local req_node = assert(tree:root():child(0))
1919
local req = parser.parse(req_node, source)
20-
assert(req)
20+
assert.not_nil(req)
21+
---@cast req rest.Request
2122
assert.same("http://example.com:8080/api/html/get?id=123&value=content", req.url)
2223
end)
2324
end)
25+
26+
describe("url without host", function ()
27+
local source = open("spec/examples/url_without_host.http")
28+
local req_nodes = parser.get_all_request_nodes(source)
29+
assert.same(4, #req_nodes)
30+
it("host with non-secure port", function ()
31+
local req = parser.parse(req_nodes[1], source)
32+
assert.not_nil(req)
33+
---@cast req rest.Request
34+
assert.same("http://example.com:8080/api", req.url)
35+
end)
36+
it("host with secure port", function ()
37+
local req = parser.parse(req_nodes[2], source)
38+
assert.not_nil(req)
39+
---@cast req rest.Request
40+
assert.same("https://example.com:443/api", req.url)
41+
end)
42+
it("host with protocol", function ()
43+
local req = parser.parse(req_nodes[3], source)
44+
assert.not_nil(req)
45+
---@cast req rest.Request
46+
assert.same("http://example.com/api", req.url)
47+
end)
48+
it("host without protocol", function ()
49+
local req = parser.parse(req_nodes[4], source)
50+
assert.not_nil(req)
51+
---@cast req rest.Request
52+
assert.same("https://example.com/api", req.url)
53+
end)
54+
end)

0 commit comments

Comments
 (0)