mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-02-15 13:53:15 +02:00
Increase test coverage slightly and add some robustness
This commit is contained in:
parent
bdf2e299cf
commit
657e960a8f
3
issue.go
3
issue.go
@ -284,11 +284,12 @@ func (s *IssueService) PostAttachment(attachmentID string, r io.Reader, attachme
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r != nil {
|
||||||
// Copy the file
|
// Copy the file
|
||||||
if _, err = io.Copy(fw, r); err != nil {
|
if _, err = io.Copy(fw, r); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
writer.Close()
|
writer.Close()
|
||||||
|
|
||||||
req, err := s.client.NewMultiPartRequest("POST", apiEndpoint, b)
|
req, err := s.client.NewMultiPartRequest("POST", apiEndpoint, b)
|
||||||
|
@ -154,6 +154,31 @@ func TestIssueDownloadAttachment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssueDownloadAttachment_BadStatus(t *testing.T) {
|
||||||
|
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
testMux.HandleFunc("/secure/attachment/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
testMethod(t, r, "GET")
|
||||||
|
testRequestURL(t, r, "/secure/attachment/10000/")
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
})
|
||||||
|
|
||||||
|
resp, err := testClient.Issue.DownloadAttachment("10000")
|
||||||
|
if resp == nil {
|
||||||
|
t.Error("Expected response. Response is nil")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusForbidden {
|
||||||
|
t.Errorf("Expected Status code %d. Given %d", http.StatusForbidden, resp.StatusCode)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Error expected")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIssuePostAttachment(t *testing.T) {
|
func TestIssuePostAttachment(t *testing.T) {
|
||||||
var testAttachment = "Here is an attachment"
|
var testAttachment = "Here is an attachment"
|
||||||
|
|
||||||
@ -165,13 +190,12 @@ func TestIssuePostAttachment(t *testing.T) {
|
|||||||
status := http.StatusOK
|
status := http.StatusOK
|
||||||
|
|
||||||
file, _, err := r.FormFile("file")
|
file, _, err := r.FormFile("file")
|
||||||
defer file.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status = http.StatusNotAcceptable
|
status = http.StatusNotAcceptable
|
||||||
}
|
}
|
||||||
if file == nil {
|
if file == nil {
|
||||||
status = http.StatusNoContent
|
status = http.StatusNoContent
|
||||||
}
|
} else {
|
||||||
|
|
||||||
// Read the file into memory
|
// Read the file into memory
|
||||||
data, err := ioutil.ReadAll(file)
|
data, err := ioutil.ReadAll(file)
|
||||||
@ -184,6 +208,8 @@ func TestIssuePostAttachment(t *testing.T) {
|
|||||||
|
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
fmt.Fprint(w, `[{"self":"http://jira/jira/rest/api/2/attachment/228924","id":"228924","filename":"example.jpg","author":{"self":"http://jira/jira/rest/api/2/user?username=test","name":"test","emailAddress":"test@test.com","avatarUrls":{"16x16":"http://jira/jira/secure/useravatar?size=small&avatarId=10082","48x48":"http://jira/jira/secure/useravatar?avatarId=10082"},"displayName":"Tester","active":true},"created":"2016-05-24T00:25:17.000-0700","size":32280,"mimeType":"image/jpeg","content":"http://jira/jira/secure/attachment/228924/example.jpg","thumbnail":"http://jira/jira/secure/thumbnail/228924/_thumb_228924.png"}]`)
|
fmt.Fprint(w, `[{"self":"http://jira/jira/rest/api/2/attachment/228924","id":"228924","filename":"example.jpg","author":{"self":"http://jira/jira/rest/api/2/user?username=test","name":"test","emailAddress":"test@test.com","avatarUrls":{"16x16":"http://jira/jira/secure/useravatar?size=small&avatarId=10082","48x48":"http://jira/jira/secure/useravatar?avatarId=10082"},"displayName":"Tester","active":true},"created":"2016-05-24T00:25:17.000-0700","size":32280,"mimeType":"image/jpeg","content":"http://jira/jira/secure/attachment/228924/example.jpg","thumbnail":"http://jira/jira/secure/thumbnail/228924/_thumb_228924.png"}]`)
|
||||||
|
file.Close()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
reader := strings.NewReader(testAttachment)
|
reader := strings.NewReader(testAttachment)
|
||||||
@ -202,3 +228,60 @@ func TestIssuePostAttachment(t *testing.T) {
|
|||||||
t.Errorf("Error given: %s", err)
|
t.Errorf("Error given: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssuePostAttachment_NoResponse(t *testing.T) {
|
||||||
|
var testAttachment = "Here is an attachment"
|
||||||
|
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
testMux.HandleFunc("/rest/api/2/issue/10000/attachments", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
testMethod(t, r, "POST")
|
||||||
|
testRequestURL(t, r, "/rest/api/2/issue/10000/attachments")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
reader := strings.NewReader(testAttachment)
|
||||||
|
|
||||||
|
_, _, err := testClient.Issue.PostAttachment("10000", reader, "attachment")
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Error expected: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIssuePostAttachment_NoFilename(t *testing.T) {
|
||||||
|
var testAttachment = "Here is an attachment"
|
||||||
|
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
testMux.HandleFunc("/rest/api/2/issue/10000/attachments", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
testMethod(t, r, "POST")
|
||||||
|
testRequestURL(t, r, "/rest/api/2/issue/10000/attachments")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprint(w, `[{"self":"http://jira/jira/rest/api/2/attachment/228924","id":"228924","filename":"example.jpg","author":{"self":"http://jira/jira/rest/api/2/user?username=test","name":"test","emailAddress":"test@test.com","avatarUrls":{"16x16":"http://jira/jira/secure/useravatar?size=small&avatarId=10082","48x48":"http://jira/jira/secure/useravatar?avatarId=10082"},"displayName":"Tester","active":true},"created":"2016-05-24T00:25:17.000-0700","size":32280,"mimeType":"image/jpeg","content":"http://jira/jira/secure/attachment/228924/example.jpg","thumbnail":"http://jira/jira/secure/thumbnail/228924/_thumb_228924.png"}]`)
|
||||||
|
})
|
||||||
|
reader := strings.NewReader(testAttachment)
|
||||||
|
|
||||||
|
_, _, err := testClient.Issue.PostAttachment("10000", reader, "")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error expected: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIssuePostAttachment_NoAttachment(t *testing.T) {
|
||||||
|
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
testMux.HandleFunc("/rest/api/2/issue/10000/attachments", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
testMethod(t, r, "POST")
|
||||||
|
testRequestURL(t, r, "/rest/api/2/issue/10000/attachments")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprint(w, `[{"self":"http://jira/jira/rest/api/2/attachment/228924","id":"228924","filename":"example.jpg","author":{"self":"http://jira/jira/rest/api/2/user?username=test","name":"test","emailAddress":"test@test.com","avatarUrls":{"16x16":"http://jira/jira/secure/useravatar?size=small&avatarId=10082","48x48":"http://jira/jira/secure/useravatar?avatarId=10082"},"displayName":"Tester","active":true},"created":"2016-05-24T00:25:17.000-0700","size":32280,"mimeType":"image/jpeg","content":"http://jira/jira/secure/attachment/228924/example.jpg","thumbnail":"http://jira/jira/secure/thumbnail/228924/_thumb_228924.png"}]`)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, _, err := testClient.Issue.PostAttachment("10000", nil, "attachment")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error given: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user