Skip to content

Commit 6b84f5a

Browse files
committed
Add code coverage badge to the README
1 parent cf80683 commit 6b84f5a

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/hpax.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@ defmodule HPAX do
119119
encode_headers(headers, table, _acc = [])
120120
end
121121

122+
@doc """
123+
Ecnodes a list of headers through the given table, applying the same `action` to all of them.
124+
125+
This function is the similar to `encode/2`, but `headers` are `{name, value}` tuples instead,
126+
and the same `action` is applied to all headers.
127+
128+
## Examples
129+
130+
headers = [{":authority", "https://example.com"}]
131+
encoding_context = HPAX.new(1000)
132+
HPAX.encode(:store, headers, encoding_context)
133+
#=> {iodata, updated_encoding_context}
134+
135+
"""
136+
# TODO: remove once we depend on Elixir 1.7+.
137+
if Version.match?(System.version(), ">= 1.7.0") do
138+
@doc since: "0.2.0"
139+
end
140+
141+
@spec encode(action, [header], Table.t()) :: {iodata(), Table.t()}
142+
when action: :store | :store_name | :no_store | :never_store,
143+
header: {header_name(), header_value()}
144+
def encode(action, headers, %Table{} = table)
145+
when is_list(headers) and action in [:store, :store_name, :no_store, :never_store] do
146+
headers
147+
|> Enum.map(fn {name, value} -> {action, name, value} end)
148+
|> encode(table)
149+
end
150+
122151
## Helpers
123152

124153
defp decode_headers(<<>>, table, acc) do

test/hpax_test.exs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ defmodule HPAXTest do
4747
assert dec_table.entries == [{"b", "B"}, {"a", "A"}]
4848
end
4949

50+
property "encode/3 with a single action" do
51+
table = HPAX.new(500)
52+
53+
check all action <- store_action(),
54+
headers <- list_of(header()) do
55+
assert {encoded, table} = HPAX.encode(action, headers, table)
56+
encoded = IO.iodata_to_binary(encoded)
57+
assert {:ok, decoded, _table} = HPAX.decode(encoded, table)
58+
assert decoded == headers
59+
end
60+
end
61+
5062
# https://datatracker.ietf.org/doc/html/rfc7541#section-4.2
5163
property "decode/2 accepts dynamic resizes at the start of a block" do
5264
enc_table = HPAX.new(20_000)
@@ -142,7 +154,7 @@ defmodule HPAXTest do
142154

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

@@ -160,4 +172,8 @@ defmodule HPAXTest do
160172
{2, random_header}
161173
])
162174
end
175+
176+
defp store_action do
177+
member_of([:store, :store_name, :no_store, :never_store])
178+
end
163179
end

0 commit comments

Comments
 (0)