@@ -47,7 +47,7 @@ defmodule TestServerTest do
4747
4848 assert % X509.Test.Suite { } = options [ :x509_suite ]
4949
50- httpc_opts = fn cacerts ->
50+ http_opts = fn cacerts ->
5151 [
5252 ssl: [
5353 verify: :verify_peer ,
@@ -65,10 +65,27 @@ defmodule TestServerTest do
6565 invalid_cacerts = X509.Test.Suite . new ( ) . cacerts
6666
6767 assert { :error , { :failed_connect , _ } } =
68- request ( TestServer . url ( "/" ) , httpc_opts: httpc_opts . ( invalid_cacerts ) )
68+ request ( TestServer . url ( "/" ) , http_opts: http_opts . ( invalid_cacerts ) )
6969
7070 assert :ok = TestServer . add ( "/" )
71- assert { :ok , _ } = request ( TestServer . url ( "/" ) , httpc_opts: httpc_opts . ( valid_cacerts ) )
71+ assert { :ok , _ } = request ( TestServer . url ( "/" ) , http_opts: http_opts . ( valid_cacerts ) )
72+ end
73+
74+ test "starts in IPv6-only mode`" do
75+ { :ok , instance } = TestServer . start ( ipfamily: :inet6 )
76+ options = TestServer.Instance . get_options ( instance )
77+
78+ assert options [ :ipfamily ] == :inet6
79+
80+ assert :ok = TestServer . add ( "/" , to: fn conn ->
81+ assert conn . remote_ip == { 0 , 0 , 0 , 0 , 0 , 65_535 , 32_512 , 1 }
82+
83+ Plug.Conn . send_resp ( conn , 200 , "OK" )
84+ end )
85+
86+ assert % { host: hostname } = URI . parse ( TestServer . url ( "/" ) )
87+ assert { :ok , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 } } == :inet . getaddr ( String . to_charlist ( hostname ) , :inet6 )
88+ assert { :ok , _ } = request ( TestServer . url ( "/" ) )
7289 end
7390 end
7491
@@ -148,7 +165,7 @@ defmodule TestServerTest do
148165 end
149166 end
150167
151- test "invalid `:host`" do
168+ test "with invalid `:host`" do
152169 TestServer . start ( )
153170
154171 assert_raise RuntimeError , ~r/ Invalid host, got: :invalid/ , fn ->
@@ -164,6 +181,32 @@ defmodule TestServerTest do
164181 refute TestServer . url ( "/" ) == TestServer . url ( "/" , host: "bad-host" )
165182 end
166183
184+ test "with `:host`" do
185+ TestServer . start ( )
186+
187+ assert :ok = TestServer . add ( "/" , to: fn conn ->
188+ assert conn . remote_ip == { 127 , 0 , 0 , 1 }
189+ assert conn . host == "custom-host"
190+
191+ Plug.Conn . send_resp ( conn , 200 , "OK" )
192+ end )
193+
194+ assert { :ok , _ } = request ( TestServer . url ( "/" , host: "custom-host" ) )
195+ end
196+
197+ test "with `:host` in IPv6-only mode" do
198+ TestServer . start ( ipfamily: :inet6 , http_server: { TestServer.HTTPServer.Httpd , [ ] } )
199+
200+ assert :ok = TestServer . add ( "/" , to: fn conn ->
201+ assert conn . remote_ip == { 0 , 0 , 0 , 0 , 0 , 65_535 , 32_512 , 1 }
202+ assert conn . host == "custom-host"
203+
204+ Plug.Conn . send_resp ( conn , 200 , "OK" )
205+ end )
206+
207+ assert { :ok , _ } = request ( TestServer . url ( "/" , host: "custom-host" ) )
208+ end
209+
167210 test "with multiple instances" do
168211 { :ok , instance_1 } = TestServer . start ( )
169212 { :ok , instance_2 } = TestServer . start ( )
@@ -674,13 +717,14 @@ defmodule TestServerTest do
674717
675718 def request ( url , opts \\ [ ] ) do
676719 url = String . to_charlist ( url )
677- httpc_opts = Keyword . get ( opts , :httpc_opts , [ ] )
720+ httpc_http_opts = Keyword . get ( opts , :http_opts , [ ] )
721+ httpc_opts = Keyword . get ( opts , :opts , [ ] )
678722
679723 opts
680724 |> Keyword . get ( :method , :get )
681725 |> case do
682- :post -> :httpc . request ( :post , { url , [ ] , 'plain/text' , 'OK' } , httpc_opts , [ ] )
683- :get -> :httpc . request ( :get , { url , [ ] } , httpc_opts , [ ] )
726+ :post -> :httpc . request ( :post , { url , [ ] , 'plain/text' , 'OK' } , httpc_http_opts , httpc_opts )
727+ :get -> :httpc . request ( :get , { url , [ ] } , httpc_http_opts , httpc_opts )
684728 end
685729 |> case do
686730 { :ok , { { _ , 200 , _ } , _headers , body } } -> { :ok , to_string ( body ) }
0 commit comments