forked from cfadmin-cn/cfadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_httpc.lua
More file actions
133 lines (114 loc) · 4.16 KB
/
test_httpc.lua
File metadata and controls
133 lines (114 loc) · 4.16 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
local cf = require "cf"
local httpc = require "httpc"
local http = require "httpc.class"
local json = require "json"
local system = require "system"
local now = system.now
require "utils"
-- 这里列举出了httpc库在各种情况下的使用方式.
local domain = 'http://localhost:8080' -- domain
-- local domain = 'http://127.0.0.1:8080' -- ipv4
-- local domain = 'http://[::1]:8080' -- ipv6
-- local domain = 'http://[fe80::875:95ce:bcaa:f66%en0]:8080' -- internal ipv6
cf.timeout(3, function ( ... )
cf.fork(function ( ... )
local t1 = now()
print("开始时间:", t1)
-- GET请求: 在参数固定的情况下可以直接写在url内
local code, body = httpc.get(domain.."/api?page=1&limit=10", {{"Auth", "admin"}})
print(code, body)
-- GET请求: 在参数为动态的情况下可以提供请求数组由httpc库进行拼接
local code, body = httpc.get(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})
print(code, body)
-- POST HEADER 为数组, BODY为数组
local code, body = httpc.post(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})
print(code, body)
-- POST HEADER 为数组, BODY为字符串
local code, body = httpc.post(domain.."/api", {{"Auth", "admin"}}, "page=1&limit=10")
print(code, body)
-- http json请求示例
local code, body = httpc.json(domain.."/api", {{"Auth", "admin"}}, json.encode({page=1, limit=10}))
print(code, body)
-- http 上传文件示例
local code, body = httpc.file(domain..'/api', nil, {
{name='1', filename='1.jpg', file='1', type='jpg'},
{name='2', filename='2.jpg', file='2', type='jpg'},
})
print(code, body)
local t2 = now()
print("结束时间:", t1, "总耗时:", t2 - t1)
end)
end)
cf.timeout(1, function ( ... )
cf.fork(function ( ... )
local hc = http:new {}
local t1 = now()
print("开始时间:", t1)
-- GET请求: 在参数固定的情况下可以直接写在url内
local code, body = hc:get(domain.."/api?page=1&limit=10", {{"Auth", "admin"}})
print(code, body)
-- GET请求: 在参数为动态的情况下可以提供请求数组由httpc库进行拼接
local code, body = hc:get(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})
print(code, body)
-- POST HEADER 为数组, BODY为数组
local code, body = hc:post(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})
print(code, body)
-- POST HEADER 为数组, BODY为字符串
local code, body = hc:post(domain.."/api", {{"Auth", "admin"}}, "page=1&limit=10")
print(code, body)
-- http json请求示例
local code, body = hc:json(domain.."/api", {{"Auth", "admin"}}, json.encode({page=1, limit=10}))
print(code, body)
local t2 = now()
print("结束时间:", t1, "总耗时:", t2 - t1)
hc:close()
end)
end)
cf.timeout(5, function ()
cf.fork(function ( ... )
local hc = http:new {}
local t1 = now()
print("开始时间:", t1)
local ok, response = hc:multi_request {
{
domain = domain.."/api",
method = "get",
headers = {{"Auth", "admin"}},
args = {{'page', 1}, {'limit', 10}}
},
{
domain = domain.."/api",
method = "post",
headers = {{"Auth", "admin"}},
body = {{'page', 1}, {'limit', 10}}
},
{
domain = domain.."/api",
method = "json",
headers = {{"Auth", "admin"}},
json = json.encode({page=1, limit=10})
},
{
domain = domain.."/api",
method = "file",
headers = {{"Auth", "admin"}},
files = {
{name='1', filename='1.jpg', file='1', type='jpg'},
{name='2', filename='2.jpg', file='2', type='jpg'},
}
}
}
local t2 = now()
print("结束时间:", t2, "总耗时:", t2 - t1)
require('logging'):new({path = 'test_httpc', dump=true}):DEBUG(response, "回应数量: " .. #response)
hc:close()
end)
end)
-- -- 如果有需要可以开启这段注释
-- local httpd = require "httpd"
-- local app = httpd:new()
-- app:api('/api', function (content)
-- return "{}"
-- end)
-- app:listen("", 8080)
-- app:run()