@@ -137,3 +137,40 @@ func TestBasicAuth401WithCustomRealm(t *testing.T) {
137137 assert .Equal (t , http .StatusUnauthorized , w .Code )
138138 assert .Equal (t , "Basic realm=\" My Custom \\ \" Realm\\ \" \" " , w .Header ().Get ("WWW-Authenticate" ))
139139}
140+
141+ func TestBasicAuthForProxySucceed (t * testing.T ) {
142+ accounts := Accounts {"admin" : "password" }
143+ router := New ()
144+ router .Use (BasicAuthForProxy (accounts , "" ))
145+ router .Any ("/*proxyPath" , func (c * Context ) {
146+ c .String (http .StatusOK , c .MustGet (AuthProxyUserKey ).(string ))
147+ })
148+
149+ w := httptest .NewRecorder ()
150+ req , _ := http .NewRequest ("GET" , "/test" , nil )
151+ req .Header .Set ("Proxy-Authorization" , authorizationHeader ("admin" , "password" ))
152+ router .ServeHTTP (w , req )
153+
154+ assert .Equal (t , http .StatusOK , w .Code )
155+ assert .Equal (t , "admin" , w .Body .String ())
156+ }
157+
158+ func TestBasicAuthForProxy407 (t * testing.T ) {
159+ called := false
160+ accounts := Accounts {"foo" : "bar" }
161+ router := New ()
162+ router .Use (BasicAuthForProxy (accounts , "" ))
163+ router .Any ("/*proxyPath" , func (c * Context ) {
164+ called = true
165+ c .String (http .StatusOK , c .MustGet (AuthProxyUserKey ).(string ))
166+ })
167+
168+ w := httptest .NewRecorder ()
169+ req , _ := http .NewRequest ("GET" , "/test" , nil )
170+ req .Header .Set ("Proxy-Authorization" , "Basic " + base64 .StdEncoding .EncodeToString ([]byte ("admin:password" )))
171+ router .ServeHTTP (w , req )
172+
173+ assert .False (t , called )
174+ assert .Equal (t , http .StatusProxyAuthRequired , w .Code )
175+ assert .Equal (t , "Basic realm=\" Proxy Authorization Required\" " , w .Header ().Get ("Proxy-Authenticate" ))
176+ }
0 commit comments