From cf706bc293d9d530c97ffc916d5fd9d47b3ab1a2 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Fri, 15 Jul 2022 22:33:47 -0600 Subject: [PATCH] Add HTTP.removeheader and some missing getparam(s) tests (#883) * Add HTTP.removeheader and some missing getparam(s) tests * fix tests * fix getparam export --- docs/src/reference.md | 1 + src/Handlers.jl | 2 +- src/Messages.jl | 9 ++++++++- src/Pairs.jl | 4 ++-- test/handlers.jl | 4 ++-- test/messages.jl | 3 +++ 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/src/reference.md b/docs/src/reference.md index 218044979..921d878b1 100644 --- a/docs/src/reference.md +++ b/docs/src/reference.md @@ -32,6 +32,7 @@ HTTP.Messages.hasheader HTTP.Messages.headercontains HTTP.Messages.setheader HTTP.Messages.appendheader +HTTP.Messages.removeheader HTTP.Messages.decode ``` diff --git a/src/Handlers.jl b/src/Handlers.jl index 0c65af6d4..5ce2770aa 100644 --- a/src/Handlers.jl +++ b/src/Handlers.jl @@ -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 diff --git a/src/Messages.jl b/src/Messages.jl index 37cd08444..e1f9bfda2 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -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, @@ -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"), ", "))) diff --git a/src/Pairs.jl b/src/Pairs.jl index 83542a595..105dabee9 100644 --- a/src/Pairs.jl +++ b/src/Pairs.jl @@ -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 diff --git a/test/handlers.jl b/test/handlers.jl index c6c9f37c4..02b602721 100644 --- a/test/handlers.jl +++ b/test/handlers.jl @@ -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])) diff --git a/test/messages.jl b/test/messages.jl index 6a507da58..8a20ca68a 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -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