Skip to content

Commit e6cd21c

Browse files
committed
helper method to create HTTP response with code
Added a helper method to create `HTTP.Response` with a HTTP code and a `APIModel` instance. ```julia HTTP.Response(code::Integer, o::APIModel) ```
1 parent 9b6bf34 commit e6cd21c

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/server.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ function to_param(T, source::Vector{HTTP.Forms.Multipart}, name::String; require
109109
end
110110
end
111111

112+
function HTTP.Response(code::Integer, o::APIModel)
113+
return HTTP.Response(code, [Pair("Content-Type", "application/json")], to_json(o))
114+
end
115+
112116
server_response(resp::HTTP.Response) = resp
113117
server_response(::Nothing) = server_response("")
114118
server_response(ret) =

test/client/allany/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include(joinpath(@__DIR__, "AllAnyClient", "src", "AllAnyClient.jl"))
44
using .AllAnyClient
55
using Test
66
using JSON
7+
using HTTP
78
using OpenAPI
89
using OpenAPI.Clients
910
import OpenAPI.Clients: Client
@@ -146,4 +147,13 @@ function test_debug()
146147
end
147148
end
148149

150+
function test_http_resp()
151+
resp = HTTP.Response(200, dog)
152+
153+
@test resp.status == 200
154+
@test resp.headers == ["Content-Type" => "application/json"]
155+
json = JSON.parse(String(copy(resp.body)))
156+
@test pet_equals(OpenAPI.Clients.from_json(M.Dog, json), dog)
157+
end
158+
149159
end # module AllAnyTests

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,8 @@ include("forms/forms_client.jl")
149149
run_tests_with_servers && servers_running && stop_server(8081, ret, out)
150150
end
151151
end
152+
153+
@testset "Helper Methods" begin
154+
AllAnyTests.test_http_resp()
155+
end
152156
end

0 commit comments

Comments
 (0)