forked from luapower/libcurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibcurl_test.lua
231 lines (214 loc) · 4.54 KB
/
libcurl_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
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
222
223
224
225
226
227
228
229
230
231
local curl = require'libcurl'
local ffi = require'ffi'
--add tests to the test table in order
local function add(test, name, func)
rawset(test, #test + 1, name)
rawset(test, name, func)
end
local test = setmetatable({}, {__newindex = add})
--[[
--TODO: load the default openssl file so it get get the path of the CA root file.
local crypto = require'libcrypto'
require'libcrypto_conf_h'
assert(crypto.CONF_modules_load_file('/etc/ssl/openssl.cnf',
nil, crypto.CONF_MFLAGS_DEFAULT_SECTION) == 1)
]]
function test.version()
print(curl.version())
end
local function list(v)
local t = {'{'}
for k,v in pairs(v) do
if v then
t[#t+1] = k..','
end
end
t[#t+1] = '}'
return table.concat(t)
end
function test.version_info()
local info = curl.version_info()
for k,v in pairs(info) do
if k == 'protocols' or k == 'features' then
v = list(v)
end
print(string.format('%-20s %s', k, v))
end
end
function make_easy()
return curl.easy{
url = 'http://google.com/',
verbose = false,
noprogress = true,
xferinfofunction = function(self, ...)
print(...)
return 0
end,
}
end
function dump_info(easy)
for _,k in ipairs{
'effective_url',
'response_code',
'total_time',
'namelookup_time',
'connect_time',
'pretransfer_time',
'size_upload',
'size_download',
'speed_download',
'speed_upload',
'header_size',
'request_size',
'ssl_verifyresult',
'filetime',
'content_length_download',
'content_length_upload',
'starttransfer_time',
'content_type',
'redirect_time',
'redirect_count',
'private',
'http_connectcode',
'httpauth_avail',
'proxyauth_avail',
'os_errno',
'num_connects',
'ssl_engines',
'cookielist',
'lastsocket',
'ftp_entry_path',
'redirect_url',
'primary_ip',
'appconnect_time',
'certinfo',
'condition_unmet',
'rtsp_session_id',
'rtsp_client_cseq',
'rtsp_server_cseq',
'rtsp_cseq_recv',
'primary_port',
'local_ip',
'local_port',
'tls_session',
'activesocket',
} do
local v = easy:info(k)
if type(v) == 'table' then
v = '{'..table.concat(v, ', ')..'}'
elseif ffi.istype('struct curl_tlssessioninfo*', v) then
v = 'backend: '..tonumber(v.backend)..'; internals: '..tostring(v.internals)
end
print(string.format('%-20s %s', k, v))
end
end
function test.easy()
local easy = make_easy()
easy:perform()
dump_info(easy)
easy:close()
end
function test.escape()
local easy = make_easy()
local url = ':/?+&='
local eurl = easy:escape(url)
local uurl = easy:unescape(eurl)
assert(url == uurl)
print(url, eurl)
easy:close()
end
function test.clone()
local e = make_easy()
e:clone():perform():close()
e:close()
end
function test.multi()
local m = curl.multi()
local e0 = make_easy()
local t = {}
for i = 1, 10 do
local e = e0:clone()
t[i] = e
m:add(e)
end
e0:close()
local n0
while true do
local n = m:perform()
if n == 0 then break end
if n0 ~= n then
print(n)
n0 = n
end
end
m:close()
for i = 1, #t do
t[i]:close()
end
end
function test.share()
local sh = curl.share{unshare = 'cookie', userdata = 123}
sh:set('share', 'dns')
sh:free()
end
function test.remove_cb()
local e = make_easy()
e:set('xferinfofunction', nil)
e:close()
end
function test.download()
local time = require'time'
local sh = curl.share()
for i = 1, 1 do
local t0 = time.time()
local t = {sz = 0}
local url = 'https://github.com/luapower/glue/archive/master.zip'
local total_size = 17954
local e = assert(curl.easy{
url = url,
share = sh,
accept_encoding = '',
--verbose = true,
dns_use_global_cache = true,
dns_cache_timeout = 999999,
followlocation = true,
writefunction = function(data, size)
if size == 0 then return end
table.insert(t, ffi.string(data, size))
t.sz = t.sz + size
local time_sofar = time.time() - t0
print(string.format('%2d%% %3dKB from %3dKB (%.2f KB/s)',
t.sz / total_size * 100,
t.sz / 1024,
total_size / 1024,
t.sz / 1024 / time_sofar))
return size
end,
})
assert(e:perform())
local s = table.concat(t)
local dt = time.time() - t0
print('DONE: ', #s, 'bytes')
e:close()
end
sh:free()
end
function test.mime()
local e = curl.easy()
local m = e:mime()
local p = m:part()
p:headers{'Some-Header: foo', 'Other-Header: bar'}
p:name'openssl-file'
p:filename'openssl-1.1.1d.tar.gz'
p:data(('hello\n'):rep(10)..'\nEND')
e:set('mimepost', m)
e:set('url', 'http://ptsv2.com/t/capr/post')
assert(e:perform())
e:close()
print'\nDone'
end
--run all tests in order
for i,name in ipairs(test) do
print(name .. ' ' .. ('-'):rep(78 - #name))
test[name]()
end