Skip to content

Commit

Permalink
Merge pull request #153 from inhabitedtype/headers-remove
Browse files Browse the repository at this point in the history
Fix bug in Headers.remove
  • Loading branch information
seliopou committed Oct 3, 2019
2 parents d9c7d0a + c0c1935 commit e82623e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/headers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ let replace t name value =
with Local -> t

let remove t name =
let rec loop s n seen =
let rec loop s needle seen =
match s with
| [] ->
if not seen then raise Local else []
| (n',_ as nv')::s' ->
if CI.equal n n' then loop s' n true else nv'::(loop s' n false)
| (name,_ as nv')::s' ->
if CI.equal needle name
then (print_endline "seen: true"; loop s' needle true )
else (print_endline "seen: false"; nv'::(loop s' needle seen))
in
try loop t name false
with Local -> t
Expand Down
24 changes: 24 additions & 0 deletions lib_test/test_httpaf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ module IOVec = struct
; "shiftv raises ", `Quick, test_shiftv_raises ]
end

module Headers = struct
include Headers

let test_remove () =
let check = Alcotest.(check (list (pair string string))) in
check "remove leading element"
["c", "d"]
(Headers.remove
(Headers.of_list ["a", "b"; "c", "d"])
"a"
|> Headers.to_list);
check "remove trailing element"
["c", "d"]
(Headers.remove
(Headers.of_list ["c", "d"; "a", "b"])
"a"
|> Headers.to_list);
;;

let tests =
[ "remove", `Quick, test_remove ]
end

let maybe_serialize_body f body =
match body with
| None -> ()
Expand Down Expand Up @@ -793,6 +816,7 @@ let () =
[ "version" , Version.tests
; "method" , Method.tests
; "iovec" , IOVec.tests
; "headers" , Headers.tests
; "client connection", Client_connection.tests
; "server connection", Server_connection.tests
]

0 comments on commit e82623e

Please sign in to comment.