@@ -27,6 +27,16 @@ defmodule Plug.RewriteOnTest do
2727 assert conn . port == 443
2828 end
2929
30+ test "rewrites http to https based on a custom header" do
31+ conn =
32+ conn ( :get , "http://example.com/" )
33+ |> put_req_header ( "custom-forwarded-proto" , "https" )
34+ |> call ( { :scheme , "custom-forwarded-proto" } )
35+
36+ assert conn . scheme == :https
37+ assert conn . port == 443
38+ end
39+
3040 test "doesn't change the port when it doesn't match the scheme" do
3141 conn =
3242 conn ( :get , "http://example.com:1234/" )
@@ -46,6 +56,15 @@ defmodule Plug.RewriteOnTest do
4656 assert conn . host == "truessl.example.com"
4757 end
4858
59+ test "rewrites host with a custom header" do
60+ conn =
61+ conn ( :get , "http://example.com/" )
62+ |> put_req_header ( "custom-forwarded-host" , "truessl.example.com" )
63+ |> call ( { :host , "custom-forwarded-host" } )
64+
65+ assert conn . host == "truessl.example.com"
66+ end
67+
4968 test "rewrites port with a x-forwarded-port header" do
5069 conn =
5170 conn ( :get , "http://example.com/" )
@@ -55,6 +74,15 @@ defmodule Plug.RewriteOnTest do
5574 assert conn . port == 3030
5675 end
5776
77+ test "rewrites port with a custom header" do
78+ conn =
79+ conn ( :get , "http://example.com/" )
80+ |> put_req_header ( "custom-forwarded-port" , "3030" )
81+ |> call ( { :port , "custom-forwarded-port" } )
82+
83+ assert conn . port == 3030
84+ end
85+
5886 test "rewrites remote_ip with a x-forwarded-for header" do
5987 conn =
6088 conn ( :get , "http://example.com/" )
@@ -85,6 +113,38 @@ defmodule Plug.RewriteOnTest do
85113 assert conn . remote_ip == { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 }
86114 end
87115
116+
117+ test "rewrites remote_ip with a custom header" do
118+ conn =
119+ conn ( :get , "http://example.com/" )
120+ |> put_req_header ( "custom-forwarded-for" , "bad" )
121+ |> call ( { :remote_ip , "custom-forwarded-for" } )
122+
123+ assert conn . remote_ip == { 127 , 0 , 0 , 1 }
124+
125+ conn =
126+ conn ( :get , "http://example.com/" )
127+ |> put_req_header ( "custom-forwarded-for" , "4.3.2.1" )
128+ |> call ( { :remote_ip , "custom-forwarded-for" } )
129+
130+ assert conn . remote_ip == { 4 , 3 , 2 , 1 }
131+
132+ conn =
133+ conn ( :get , "http://example.com/" )
134+ |> put_req_header ( "custom-forwarded-for" , "1.2.3.4,::1" )
135+ |> call ( { :remote_ip , "custom-forwarded-for" } )
136+
137+ assert conn . remote_ip == { 1 , 2 , 3 , 4 }
138+
139+ conn =
140+ conn ( :get , "http://example.com/" )
141+ |> put_req_header ( "custom-forwarded-for" , "::1,1.2.3.4" )
142+ |> call ( { :remote_ip , "custom-forwarded-for" } )
143+
144+ assert conn . remote_ip == { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 }
145+ end
146+
147+
88148 test "rewrites the host, the port, and the protocol" do
89149 conn =
90150 conn ( :get , "http://example.com/" )
@@ -98,9 +158,28 @@ defmodule Plug.RewriteOnTest do
98158 assert conn . scheme == :https
99159 end
100160
161+
162+ test "rewrites the host, the port, and the protocol with custom headers" do
163+ conn =
164+ conn ( :get , "http://example.com/" )
165+ |> put_req_header ( "custom-forwarded-host" , "truessl.example.com" )
166+ |> put_req_header ( "custom-forwarded-port" , "3030" )
167+ |> put_req_header ( "custom-forwarded-proto" , "https" )
168+ |> call ( [ host: "custom-forwarded-host" ,
169+ port: "custom-forwarded-port" ,
170+ scheme: "custom-forwarded-proto" ] )
171+
172+ assert conn . host == "truessl.example.com"
173+ assert conn . port == 3030
174+ assert conn . scheme == :https
175+ end
176+
101177 test "raises when receiving an unknown rewrite" do
102178 assert_raise RuntimeError , "unknown rewrite: :x_forwarded_other" , fn ->
103179 call ( conn ( :get , "http://example.com/" ) , :x_forwarded_other )
104180 end
181+ assert_raise RuntimeError , "unknown rewrite: {:other, \" value\" }" , fn ->
182+ call ( conn ( :get , "http://example.com/" ) , [ other: "value" ] )
183+ end
105184 end
106185end
0 commit comments