@@ -81,7 +81,7 @@ func testMethod(t *testing.T, r *http.Request, want string) {
8181 }
8282}
8383
84- func testRequestURL (t * testing.T , r * http.Request , want string ) {
84+ func testRequestURL (t * testing.T , r * http.Request , want string ) { // nolint: unparam
8585 if got := r .URL .String (); got != want {
8686 t .Errorf ("Request URL: %v, want %v" , got , want )
8787 }
@@ -264,6 +264,81 @@ func TestNewClient_BasicAuth(t *testing.T) {
264264 }
265265}
266266
267+ func TestNewClient_ReParseURL (t * testing.T ) {
268+ urls := map [string ][]string {
269+ "http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/[email protected] :5000/" : {
270+ "http://127.0.0.1:5000/" , "admin" , "ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg" ,
271+ },
272+ "http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/[email protected] :5000/foo" : {
273+ "http://127.0.0.1:5000/foo" , "admin" , "ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg" ,
274+ },
275+ "http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/[email protected] :5000" : {
276+ "http://127.0.0.1:5000" , "admin" , "ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg" ,
277+ },
278+ "https://admin:foo/bar@localhost:5" : {
279+ "https://localhost:5" , "admin" , "foo/bar" ,
280+ },
281+ }
282+ for input , expectations := range urls {
283+ submatches := gerrit .ReParseURL .FindAllStringSubmatch (input , - 1 )
284+ submatch := submatches [0 ]
285+ username := submatch [2 ]
286+ password := submatch [3 ]
287+ endpoint := fmt .Sprintf (
288+ "%s://%s:%s%s" , submatch [1 ], submatch [4 ], submatch [5 ], submatch [6 ])
289+ if endpoint != expectations [0 ] {
290+ t .Errorf ("%s != %s" , expectations [0 ], endpoint )
291+ }
292+ if username != expectations [1 ] {
293+ t .Errorf ("%s != %s" , expectations [1 ], username )
294+ }
295+ if password != expectations [2 ] {
296+ t .Errorf ("%s != %s" , expectations [2 ], password )
297+ }
298+
299+ }
300+ }
301+
302+ func TestNewClient_BasicAuth_PasswordWithSlashes (t * testing.T ) {
303+ setup ()
304+ defer teardown ()
305+
306+ account := gerrit.AccountInfo {
307+ AccountID : 100000 ,
308+ Name : "test" ,
309+ Email : "test@localhost" ,
310+ Username : "test" }
311+ hits := 0
312+
313+ testMux .HandleFunc ("/a/accounts/self" , func (w http.ResponseWriter , r * http.Request ) {
314+ hits ++
315+ switch hits {
316+ case 1 :
317+ writeresponse (t , w , nil , http .StatusUnauthorized )
318+ case 2 :
319+ // The second request should be a basic auth request if the first request, which is for
320+ // digest based auth, fails.
321+ if ! strings .HasPrefix (r .Header .Get ("Authorization" ), "Basic " ) {
322+ t .Error ("Missing 'Basic ' prefix" )
323+ }
324+ writeresponse (t , w , account , http .StatusOK )
325+ case 3 :
326+ t .Error ("Did not expect another request" )
327+ }
328+ })
329+
330+ serverURL := fmt .Sprintf (
331+ "http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg@%s" ,
332+ testServer .Listener .Addr ().String ())
333+ client , err := gerrit .NewClient (serverURL , nil )
334+ if err != nil {
335+ t .Error (err )
336+ }
337+ if ! client .Authentication .HasAuth () {
338+ t .Error ("Expected HasAuth() == true" )
339+ }
340+ }
341+
267342func TestNewClient_CookieAuth (t * testing.T ) {
268343 setup ()
269344 defer teardown ()
0 commit comments