-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathrequest.lua
66 lines (54 loc) · 1.55 KB
/
request.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
local copas = require("copas")
local http = copas.http
local url = assert(arg[1], "missing url argument")
local debug_mode = not not arg[2]
print("Testing copas.http.request with url " .. url .. (debug_mode and "(in debug mode)" or ""))
local switches, max_switches = 0, 10000000
local done = false
if debug_mode then
copas.debug.start()
local socket = require "socket"
local old_tcp = socket.tcp
socket.tcp = function(...)
local sock, err = old_tcp(...)
if not sock then
return sock, err
end
return copas.debug.socket(sock)
end
end
copas.addthread(function()
while switches < max_switches do
copas.pause()
switches = switches + 1
end
if not done then
print(("Error: Request not finished after %d thread switches"):format(switches))
os.exit(1)
end
end)
copas.addthread(function()
print("Starting request")
local content, code, headers, status = http.request(url)
print(("Finished request after %d thread switches"):format(switches))
if type(content) ~= "string" or type(code) ~= "number" or
type(headers) ~= "table" or type(status) ~= "string" then
print("Error: incorrect return values:")
print(content)
print(code)
print(headers)
print(status)
os.exit(1)
end
print(("Status: %s, content: %d bytes"):format(status, #content))
done = true
max_switches = switches + 10 -- just do a few more and finish the test
end)
print("Starting loop")
copas.loop()
if done then
print("Finished loop")
else
print("Error: Finished loop but request is not complete")
os.exit(1)
end