From e9740fe3e9df4a742fe6e44f68de7137b9f3f535 Mon Sep 17 00:00:00 2001 From: Ante Kresic Date: Wed, 27 Jul 2016 12:21:09 +0200 Subject: [PATCH] Fixed issue with Jira auth for multipart requests --- jira.go | 6 ++++-- jira_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/jira.go b/jira.go index fbd8c8b..70b7e6e 100644 --- a/jira.go +++ b/jira.go @@ -142,8 +142,10 @@ func (c *Client) NewMultiPartRequest(method, urlStr string, buf *bytes.Buffer) ( req.Header.Set("X-Atlassian-Token", "nocheck") // Set session cookie if there is one - if c.Authentication.Authenticated() { - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", c.session.Session.Name, c.session.Session.Value)) + if c.session != nil { + for _, cookie := range c.session.Cookies { + req.AddCookie(cookie) + } } return req, nil diff --git a/jira_test.go b/jira_test.go index 8fa60c1..7e6de1d 100644 --- a/jira_test.go +++ b/jira_test.go @@ -1,6 +1,7 @@ package jira import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -182,6 +183,34 @@ func TestClient_NewRequest_BadURL(t *testing.T) { testURLParseError(t, err) } +func TestClient_NewRequest_SessionCookies(t *testing.T) { + c, err := NewClient(nil, testJIRAInstanceURL) + if err != nil { + t.Errorf("An error occured. Expected nil. Got %+v.", err) + } + + cookie := &http.Cookie{Name: "testcookie", Value: "testvalue"} + c.session = &Session{Cookies: []*http.Cookie{cookie}} + + inURL := "rest/api/2/issue/" + inBody := &Issue{Key: "MESOS"} + req, err := c.NewRequest("GET", inURL, inBody) + + if err != nil { + t.Errorf("An error occured. Expected nil. Got %+v.", err) + } + + if len(req.Cookies()) != len(c.session.Cookies) { + t.Errorf("An error occured. Expected %d cookie(s). Got %d.", len(c.session.Cookies), len(req.Cookies())) + } + + for i, v := range req.Cookies() { + if v.String() != c.session.Cookies[i].String() { + t.Errorf("An error occured. Unexpected cookie. Expected %s, actual %s.", v.String(), c.session.Cookies[i].String()) + } + } +} + // If a nil body is passed to gerrit.NewRequest, make sure that nil is also passed to http.NewRequest. // In most cases, passing an io.Reader that returns no content is fine, // since there is no difference between an HTTP request body that is an empty string versus one that is not set at all. @@ -200,6 +229,38 @@ func TestClient_NewRequest_EmptyBody(t *testing.T) { } } +func TestClient_NewMultiPartRequest(t *testing.T) { + c, err := NewClient(nil, testJIRAInstanceURL) + if err != nil { + t.Errorf("An error occured. Expected nil. Got %+v.", err) + } + + cookie := &http.Cookie{Name: "testcookie", Value: "testvalue"} + c.session = &Session{Cookies: []*http.Cookie{cookie}} + + inURL := "rest/api/2/issue/" + inBuf := bytes.NewBufferString("teststring") + req, err := c.NewMultiPartRequest("GET", inURL, inBuf) + + if err != nil { + t.Errorf("An error occured. Expected nil. Got %+v.", err) + } + + if len(req.Cookies()) != len(c.session.Cookies) { + t.Errorf("An error occured. Expected %d cookie(s). Got %d.", len(c.session.Cookies), len(req.Cookies())) + } + + for i, v := range req.Cookies() { + if v.String() != c.session.Cookies[i].String() { + t.Errorf("An error occured. Unexpected cookie. Expected %s, actual %s.", v.String(), c.session.Cookies[i].String()) + } + } + + if req.Header.Get("X-Atlassian-Token") != "nocheck" { + t.Errorf("An error occured. Unexpected X-Atlassian-Token header value. Expected nocheck, actual %s.", req.Header.Get("X-Atlassian-Token")) + } +} + func TestClient_Do(t *testing.T) { setup() defer teardown()