Skip to content

Commit 8b20fc9

Browse files
mind6etpinard
authored andcommitted
made RequestHandlerFunction callable and added tests for it
1 parent 3019b25 commit 8b20fc9

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/HttpHelpers/handlers.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
struct RequestHandlerFunction <: HTTP.Handler
22
func::Function # func(req)
33
end
4+
(x::RequestHandlerFunction)(args...) = x.func(args...)
45

56
function handle(h::RequestHandlerFunction, request::HTTP.Request, args...)
6-
h.func(request, args...)
7+
h(request, args...)
78
end
89

910
function handle(handler::Function, request::HTTP.Request, args...)
1011
handler(request, args)
1112
end
1213

1314
function handle(h::RequestHandlerFunction, request::HTTP.Request, state, args...)
14-
h.func(request, state, args...)
15+
h(request, state, args...)
1516
end
1617

1718
function handle(handler::Function, request::HTTP.Request, state, args...)

test/handlers.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ end
165165
end
166166

167167
@testset "base_handler" begin
168+
function structsequal(a::T, b::T)::Bool where T
169+
for name in propertynames(a)
170+
if getfield(a, name) != getfield(b, name)
171+
return false
172+
end
173+
end
174+
return true
175+
end
176+
168177
base_handler = function(request, state)
169178
@test request.target == "/test_path"
170179
@test state isa TestState
@@ -176,6 +185,7 @@ end
176185
test_handler = state_handler(base_handler, state)
177186
test_request = HTTP.Request("GET", "/test_path")
178187
res = Dash.HttpHelpers.handle(test_handler, test_request)
188+
@test structsequal(res, test_handler(test_request)) #RequestHandlerFunction must be directly callable since HTTP.jl will use it
179189
@test res.status == 200
180190
@test String(res.body) == "test1"
181191
@test startswith(HTTP.header(res, "Content-Type", ""), "text/plain")
@@ -192,7 +202,8 @@ end
192202
test_handler = state_handler(base_handler_http, state)
193203
test_request = HTTP.Request("GET", "/test_path2")
194204
res = Dash.HttpHelpers.handle(test_handler, test_request)
195-
@test res.status == 200
205+
@test structsequal(res, test_handler(test_request))
206+
@test res.status == 200
196207
@test String(res.body) == "<html></html>"
197208
@test startswith(HTTP.header(res, "Content-Type", ""), "text/html")
198209
@test parse(Int, HTTP.header(res, "Content-Length", "0")) == sizeof("<html></html>")
@@ -207,7 +218,8 @@ end
207218
test_handler = state_handler(base_handler_js, state)
208219
test_request = HTTP.Request("GET", "/test_path3")
209220
res = Dash.HttpHelpers.handle(test_handler, test_request)
210-
@test res.status == 200
221+
@test structsequal(res, test_handler(test_request))
222+
@test res.status == 200
211223
@test String(res.body) == "<html></html>"
212224
@test startswith(HTTP.header(res, "Content-Type", ""), "text/javascript")
213225
@test parse(Int, HTTP.header(res, "Content-Length", "0")) == sizeof("<html></html>")

0 commit comments

Comments
 (0)