Skip to content

Commit b5a2ddd

Browse files
committed
fix server and client tests
1 parent 10640b4 commit b5a2ddd

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

src/server.jl

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
module Servers
22

33
using JSON
4+
using HTTP
45

5-
import ..OpenAPI: APIModel, ValidationException, from_json
6+
import ..OpenAPI: APIModel, ValidationException, from_json, to_json
67

7-
# const OpenAPIParams = :openapi_params
8-
9-
function middleware(read, validate, invoke; init=nothing, pre_validation=nothing, pre_invoke=nothing, post_invoke=nothing)
10-
ret = (init === nothing) ? read : init |> read
11-
ret = (pre_validation === nothing) ? ret |> validate : ret |> pre_validation |> validate
12-
ret = (pre_invoke === nothing) ? ret |> invoke : ret |> pre_invoke |> invoke
13-
if post_invoke !== nothing
14-
ret = ret |> post_invoke
8+
function middleware(impl, read, validate, invoke;
9+
init=nothing,
10+
pre_validation=nothing,
11+
pre_invoke=nothing,
12+
post_invoke=nothing
13+
)
14+
handler = req -> (invoke(impl; post_invoke=post_invoke))(req)
15+
if !isnothing(pre_invoke)
16+
handler = pre_invoke(handler)
1517
end
16-
17-
return ret
18+
handler = validate(handler)
19+
if !isnothing(pre_validation)
20+
handler = pre_validation(handler)
21+
end
22+
handler = read(handler)
23+
if !isnothing(init)
24+
handler = init(handler)
25+
end
26+
return handler
1827
end
1928

2029
##############################
@@ -41,12 +50,21 @@ function to_param_type(::Type{T}, strval::String) where {T <: APIModel}
4150
from_json(T, JSON.parse(strval))
4251
end
4352

53+
function to_param_type(::Type{T}, json::Dict{String,Any}) where {T <: APIModel}
54+
from_json(T, json)
55+
end
56+
4457
function to_param_type(::Type{Vector{T}}, strval::String, delim::String) where {T}
45-
elems = strip.(split(strval, delim))
58+
elems = string.(strip.(split(strval, delim)))
4659
return map(x->to_param_type(T, x), elems)
4760
end
4861

49-
function to_param(T, source::Dict, name::String; required::Bool=false, collection_format::Union{String,Nothing}=nothing, multipart::Bool=false, isfile::Bool=false)
62+
function to_param_type(::Type{Vector{T}}, strval::String) where {T}
63+
elems = JSON.parse(strval)
64+
return map(x->to_param_type(T, x), elems)
65+
end
66+
67+
function to_param(T, source::Dict, name::String; required::Bool=false, collection_format::Union{String,Nothing}=",", multipart::Bool=false, isfile::Bool=false)
5068
param = get_param(source, name, required)
5169
if param === nothing
5270
return nothing
@@ -62,4 +80,9 @@ function to_param(T, source::Dict, name::String; required::Bool=false, collectio
6280
end
6381
end
6482

83+
server_response(resp::HTTP.Response) = resp
84+
server_response(::Nothing) = server_response("")
85+
server_response(ret) = server_response(to_json(ret))
86+
server_response(resp::String) = HTTP.Response(200, resp)
87+
6588
end # module Servers

test/client/petstore_v3/petstore_test_userapi.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const PRESET_TEST_USER = "user1" # this is the username that works for get us
1616

1717
function test_404(uri)
1818
@info("Error handling")
19-
client = Client(uri*"_invalid")
19+
client = Client(uri*"/invalid")
2020
api = UserApi(client)
2121

2222
try

0 commit comments

Comments
 (0)