Skip to content

Commit

Permalink
Add HTTP.removeheader and some missing getparam(s) tests (#883)
Browse files Browse the repository at this point in the history
* Add HTTP.removeheader and some missing getparam(s) tests

* fix tests

* fix getparam export
  • Loading branch information
quinnj authored Jul 16, 2022
1 parent ce8601c commit cf706bc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ HTTP.Messages.hasheader
HTTP.Messages.headercontains
HTTP.Messages.setheader
HTTP.Messages.appendheader
HTTP.Messages.removeheader
HTTP.Messages.decode
```

Expand Down
2 changes: 1 addition & 1 deletion src/Handlers.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Handlers

export Handler, Middleware, serve, serve!, Router, register!, getparams, getcookies, streamhandler
export Handler, Middleware, serve, serve!, Router, register!, getparams, getparam, getcookies, streamhandler

using URIs
using ..Messages, ..Streams, ..IOExtras, ..Servers, ..Sockets, ..Cookies
Expand Down
9 changes: 8 additions & 1 deletion src/Messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export Message, Request, Response,
reset!, status, method, headers, uri, body, resource,
iserror, isredirect, retryable, ischunked, issafe, isidempotent,
header, hasheader, headercontains, setheader, defaultheader!, appendheader,
mkheaders, readheaders, headerscomplete,
removeheader, mkheaders, readheaders, headerscomplete,
readchunksize,
writeheaders, writestartline,
bodylength, unknown_length,
Expand Down Expand Up @@ -413,6 +413,13 @@ function appendheader(m::Message, header::Header)
return
end

"""
HTTP.removeheader(::Message, key)
Remove header for `key` (case-insensitive).
"""
removeheader(m::Message, header::String) = rmkv(m.headers, header, field_name_isequal)

# HTTP payload body
function payload(m::Message)::Vector{UInt8}
enc = lowercase(first(split(header(m, "Transfer-Encoding"), ", ")))
Expand Down
4 changes: 2 additions & 2 deletions src/Pairs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ end
Remove `key` from `collection` of key/value `Pairs`.
"""
function rmkv(c, k, default=nothing)
i = findfirst(x->first(x) == k, c)
function rmkv(c, k, eq = ==)
i = findfirst(x->eq(first(x), k), c)
if !(i === nothing)
deleteat!(c, i)
end
Expand Down
4 changes: 2 additions & 2 deletions test/handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ using HTTP, Test
@test r(HTTP.Request("GET", "/test/sarv/ghotra/seven")) == 10
@test r(HTTP.Request("GET", "/test/foo")) == 8

HTTP.register!(r, "/api/issue/{issue_id}", req -> req.context[:params]["issue_id"])
HTTP.register!(r, "/api/issue/{issue_id}", req -> HTTP.getparams(req)["issue_id"])
@test r(HTTP.Request("GET", "/api/issue/871")) == "871"

HTTP.register!(r, "/api/widgets/{id}", req -> req.context[:params]["id"])
HTTP.register!(r, "/api/widgets/{id}", req -> HTTP.getparam(req, "id"))
@test r(HTTP.Request("GET", "/api/widgets/11")) == "11"

HTTP.register!(r, "/api/widgets/{name}", req -> (req.context[:params]["name"], req.context[:route]))
Expand Down
3 changes: 3 additions & 0 deletions test/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ using JSON

setheader(req, "X" => "Y")
@test header(req, "X") == "Y"
removeheader(req, "X")
@test header(req, "X") == ""
setheader(req, "X" => "Y")
end

@testset "Response" begin
Expand Down

0 comments on commit cf706bc

Please sign in to comment.