diff --git a/src/Messages.jl b/src/Messages.jl index f7b7cef8b..871eb6aa5 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -200,7 +200,14 @@ resource(uri::URI) = string( isempty(uri.path) ? "/" : uri.path, !isempty(uri.fragment) ? "#" : "", uri.fragment) mkheaders(h::Headers) = h -mkheaders(h)::Headers = Header[string(k) => string(v) for (k,v) in h] +function mkheaders(h)::Headers + # validation + foreach(h) do head + head isa String && throw(ArgumentError("header must be passed as key => value pair: `$head`")) + length(head) != 2 && throw(ArgumentError("invalid header key-value pair: $head")) + end + return Header[string(k) => string(v) for (k, v) in h] +end method(r::Request) = getfield(r, :method) target(r::Request) = getfield(r, :target) diff --git a/test/messages.jl b/test/messages.jl index 8a20ca68a..8c289b556 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -1,7 +1,7 @@ using Test using HTTP.Messages -import HTTP.Messages.appendheader +import HTTP.Messages: appendheader, mkheaders import HTTP.URI import HTTP.request import HTTP: bytes, nbytes @@ -17,6 +17,13 @@ using JSON http_reads = ["GET", "HEAD", "OPTIONS"] http_writes = ["POST", "PUT", "DELETE", "PATCH"] + @testset "Invalid headers" begin + @test_throws ArgumentError mkheaders(["hello"]) + @test_throws ArgumentError mkheaders([(1,2,3,4,5)]) + @test_throws ArgumentError mkheaders([1, 2]) + @test_throws ArgumentError mkheaders(["hello", "world"]) + end + @testset "Body Length" begin @test nbytes(7) === nothing @test nbytes(UInt8[1,2,3]) == 3