-
Notifications
You must be signed in to change notification settings - Fork 23
/
multipart-post.test.lua
73 lines (63 loc) · 1.81 KB
/
multipart-post.test.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
local cwtest = require "cwtest"
local ltn12 = require "ltn12"
local H = (require "socket.http").request
local mp = (require "multipart-post").gen_request
local enc = (require "multipart-post").encode
local J
do -- Find a JSON parser
local ok, json = pcall(require, "cjson")
if not ok then ok, json = pcall(require, "json") end
J = json.decode
assert(ok and J, "no JSON parser found :(")
end
local file = io.open("testfile.tmp", "w+")
file:write("file data")
file:flush()
file:seek("set", 0)
local fsz = file:seek("end")
file:seek("set", 0)
local T = cwtest.new()
T:start("gen_request"); do
local r = {}
local rq = mp{
myfile = {name = "myfilename", data = "some data"},
diskfile = {name = "diskfilename", data = file, len = fsz},
ltn12file = {
name = "ltn12filename",
data = ltn12.source.string("ltn12 data"),
len = string.len("ltn12 data")
},
foo = "bar",
}
rq.url = "http://httpbin.org/post"
rq.sink = ltn12.sink.table(r)
local _, c = H(rq)
T:eq(c, 200)
r = J(table.concat(r))
T:eq(r.files, {
myfile="some data",
diskfile="file data",
ltn12file="ltn12 data"
})
T:eq(r.form, {foo = "bar"})
end; T:done()
T:start("encode"); do
local body, boundary = enc{foo="bar"}
local r = {}
local _, c = H{
url = "http://httpbin.org/post",
source = ltn12.source.string(body),
method = "POST",
sink = ltn12.sink.table(r),
headers = {
["content-length"] = string.len(body),
["content-type"] = string.format(
"multipart/form-data; boundary=\"%s\"", boundary
),
},
}
T:eq(c, 200)
r = J(table.concat(r))
T:eq(r.form, {foo = "bar"})
end; T:done()
os.remove("testfile.tmp")