-
Notifications
You must be signed in to change notification settings - Fork 4
/
http-digest.test.lua
106 lines (82 loc) · 2.73 KB
/
http-digest.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
local cwtest = require "cwtest"
local ltn12 = require "ltn12"
local http_digest = require "http-digest"
local json_decode
do -- Find a JSON parser
local ok, json = pcall(require, "cjson")
if not ok then ok, json = pcall(require, "json") end
json_decode = json.decode
assert(ok and json_decode, "no JSON parser found :(")
end
-- To use a local httpbin-go build, use `localhost:8080`.
local httpbin_domain = "httpbingo.org"
-- `httpbin.org` returns {authenticated = true, user = "user"} instead.
local httpbin_authenticated = {authorized = true, user = "user"}
local httpbin_route = httpbin_domain .. "/digest-auth/auth/user/passwd"
local httpbin_route_no_auth = httpbin_domain .. "/get"
local url = {
good_creds = "http://user:passwd@" .. httpbin_route,
bad_creds = "http://user:nawak@" .. httpbin_route,
no_creds = "http://" .. httpbin_route,
good_creds_no_auth = "http://user:passwd@" .. httpbin_route_no_auth,
no_creds_no_auth = "http://" .. httpbin_route_no_auth,
}
local T = cwtest.new()
local b, c, _
T:start("basics")
-- simple interface
b, c = http_digest.request(url.good_creds)
T:eq( c, 200 )
T:eq( json_decode(b), httpbin_authenticated )
_, c = http_digest.request(url.bad_creds)
T:eq( c, 401 )
_, c = http_digest.request(url.no_creds)
T:eq( c, 401 )
b, c = http_digest.request(url.good_creds_no_auth)
T:eq( c, 200 )
T:eq( json_decode(b)["url"], url.no_creds_no_auth )
b, c = http_digest.request(url.no_creds_no_auth)
T:eq( c, 200 )
T:eq( json_decode(b)["url"], url.no_creds_no_auth )
-- generic interface
b = {}
_, c = http_digest.request {
url = url.good_creds,
sink = ltn12.sink.table(b),
}
T:eq( c, 200 )
b = table.concat(b)
T:eq( json_decode(b), httpbin_authenticated )
_, c = http_digest.request({url = url.bad_creds})
T:eq( c, 401 )
-- with ltn12 source
b = {}
_, c = http_digest.request {
url = url.good_creds,
sink = ltn12.sink.table(b),
source = ltn12.source.string("test"),
headers = {["content-length"] = 4}, -- 0 would work too
}
T:eq( c, 200 )
b = table.concat(b)
T:eq( select(2, b:gsub("{","")), 1 ) -- no duplicate JSON body
T:eq( json_decode(b), httpbin_authenticated )
T:done()
-- Copas HTTP client
if _VERSION ~= "Lua 5.1" then
-- We do not test Copas on Lua 5.1 because of this:
-- https://github.com/lunarmodules/copas/blob/27fb24326a8e513ae7ccf5b134a738f0de2f45c5/src/copas.lua#L16-L18
local copas = require "copas"
local default_http = http_digest.http
http_digest.http = require "copas.http"
T:start("copas")
b, c = nil, nil
copas.addthread(function()
b, c = http_digest.request(url.good_creds)
end)
copas()
T:eq( c, 200 )
T:eq( json_decode(b), httpbin_authenticated )
T:done()
http_digest.http = default_http
end