Skip to content

Commit 73e57de

Browse files
authored
Merge pull request #21 from danschultzer/handle-http-options
Validate http_server option
2 parents f9149b1 + 6431e13 commit 73e57de

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.1.15 (TBA)
4+
5+
- Validates `:http_server` option
6+
37
## v0.1.14 (2023-11-18)
48

59
- Fixed compiler warning in `TestServer.Instance`

lib/test_server/http_server.ex

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ defmodule TestServer.HTTPServer do
3232
@callback stop(instance(), server_options()) :: :ok | {:error, any()}
3333
@callback get_socket_pid(Plug.Conn.t()) :: pid()
3434

35-
@default_http_server Enum.find_value(
36-
[
37-
{Bandit, TestServer.HTTPServer.Bandit},
38-
{Plug.Cowboy, TestServer.HTTPServer.Plug.Cowboy},
39-
{:httpd, TestServer.HTTPServer.Httpd}
40-
],
41-
fn {dep, module} ->
42-
if Code.ensure_loaded?(dep), do: {module, []}
43-
end
44-
)
45-
4635
@doc false
4736
@spec start(pid(), keyword()) :: {:ok, keyword()} | {:error, any()}
4837
def start(instance, options) do
@@ -51,13 +40,7 @@ defmodule TestServer.HTTPServer do
5140
{tls_options, x509_options} = maybe_generate_x509_suite(options, scheme)
5241
ip_family = Keyword.get(options, :ipfamily, :inet)
5342
test_server_options = [tls: tls_options, ipfamily: ip_family]
54-
55-
{mod, server_options} =
56-
Keyword.get(
57-
options,
58-
:http_server,
59-
Application.get_env(:test_server, :http_server, @default_http_server)
60-
)
43+
{mod, server_options} = http_server(options)
6144

6245
case mod.start(instance, port, scheme, test_server_options, server_options) do
6346
{:ok, reference, server_options} ->
@@ -107,25 +90,46 @@ defmodule TestServer.HTTPServer do
10790
defp maybe_generate_x509_suite(options, :https) do
10891
tls_opts = Keyword.get(options, :tls, [])
10992

110-
case Keyword.has_key?(tls_opts, :key) || Keyword.has_key?(tls_opts, :keyfile) do
111-
true ->
112-
{tls_opts, []}
113-
114-
false ->
93+
case Keyword.take(tls_opts, [:key, :keyfile]) do
94+
[] ->
11595
suite = X509.Test.Suite.new()
11696

11797
{[
11898
key: {:RSAPrivateKey, X509.PrivateKey.to_der(suite.server_key)},
11999
cert: X509.Certificate.to_der(suite.valid),
120100
cacerts: suite.chain ++ suite.cacerts
121101
], x509_suite: suite}
102+
103+
[_ | _] ->
104+
{tls_opts, []}
122105
end
123106
end
124107

125108
defp maybe_generate_x509_suite(_options, :http) do
126109
{[], []}
127110
end
128111

112+
defp http_server(options) do
113+
case options[:http_server] || Application.get_env(:test_server, :http_server) ||
114+
default_http_server() do
115+
{mod, server_options} when is_atom(mod) and is_list(server_options) -> {mod, server_options}
116+
other -> raise("Invalid http_server, got: #{inspect(other)}")
117+
end
118+
end
119+
120+
defp default_http_server do
121+
cond do
122+
Code.ensure_loaded?(TestServer.HTTPServer.Bandit) ->
123+
{TestServer.HTTPServer.Bandit, []}
124+
125+
Code.ensure_loaded?(TestServer.HTTPServer.Plug.Cowboy) ->
126+
{TestServer.HTTPServer.Plug.Cowboy, []}
127+
128+
true ->
129+
{TestServer.HTTPServer.Httpd, []}
130+
end
131+
end
132+
129133
@doc false
130134
@spec stop(keyword()) :: :ok | {:error, any()}
131135
def stop(options) do

test/test_server_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ defmodule TestServerTest do
9494

9595
assert {:ok, _} = http1_request(TestServer.url("/"))
9696
end
97+
98+
test "with invalid http server" do
99+
assert_raise RuntimeError, ~r/Invalid http_server, got: :invalid/, fn ->
100+
TestServer.start(http_server: :invalid)
101+
end
102+
end
97103
end
98104

99105
describe "stop/1" do

0 commit comments

Comments
 (0)