1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2024-11-28 08:39:03 +02:00

Fixed issue with Jira auth for multipart requests

This commit is contained in:
Ante Kresic 2016-07-27 12:21:09 +02:00
parent cdb3939c4c
commit e9740fe3e9
2 changed files with 65 additions and 2 deletions

View File

@ -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

View File

@ -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()