Skip to content

Improve error message on invalid headers #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/hpax.ex
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ defmodule HPAX do
end

defp encode_headers([{action, name, value} | rest], table, acc)
when action in @valid_header_actions and is_binary(name) and is_binary(value) do
when action in @valid_header_actions do
validate_header(name, value)
huffman? = table.huffman_encoding == :always

{encoded, table} =
Expand Down Expand Up @@ -330,6 +331,15 @@ defmodule HPAX do
encode_headers(rest, table, [acc, encoded])
end

defp validate_header(name, value) when is_binary(name) and is_binary(value) do
:ok
end

defp validate_header(name, value) do
raise ArgumentError,
"expected header name/value to be strings, got: #{inspect(name)}/#{inspect(value)}"
end

defp encode_indexed_header(index) do
<<1::1, Types.encode_integer(index, 7)::bitstring>>
end
Expand Down
16 changes: 16 additions & 0 deletions test/hpax_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ defmodule HPAXTest do
assert dec_table.entries == [{"b", "B"}, {"a", "A"}]
end

test "encode/3 with invalid headers" do
table = HPAX.new(1000)

assert_raise ArgumentError,
~s(expected header name/value to be strings, got: :foo/"bar"),
fn ->
HPAX.encode([{:store, :foo, "bar"}], table)
end

assert_raise ArgumentError,
~s(expected header name/value to be strings, got: "foo"/:bar),
fn ->
HPAX.encode([{:store, "foo", :bar}], table)
end
end

property "encode/3 with a single action" do
table = HPAX.new(500)

Expand Down
Loading