@@ -589,6 +589,42 @@ defmodule ReverseProxyPlugTest do
589589 assert_header ( headers , "host" , [ "example-custom-port.com" ] )
590590 end
591591
592+ test_stream_and_buffer "preserve host header with port number when preserve_host_header is true" do
593+ % { req_function: req_function , opts: opts , get_responder: get_responder } = test_reuse_opts
594+ opts_with_preserve = Keyword . merge ( opts , preserve_host_header: true )
595+
596+ ReverseProxyPlug.HTTPClientMock
597+ |> expect ( req_function , fn % { headers: headers } = request ->
598+ send ( self ( ) , { :headers , headers } )
599+ get_responder . ( % { } ) . ( request )
600+ end )
601+
602+ conn ( :get , "/" )
603+ |> Map . put ( :req_headers , [ { "host" , "original-host.com:8080" } ] )
604+ |> ReverseProxyPlug . call ( ReverseProxyPlug . init ( opts_with_preserve ) )
605+
606+ assert_receive { :headers , headers }
607+ assert_header ( headers , "host" , [ "original-host.com:8080" ] )
608+ end
609+
610+ test_stream_and_buffer "preserve host header without port when preserve_host_header is true and no port in original request" do
611+ % { req_function: req_function , opts: opts , get_responder: get_responder } = test_reuse_opts
612+ opts_with_preserve = Keyword . merge ( opts , preserve_host_header: true )
613+
614+ ReverseProxyPlug.HTTPClientMock
615+ |> expect ( req_function , fn % { headers: headers } = request ->
616+ send ( self ( ) , { :headers , headers } )
617+ get_responder . ( % { } ) . ( request )
618+ end )
619+
620+ conn ( :get , "/" )
621+ |> Map . put ( :req_headers , [ { "host" , "original-host.com" } ] )
622+ |> ReverseProxyPlug . call ( ReverseProxyPlug . init ( opts_with_preserve ) )
623+
624+ assert_receive { :headers , headers }
625+ assert_header ( headers , "host" , [ "original-host.com" ] )
626+ end
627+
592628 for timeout_reason <- ReverseProxyPlug . get_timeout_error_reasons ( ) do
593629 test_stream_and_buffer "returns gateway timeout on #{ inspect ( timeout_reason ) } as error reason" do
594630 % { req_function: req_function , opts: opts } = test_reuse_opts
0 commit comments