1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
local Result = require "mason-core.result"
local fetch = require "mason-core.fetch"
local match = require "luassert.match"
local spawn = require "mason-core.spawn"
local stub = require "luassert.stub"
local version = require "mason.version"
describe("fetch", function()
local snapshot
before_each(function()
snapshot = assert.snapshot()
end)
after_each(function()
snapshot:revert()
end)
it("should exhaust all candidates", function()
stub(spawn, "wget")
stub(spawn, "curl")
spawn.wget.returns(Result.failure "wget failure")
spawn.curl.returns(Result.failure "curl failure")
local result = fetch("https://api.github.com", {
headers = { ["X-Custom-Header"] = "here" },
})
assert.is_true(result:is_failure())
assert.spy(spawn.wget).was_called(1)
assert.spy(spawn.curl).was_called(1)
assert.spy(spawn.wget).was_called_with {
{
{
"--header",
("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION),
},
{
"--header",
"X-Custom-Header: here",
},
},
"-o",
"/dev/null",
"-O",
"-",
"-T",
30,
vim.NIL, -- body-data
"https://api.github.com",
}
assert.spy(spawn.curl).was_called_with(match.tbl_containing {
match.same {
{
"-H",
("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION),
},
{
"-H",
"X-Custom-Header: here",
},
},
"-fsSL",
match.same { "-X", "GET" },
vim.NIL, -- data
vim.NIL, -- out file
match.same { "--connect-timeout", 30 },
"https://api.github.com",
on_spawn = match.is_function(),
})
end)
it("should return stdout", function()
stub(spawn, "curl")
spawn.curl.returns(Result.success {
stdout = [[{"data": "here"}]],
})
local result = fetch "https://api.github.com/data"
assert.is_true(result:is_success())
assert.equals([[{"data": "here"}]], result:get_or_throw())
end)
it("should respect out_file opt", function()
stub(spawn, "wget")
stub(spawn, "curl")
spawn.wget.returns(Result.failure "wget failure")
spawn.curl.returns(Result.failure "curl failure")
fetch("https://api.github.com/data", { out_file = "/test.json" })
assert.spy(spawn.wget).was_called_with {
{
{
"--header",
("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION),
},
},
"-o",
"/dev/null",
"-O",
"/test.json",
"-T",
30,
vim.NIL, -- body-data
"https://api.github.com/data",
}
assert.spy(spawn.curl).was_called_with(match.tbl_containing {
match.same {
{
"-H",
("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION),
},
},
"-fsSL",
match.same { "-X", "GET" },
vim.NIL, -- data
match.same { "-o", "/test.json" },
match.same { "--connect-timeout", 30 },
"https://api.github.com/data",
on_spawn = match.is_function(),
})
end)
end)
describe("fetch :: wget", function()
it("should reject non-supported HTTP methods", function()
stub(spawn, "wget")
stub(spawn, "curl")
spawn.wget.returns(Result.failure "wget failure")
spawn.curl.returns(Result.failure "curl failure")
local PATCH_ERR = assert.has_error(function()
fetch("https://api.github.com/data", { method = "PATCH" }):get_or_throw()
end)
local DELETE_ERR = assert.has_error(function()
fetch("https://api.github.com/data", { method = "DELETE" }):get_or_throw()
end)
local PUT_ERR = assert.has_error(function()
fetch("https://api.github.com/data", { method = "PUT" }):get_or_throw()
end)
assert.equals("fetch: wget doesn't support HTTP method PATCH", PATCH_ERR)
assert.equals("fetch: wget doesn't support HTTP method DELETE", DELETE_ERR)
assert.equals("fetch: wget doesn't support HTTP method PUT", PUT_ERR)
end)
it("should reject requests with opts.data if not opts.method is not POST", function()
stub(spawn, "wget")
stub(spawn, "curl")
spawn.wget.returns(Result.failure "wget failure")
spawn.curl.returns(Result.failure "curl failure")
local err = assert.has_error(function()
fetch("https://api.github.com/data", { data = "data" }):get_or_throw()
end)
assert.equals("fetch: data provided but method is not POST (was GET)", err)
end)
end)
|