1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-03-17 20:47:57 +02:00

Merge pull request #168 from dvdizon/empty-cookie-check

Check for empty cookies for CookieAuthTransport
This commit is contained in:
rbriski 2018-09-12 11:10:59 -07:00 committed by GitHub
commit a3133c8d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -375,7 +375,10 @@ func (t *CookieAuthTransport) RoundTrip(req *http.Request) (*http.Response, erro
req2 := cloneRequest(req) // per RoundTripper contract
for _, cookie := range t.SessionObject {
req2.AddCookie(cookie)
// Don't add an empty value cookie to the request
if cookie.Value != "" {
req2.AddCookie(cookie)
}
}
return t.transport().RoundTrip(req2)

View File

@ -544,6 +544,42 @@ func TestCookieAuthTransport_SessionObject_Exists(t *testing.T) {
basicAuthClient.Do(req, nil)
}
// Test that an empty cookie in the transport is not returned in the header
func TestCookieAuthTransport_SessionObject_ExistsWithEmptyCookie(t *testing.T) {
setup()
defer teardown()
emptyCookie := &http.Cookie{Name: "empty_cookie", Value: ""}
testCookie := &http.Cookie{Name: "test", Value: "test"}
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
cookies := r.Cookies()
if len(cookies) > 1 {
t.Errorf("The empty cookie should not have been added")
}
if cookies[0].Name != testCookie.Name {
t.Errorf("Cookie names don't match, expected %v, got %v", testCookie.Name, cookies[0].Name)
}
if cookies[0].Value != testCookie.Value {
t.Errorf("Cookie values don't match, expected %v, got %v", testCookie.Value, cookies[0].Value)
}
})
tp := &CookieAuthTransport{
Username: "username",
Password: "password",
AuthURL: "https://some.jira.com/rest/auth/1/session",
SessionObject: []*http.Cookie{emptyCookie, testCookie},
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL)
req, _ := basicAuthClient.NewRequest("GET", ".", nil)
basicAuthClient.Do(req, nil)
}
// Test that if no cookie is in the transport, it checks for a cookie
func TestCookieAuthTransport_SessionObject_DoesNotExist(t *testing.T) {
setup()