Skip to content

Commit

Permalink
Add code coverage badge to the README
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Jan 13, 2023
1 parent cf80683 commit 6b84f5a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
29 changes: 29 additions & 0 deletions lib/hpax.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,35 @@ defmodule HPAX do
encode_headers(headers, table, _acc = [])
end

@doc """
Ecnodes a list of headers through the given table, applying the same `action` to all of them.
This function is the similar to `encode/2`, but `headers` are `{name, value}` tuples instead,
and the same `action` is applied to all headers.
## Examples
headers = [{":authority", "https://example.com"}]
encoding_context = HPAX.new(1000)
HPAX.encode(:store, headers, encoding_context)
#=> {iodata, updated_encoding_context}
"""
# TODO: remove once we depend on Elixir 1.7+.
if Version.match?(System.version(), ">= 1.7.0") do
@doc since: "0.2.0"
end

@spec encode(action, [header], Table.t()) :: {iodata(), Table.t()}
when action: :store | :store_name | :no_store | :never_store,
header: {header_name(), header_value()}
def encode(action, headers, %Table{} = table)
when is_list(headers) and action in [:store, :store_name, :no_store, :never_store] do
headers
|> Enum.map(fn {name, value} -> {action, name, value} end)
|> encode(table)
end

## Helpers

defp decode_headers(<<>>, table, acc) do
Expand Down
18 changes: 17 additions & 1 deletion test/hpax_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ defmodule HPAXTest do
assert dec_table.entries == [{"b", "B"}, {"a", "A"}]
end

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

check all action <- store_action(),
headers <- list_of(header()) do
assert {encoded, table} = HPAX.encode(action, headers, table)
encoded = IO.iodata_to_binary(encoded)
assert {:ok, decoded, _table} = HPAX.decode(encoded, table)
assert decoded == headers
end
end

# https://datatracker.ietf.org/doc/html/rfc7541#section-4.2
property "decode/2 accepts dynamic resizes at the start of a block" do
enc_table = HPAX.new(20_000)
Expand Down Expand Up @@ -142,7 +154,7 @@ defmodule HPAXTest do

# Header generator.
defp header_with_action() do
action = member_of([:store, :store_name, :no_store, :never_store])
action = store_action()
bind(header(), fn {name, value} -> {action, constant(name), constant(value)} end)
end

Expand All @@ -160,4 +172,8 @@ defmodule HPAXTest do
{2, random_header}
])
end

defp store_action do
member_of([:store, :store_name, :no_store, :never_store])
end
end

0 comments on commit 6b84f5a

Please sign in to comment.