Skip to content

Commit 31ed43b

Browse files
committed
validate opts
1 parent e196c14 commit 31ed43b

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/mint/http1.ex

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ defmodule Mint.HTTP1 do
8383
"""
8484
@type error_reason() :: term()
8585

86+
@optional_responses_opts [:status_reason]
87+
8688
defstruct [
8789
:host,
8890
:port,
@@ -130,10 +132,11 @@ defmodule Mint.HTTP1 do
130132
conforming URIs but need to preserve them. The default is to validate the request
131133
target. *Available since v1.7.0*.
132134
* `:optional_responses` - (list of atoms) a list of optional responses to return.
133-
The possible values in the list are `:status_reason` which will return the
134-
[reason-phrase](https://datatracker.ietf.org/doc/html/rfc9112#name-status-line)
135-
for the status code, if it is returned by the server in status-line.
136-
This is only available for HTTP/1.1 connections. *Available since v1.7.2*.
135+
The possible values in the list are -
136+
* `:status_reason` which will return the
137+
[reason-phrase](https://datatracker.ietf.org/doc/html/rfc9112#name-status-line)
138+
for the status code, if it is returned by the server in status-line.
139+
This is only available for HTTP/1.1 connections. *Available since v1.7.2*.
137140
138141
"""
139142
@spec connect(Types.scheme(), Types.address(), :inet.port_number(), keyword()) ::
@@ -213,7 +216,7 @@ defmodule Mint.HTTP1 do
213216
log: log?,
214217
case_sensitive_headers: Keyword.get(opts, :case_sensitive_headers, false),
215218
skip_target_validation: Keyword.get(opts, :skip_target_validation, false),
216-
optional_responses: Keyword.get(opts, :optional_responses, [])
219+
optional_responses: validate_optional_response_values(opts)
217220
}
218221

219222
{:ok, conn}
@@ -224,6 +227,21 @@ defmodule Mint.HTTP1 do
224227
end
225228
end
226229

230+
defp validate_optional_response_values(opts) do
231+
opts
232+
|> Keyword.get(:optional_responses, [])
233+
|> Enum.map(fn opt ->
234+
if opt not in @optional_responses_opts do
235+
raise ArgumentError, """
236+
invalid :optional_responses value #{opt}.
237+
allowed values are - #{inspect(@optional_responses_opts)}
238+
"""
239+
end
240+
241+
opt
242+
end)
243+
end
244+
227245
@doc """
228246
See `Mint.HTTP.close/1`.
229247
"""

test/mint/http1/conn_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ defmodule Mint.HTTP1Test do
187187
refute HTTP1.open?(conn)
188188
end
189189

190+
test "raises error connecting with invalid optional_responses params", %{port: port} do
191+
assert_raise ArgumentError, fn ->
192+
HTTP1.connect(:http, "localhost", port, optional_responses: [:someting])
193+
end
194+
end
195+
190196
test "pipeline", %{conn: conn} do
191197
{:ok, conn, ref1} = HTTP1.request(conn, "GET", "/", [], nil)
192198
{:ok, conn, ref2} = HTTP1.request(conn, "GET", "/", [], nil)

0 commit comments

Comments
 (0)