mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-02-09 13:36:58 +02:00
Increase test coverage slightly and add some robustness
This commit is contained in:
parent
bdf2e299cf
commit
657e960a8f
9
issue.go
9
issue.go
@ -284,11 +284,12 @@ func (s *IssueService) PostAttachment(attachmentID string, r io.Reader, attachme
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Copy the file
|
||||
if _, err = io.Copy(fw, r); err != nil {
|
||||
return nil, nil, err
|
||||
if r != nil {
|
||||
// Copy the file
|
||||
if _, err = io.Copy(fw, r); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
writer.Close()
|
||||
|
||||
req, err := s.client.NewMultiPartRequest("POST", apiEndpoint, b)
|
||||
|
107
issue_test.go
107
issue_test.go
@ -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) {
|
||||
var testAttachment = "Here is an attachment"
|
||||
|
||||
@ -165,25 +190,26 @@ func TestIssuePostAttachment(t *testing.T) {
|
||||
status := http.StatusOK
|
||||
|
||||
file, _, err := r.FormFile("file")
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
status = http.StatusNotAcceptable
|
||||
}
|
||||
if file == nil {
|
||||
status = http.StatusNoContent
|
||||
}
|
||||
} else {
|
||||
|
||||
// Read the file into memory
|
||||
data, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
if string(data) != testAttachment {
|
||||
status = http.StatusNotAcceptable
|
||||
}
|
||||
// Read the file into memory
|
||||
data, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
if string(data) != testAttachment {
|
||||
status = http.StatusNotAcceptable
|
||||
}
|
||||
|
||||
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"}]`)
|
||||
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"}]`)
|
||||
file.Close()
|
||||
}
|
||||
})
|
||||
|
||||
reader := strings.NewReader(testAttachment)
|
||||
@ -202,3 +228,60 @@ func TestIssuePostAttachment(t *testing.T) {
|
||||
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