Skip to content

Commit

Permalink
Merge pull request #21 from danschultzer/handle-http-options
Browse files Browse the repository at this point in the history
Validate http_server option
  • Loading branch information
danschultzer committed Feb 26, 2024
2 parents f9149b1 + 6431e13 commit 73e57de
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.1.15 (TBA)

- Validates `:http_server` option

## v0.1.14 (2023-11-18)

- Fixed compiler warning in `TestServer.Instance`
Expand Down
50 changes: 27 additions & 23 deletions lib/test_server/http_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ defmodule TestServer.HTTPServer do
@callback stop(instance(), server_options()) :: :ok | {:error, any()}
@callback get_socket_pid(Plug.Conn.t()) :: pid()

@default_http_server Enum.find_value(
[
{Bandit, TestServer.HTTPServer.Bandit},
{Plug.Cowboy, TestServer.HTTPServer.Plug.Cowboy},
{:httpd, TestServer.HTTPServer.Httpd}
],
fn {dep, module} ->
if Code.ensure_loaded?(dep), do: {module, []}
end
)

@doc false
@spec start(pid(), keyword()) :: {:ok, keyword()} | {:error, any()}
def start(instance, options) do
Expand All @@ -51,13 +40,7 @@ defmodule TestServer.HTTPServer do
{tls_options, x509_options} = maybe_generate_x509_suite(options, scheme)
ip_family = Keyword.get(options, :ipfamily, :inet)
test_server_options = [tls: tls_options, ipfamily: ip_family]

{mod, server_options} =
Keyword.get(
options,
:http_server,
Application.get_env(:test_server, :http_server, @default_http_server)
)
{mod, server_options} = http_server(options)

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

case Keyword.has_key?(tls_opts, :key) || Keyword.has_key?(tls_opts, :keyfile) do
true ->
{tls_opts, []}

false ->
case Keyword.take(tls_opts, [:key, :keyfile]) do
[] ->
suite = X509.Test.Suite.new()

{[
key: {:RSAPrivateKey, X509.PrivateKey.to_der(suite.server_key)},
cert: X509.Certificate.to_der(suite.valid),
cacerts: suite.chain ++ suite.cacerts
], x509_suite: suite}

[_ | _] ->
{tls_opts, []}
end
end

defp maybe_generate_x509_suite(_options, :http) do
{[], []}
end

defp http_server(options) do
case options[:http_server] || Application.get_env(:test_server, :http_server) ||
default_http_server() do
{mod, server_options} when is_atom(mod) and is_list(server_options) -> {mod, server_options}
other -> raise("Invalid http_server, got: #{inspect(other)}")
end
end

defp default_http_server do
cond do
Code.ensure_loaded?(TestServer.HTTPServer.Bandit) ->
{TestServer.HTTPServer.Bandit, []}

Code.ensure_loaded?(TestServer.HTTPServer.Plug.Cowboy) ->
{TestServer.HTTPServer.Plug.Cowboy, []}

true ->
{TestServer.HTTPServer.Httpd, []}
end
end

@doc false
@spec stop(keyword()) :: :ok | {:error, any()}
def stop(options) do
Expand Down
6 changes: 6 additions & 0 deletions test/test_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ defmodule TestServerTest do

assert {:ok, _} = http1_request(TestServer.url("/"))
end

test "with invalid http server" do
assert_raise RuntimeError, ~r/Invalid http_server, got: :invalid/, fn ->
TestServer.start(http_server: :invalid)
end
end
end

describe "stop/1" do
Expand Down

0 comments on commit 73e57de

Please sign in to comment.