Repository navigation
Expand file tree
/
Copy pathutil_spec.lua
More file actions
162 lines (162 loc) · 5.54 KB
/
Copy pathutil_spec.lua
File metadata and controls
162 lines (162 loc) · 5.54 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
describe("http.util module", function()
local unpack = table.unpack or unpack -- luacheck: ignore 113 143
local util = require "http.util"
it("decodeURI works", function()
assert.same("Encoded string", util.decodeURI("Encoded%20string"))
end)
it("decodeURI doesn't decode blacklisted characters", function()
assert.same("%24", util.decodeURI("%24"))
local s = util.encodeURIComponent("#$&+,/:;=?@")
assert.same(s, util.decodeURI(s))
end)
it("decodeURIComponent round-trips with encodeURIComponent", function()
local allchars do
local t = {}
for i=0, 255 do
t[i] = i
end
allchars = string.char(unpack(t, 0, 255))
end
assert.same(allchars, util.decodeURIComponent(util.encodeURIComponent(allchars)))
end)
it("query_args works", function()
do
local iter, state, first = util.query_args("foo=bar")
assert.same({"foo", "bar"}, {iter(state, first)})
assert.same(nil, iter(state, first))
end
do
local iter, state, first = util.query_args("foo=bar&baz=qux&foo=somethingelse")
assert.same({"foo", "bar"}, {iter(state, first)})
assert.same({"baz", "qux"}, {iter(state, first)})
assert.same({"foo", "somethingelse"}, {iter(state, first)})
assert.same(nil, iter(state, first))
end
do
local iter, state, first = util.query_args("%3D=%26")
assert.same({"=", "&"}, {iter(state, first)})
assert.same(nil, iter(state, first))
end
do
local iter, state, first = util.query_args("foo=bar&noequals")
assert.same({"foo", "bar"}, {iter(state, first)})
assert.same({"noequals", nil}, {iter(state, first)})
assert.same(nil, iter(state, first))
end
end)
it("dict_to_query works", function()
assert.same("foo=bar", util.dict_to_query{foo = "bar"})
assert.same("foo=%CE%BB", util.dict_to_query{foo = "λ"})
do
local t = {foo = "bar"; baz = "qux"}
local r = {}
for k, v in util.query_args(util.dict_to_query(t)) do
r[k] = v
end
assert.same(t, r)
end
end)
it("is_safe_method works", function()
assert.same(true, util.is_safe_method "GET")
assert.same(true, util.is_safe_method "HEAD")
assert.same(true, util.is_safe_method "OPTIONS")
assert.same(true, util.is_safe_method "TRACE")
assert.same(false, util.is_safe_method "POST")
assert.same(false, util.is_safe_method "PUT")
end)
it("is_ip works", function()
assert.same(true, util.is_ip "127.0.0.1")
assert.same(true, util.is_ip "192.168.1.1")
assert.same(true, util.is_ip "::")
assert.same(true, util.is_ip "::1")
assert.same(true, util.is_ip "2001:0db8:85a3:0042:1000:8a2e:0370:7334")
assert.same(true, util.is_ip "::FFFF:204.152.189.116")
assert.same(false, util.is_ip "not an ip")
assert.same(false, util.is_ip "0x80")
assert.same(false, util.is_ip "::FFFF:0.0.0")
end)
it("split_authority works", function()
assert.same({"example.com", 80}, {util.split_authority("example.com", "http")})
assert.same({"example.com", 8000}, {util.split_authority("example.com:8000", "http")})
assert.falsy(util.split_authority("example.com", "madeupscheme"))
-- IPv6
assert.same({"::1", 443}, {util.split_authority("[::1]", "https")})
assert.same({"::1", 8000}, {util.split_authority("[::1]:8000", "https")})
end)
it("to_authority works", function()
assert.same("example.com", util.to_authority("example.com", 80, "http"))
assert.same("example.com:8000", util.to_authority("example.com", 8000, "http"))
-- IPv6
assert.same("[::1]", util.to_authority("::1", 443, "https"))
assert.same("[::1]:8000", util.to_authority("::1", 8000, "https"))
end)
it("generates correct looking Date header format", function()
assert.same("Fri, 13 Feb 2009 23:31:30 GMT", util.imf_date(1234567890))
end)
describe("maybe_quote", function()
it("makes acceptable tokens or quoted-string", function()
assert.same([[foo]], util.maybe_quote([[foo]]))
assert.same([["with \" quote"]], util.maybe_quote([[with " quote]]))
end)
it("escapes all bytes correctly", function()
local http_patts = require "lpeg_patterns.http"
local s do -- Make a string containing every byte allowed in a quoted string
local t = {"\t"} -- tab
for i=32, 126 do
t[#t+1] = string.char(i)
end
for i=128, 255 do
t[#t+1] = string.char(i)
end
s = table.concat(t)
end
assert.same(s, http_patts.quoted_string:match(util.maybe_quote(s)))
end)
it("returns nil on invalid input", function()
local function check(s)
assert.same(nil, util.maybe_quote(s))
end
for i=0, 8 do
check(string.char(i))
end
-- skip tab
for i=10, 31 do
check(string.char(i))
end
check("\127")
end)
end)
describe("yieldable_pcall", function()
it("returns multiple return values", function()
assert.same({true, 1, 2, 3, 4, nil, nil, nil, nil, nil, nil, "foo"},
{util.yieldable_pcall(function() return 1, 2, 3, 4, nil, nil, nil, nil, nil, nil, "foo" end)})
end)
it("protects from errors", function()
assert.falsy(util.yieldable_pcall(error))
end)
it("returns error objects", function()
local err = {"myerror"}
local ok, err2 = util.yieldable_pcall(error, err)
assert.falsy(ok)
assert.equal(err, err2)
end)
it("works on all levels", function()
local f = coroutine.wrap(function()
return util.yieldable_pcall(coroutine.yield, true)
end)
assert.truthy(f()) -- 'true' that was yielded
assert.truthy(f()) -- 'true' from the pcall
assert.has.errors(f) -- cannot resume dead coroutine
end)
it("works with __call objects", function()
local done = false
local o = setmetatable({}, {
__call=function()
done = true
end;
})
util.yieldable_pcall(o)
assert.truthy(done)
end)
end)
end)