forked from rest-nvim/rest.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_spec.lua
More file actions
221 lines (215 loc) · 6.76 KB
/
cli_spec.lua
File metadata and controls
221 lines (215 loc) · 6.76 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
---@diagnostic disable: invisible
---@module 'luassert'
require("spec.minimal_init")
vim.g.rest_nvim = vim.tbl_deep_extend("force", {
clients = {
curl = {
opts = {
set_compressed = true,
certificates = {},
},
},
},
}, vim.g.rest_nvim)
local Context = require("rest-nvim.context").Context
local curl = require("rest-nvim.client.curl.cli")
local builder = curl.builder
local parser = curl.parser
local STAT_FORMAT = builder.STAT_ARGS[2]
describe("curl cli builder", function()
it("from GET request", function()
local args = builder.build({
context = Context:new(),
method = "GET",
url = "http://localhost:8000",
headers = {},
cookies = {},
handlers = {},
})
assert.same({ "http://localhost:8000", "-X", "GET", "-w", STAT_FORMAT }, args)
end)
it("from GET request with headers", function()
local args = builder.build({
context = Context:new(),
method = "GET",
url = "http://localhost:8000",
headers = {
["x-foo"] = { "bar" },
},
cookies = {},
handlers = {},
})
assert.same({ "http://localhost:8000", "-X", "GET", "-H", "X-Foo: bar", "-w", STAT_FORMAT }, args)
end)
it("from POST request with form body", function()
local args = builder.build({
context = Context:new(),
method = "POST",
url = "http://localhost:8000",
headers = {},
cookies = {},
handlers = {},
body = {
__TYPE = "raw",
data = "field1=value1&field2=value2",
},
})
assert.same(
{ "http://localhost:8000", "-X", "POST", "--data-raw", "field1=value1&field2=value2", "-w", STAT_FORMAT },
args
)
end)
it("from POST request with json body", function()
local json_text = [[{ "string": "foo", "number": 100, "array": [1, 2, 3], "json": { "key": "value" } }]]
local args = builder.build({
context = Context:new(),
method = "POST",
url = "http://localhost:8000",
headers = {},
cookies = {},
handlers = {},
body = {
__TYPE = "json",
data = json_text,
},
})
assert.same({ "http://localhost:8000", "-X", "POST", "--data-raw", json_text, "-w", STAT_FORMAT }, args)
end)
it("from POST request with external body", function()
local args = builder.build({
context = Context:new(),
method = "POST",
url = "http://localhost:8000",
headers = {},
cookies = {},
handlers = {},
body = {
__TYPE = "external",
data = {
path = "spec/test_server/post_json.json",
},
},
})
assert.same({
"http://localhost:8000",
"-X",
"POST",
"--data-binary",
"@spec/test_server/post_json.json",
"-w",
STAT_FORMAT,
}, args)
end)
it("with opts.set_compressed", function()
local args = builder.build({
context = Context:new(),
method = "POST",
url = "http://localhost:8000",
headers = {
["accept-encoding"] = { "gzip" },
},
cookies = {},
handlers = {},
})
assert.same({
"http://localhost:8000",
"--compressed",
"-X",
"POST",
"-H",
"Accept-Encoding: gzip",
"-w",
STAT_FORMAT,
}, args)
end)
end)
describe("curl cli response parser", function()
it("from http GET request", function()
local stdin = {
"* Trying 127.0.0.1:8000...",
"* Connected to localhost (127.0.0.1) port 8000 (#0)",
"> GET / HTTP/1.1",
"> Host: localhost:8000",
"> User-Agent: curl/7.81.0",
"> Accept: */*",
">",
"* Mark bundle as not supporting multiuse",
"< HTTP/1.1 200 OK",
"< Content-Type: text/plain",
"< Date: Tue, 06 Aug 2024 12:22:44 GMT",
"< Content-Length: 15",
"<",
"{ [15 bytes data]",
"* Connection #0 to host localhost left intact",
}
local response = parser.parse_verbose(stdin)
assert.same({
status = {
version = "HTTP/1.1",
code = 200,
text = "OK",
},
statistics = {},
headers = {
["content-type"] = { "text/plain" },
date = { "Tue, 06 Aug 2024 12:22:44 GMT" },
["content-length"] = { "15" },
},
}, response)
end)
end)
-- -- don't run real request on test by default
-- describe("curl cli request", function()
-- nio.tests.it("basic GET request", function()
-- local response = curl
-- .request({
-- context = Context:new(),
-- url = "https://reqres.in/api/users?page=5",
-- handlers = {},
-- headers = {},
-- cookies = {},
-- method = "GET",
-- })
-- .wait()
-- assert.same(
-- '{"page":5,"per_page":6,"total":12,"total_pages":2,"data":[],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}',
-- response.body
-- )
-- assert.same({
-- version = "HTTP/2",
-- code = 200,
-- text = ""
-- }, response.status)
-- -- HACK: have no idea how to make sure it is table<string,string>
-- assert.are_table(response.headers)
-- end)
-- nio.tests.it("basic POST request", function()
-- local response = curl
-- .request({
-- context = Context:new(),
-- url = "https://reqres.in/api/register",
-- handlers = {},
-- headers = {
-- ["content-type"] = { "application/json" }
-- },
-- cookies = {},
-- method = "POST",
-- body = {
-- __TYPE = "json",
-- data = '{ "email": "eve.holt@reqres.in", "password": "pistol" }',
-- }
-- })
-- .wait()
-- assert.same(
-- '{"id":4,"token":"QpwL5tke4Pnpja7X4"}',
-- response.body
-- )
-- assert.same({
-- version = "HTTP/2",
-- code = 200,
-- text = ""
-- }, response.status)
-- -- HACK: have no idea how to make sure it is table<string,string>
-- assert.are_table(response.headers)
-- end)
-- end)