@@ -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
0 commit comments