-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.spec.lua
84 lines (74 loc) · 2.57 KB
/
http.spec.lua
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
local http = ...
local toJson = function(res) return res.json() end
mtt.register("Promise.http GET", function(callback)
Promise.http(http, "https://api.chucknorris.io/jokes/random"):next(function(res)
return res.json()
end):next(function(joke)
assert(type(joke.value) == "string")
callback()
end):catch(function(e)
callback(e)
end)
end)
mtt.register("Promise.http GET with async/await", function()
return Promise.async(function(await)
local joke = await(Promise.http(http, "https://api.chucknorris.io/jokes/random"):next(toJson))
assert(type(joke.value) == "string")
end)
end)
mtt.register("Promise.json GET with async/await", function()
return Promise.async(function(await)
local joke = await(Promise.json(http, "https://api.chucknorris.io/jokes/random"))
assert(type(joke.value) == "string")
end)
end)
mtt.register("Promise.http/all GET", function(callback)
local p1 = Promise.http(http, "https://api.chucknorris.io/jokes/random"):next(toJson)
local p2 = Promise.http(http, "https://api.chucknorris.io/jokes/random"):next(toJson)
Promise.all(p1, p2):next(function(values)
assert(#values == 2)
assert(type(values[1].value) == "string")
assert(type(values[2].value) == "string")
callback()
end):catch(function(e)
callback(e)
end)
end)
mtt.register("Promise.http GET not found", function(callback)
Promise.http(http, "https://httpbin.org/status/404"):next(function(res)
assert(res)
assert(res.code == 404)
callback()
end):catch(function(e)
callback(e)
end)
end)
mtt.register("Promise.http GET internal error", function(callback)
Promise.http(http, "https://httpbin.org/status/500"):next(function(res)
assert(res)
assert(res.code == 500)
callback()
end):catch(function(e)
callback(e)
end)
end)
mtt.register("Promise.json GET internal error", function(callback)
Promise.json(http, "https://httpbin.org/status/500"):next(function()
callback("unexpected success")
end, function(e)
assert(e == "unexpected status-code: 500")
callback()
end)
end)
mtt.register("Promise.http POST", function(callback)
local opts = { method = "POST", data = { x=1 }}
Promise.http(http, "https://postman-echo.com/post", opts):next(function(res)
return res.json()
end):next(function(data)
assert(type(data) == "table")
assert(data.json and data.json.x == 1)
callback()
end):catch(function(e)
callback(e)
end)
end)