-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net/http: don't overwrite Authorization headers when URL has username
Fixes golang#11399 Change-Id: I3be7fbc86c5f62761f47122632f3e11b56cb6be6
- Loading branch information
Showing
2 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -843,6 +843,47 @@ func TestBasicAuth(t *testing.T) { | |
} | ||
} | ||
|
||
func TestBasicAuthHeadersPreserved(t *testing.T) { | ||
defer afterTest(t) | ||
tr := &recordingTransport{} | ||
client := &Client{Transport: tr} | ||
|
||
// If Authorization header is provided, username in URL should not override it | ||
url := "http://My%[email protected]/" | ||
req, err := NewRequest("GET", url, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req.SetBasicAuth("My User", "My Pass") | ||
expected := "My User:My Pass" | ||
client.Do(req) | ||
|
||
if tr.req.Method != "GET" { | ||
t.Errorf("got method %q, want %q", tr.req.Method, "GET") | ||
} | ||
if tr.req.URL.String() != url { | ||
t.Errorf("got URL %q, want %q", tr.req.URL.String(), url) | ||
} | ||
if tr.req.Header == nil { | ||
t.Fatalf("expected non-nil request Header") | ||
} | ||
auth := tr.req.Header.Get("Authorization") | ||
if strings.HasPrefix(auth, "Basic ") { | ||
encoded := auth[6:] | ||
decoded, err := base64.StdEncoding.DecodeString(encoded) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
s := string(decoded) | ||
if expected != s { | ||
t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected) | ||
} | ||
} else { | ||
t.Errorf("Invalid auth %q", auth) | ||
} | ||
|
||
} | ||
|
||
func TestClientTimeout(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping in short mode") | ||
|