@@ -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,10 @@ 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 ) )
7272 end
7373
7474 test "starts in IPv6-only mode`" do
@@ -165,7 +165,7 @@ defmodule TestServerTest do
165165 end
166166 end
167167
168- test "invalid `:host`" do
168+ test "with invalid `:host`" do
169169 TestServer . start ( )
170170
171171 assert_raise RuntimeError , ~r/ Invalid host, got: :invalid/ , fn ->
@@ -181,6 +181,32 @@ defmodule TestServerTest do
181181 refute TestServer . url ( "/" ) == TestServer . url ( "/" , host: "bad-host" )
182182 end
183183
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+
184210 test "with multiple instances" do
185211 { :ok , instance_1 } = TestServer . start ( )
186212 { :ok , instance_2 } = TestServer . start ( )
@@ -691,13 +717,14 @@ defmodule TestServerTest do
691717
692718 def request ( url , opts \\ [ ] ) do
693719 url = String . to_charlist ( url )
694- httpc_opts = Keyword . get ( opts , :httpc_opts , [ ] )
720+ httpc_http_opts = Keyword . get ( opts , :http_opts , [ ] )
721+ httpc_opts = Keyword . get ( opts , :opts , [ ] )
695722
696723 opts
697724 |> Keyword . get ( :method , :get )
698725 |> case do
699- :post -> :httpc . request ( :post , { url , [ ] , 'plain/text' , 'OK' } , httpc_opts , [ ] )
700- :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 )
701728 end
702729 |> case do
703730 { :ok , { { _ , 200 , _ } , _headers , body } } -> { :ok , to_string ( body ) }
0 commit comments