@@ -8,9 +8,12 @@ defmodule Hex.HTTPTest do
88 Enum . map ( [ :proxy , :https_proxy ] , fn opt ->
99 :httpc . set_options ( [ { opt , { { 'localhost' , 80 } , [ 'localhost' ] } } ] , :hex )
1010 end )
11+
12+ System . delete_env ( "NETRC" )
1113 end )
1214
13- :ok
15+ bypass = Bypass . open ( )
16+ { :ok , bypass: bypass }
1417 end
1518
1619 test "proxy_config returns no credentials when no proxy supplied" do
@@ -51,4 +54,65 @@ defmodule Hex.HTTPTest do
5154 Hex.HTTP . handle_hex_message ( '"oops, you done goofed";level=fatal ' )
5255 assert_received { :mix_shell , :error , [ "API error: oops, you done goofed" ] }
5356 end
57+
58+ test "request adds no authorization header if none is given and no netrc is found" , % {
59+ bypass: bypass
60+ } do
61+ in_tmp ( fn ->
62+ Bypass . expect ( bypass , fn conn ->
63+ assert Plug.Conn . get_req_header ( conn , "authorization" ) == [ ]
64+ Plug.Conn . resp ( conn , 200 , "" )
65+ end )
66+
67+ Hex.HTTP . request ( :get , "http://localhost:#{ bypass . port } " , [ ] , nil )
68+ end )
69+ end
70+
71+ test "request adds authorization header based on netrc if none is given" , % { bypass: bypass } do
72+ in_tmp ( fn ->
73+ File . write! ( ".netrc" , """
74+ machine localhost
75+ login john
76+ password doe
77+ """ )
78+
79+ System . put_env ( "NETRC" , Path . join ( File . cwd! ( ) , ".netrc" ) )
80+
81+ Bypass . expect ( bypass , fn conn ->
82+ assert Plug.Conn . get_req_header ( conn , "authorization" ) == [
83+ "Basic #{ :base64 . encode ( "john:doe" ) } "
84+ ]
85+
86+ Plug.Conn . resp ( conn , 200 , "" )
87+ end )
88+
89+ Hex.HTTP . request ( :get , "http://localhost:#{ bypass . port } " , [ ] , nil )
90+ end )
91+ end
92+
93+ test "request adds no authorization header based on netrc if authorization is given" , % {
94+ bypass: bypass
95+ } do
96+ in_tmp ( fn ->
97+ File . write! ( ".netrc" , """
98+ machine localhost
99+ login john
100+ password doe
101+ """ )
102+
103+ System . put_env ( "NETRC" , Path . join ( File . cwd! ( ) , ".netrc" ) )
104+
105+ Bypass . expect ( bypass , fn conn ->
106+ assert Plug.Conn . get_req_header ( conn , "authorization" ) == [ "myAuthHeader" ]
107+ Plug.Conn . resp ( conn , 200 , "" )
108+ end )
109+
110+ Hex.HTTP . request (
111+ :get ,
112+ "http://localhost:#{ bypass . port } " ,
113+ [ { 'authorization' , 'myAuthHeader' } ] ,
114+ nil
115+ )
116+ end )
117+ end
54118end
0 commit comments