1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2024-11-30 08:47:11 +02:00
go-jira/error_test.go
Andy Grunwald 43e8242f2c
style: Fix staticcheck (static analysis) errors for this library (#283)
* style: Fix staticcheck errors for "error strings should not be capitalized (ST1005)"

staticcheck is a static analysis tool for go.
It reports several "error strings should not be capitalized (ST1005)" messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "printf-style function with dynamic format ... (SA1006)"

staticcheck is a static analysis tool for go.
It reports several "printf-style function with dynamic format string and no further arguments should use print-style function instead (SA1006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "type X is unused (U1000)"

staticcheck is a static analysis tool for go.
It reports several "type X is unused (U1000)" messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "should use X instead (S1003 & SA6005)"

staticcheck is a static analysis tool for go.
It reports several

- should use !bytes.Contains(b, []byte(`"password":"bar"`)) instead (S1003)
- should use strings.EqualFold instead (SA6005)

messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "unnecessary use of fmt.Sprintf (S1039)"

staticcheck is a static analysis tool for go.
It report several "unnecessary use of fmt.Sprintf (S1039)" messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "this value of X is never used (SA4006)"

staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "redundant return statement (S1023)"

staticcheck is a static analysis tool for go.
It report several "redundant return statement (S1023)" messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "possible nil pointer dereference (SA5011)"

staticcheck is a static analysis tool for go.
It report several

    file.go:Line:character: possible nil pointer dereference (SA5011)
        file.go:Line:character: this check suggests that the pointer can be nil

messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280

* style: Fix staticcheck errors for "this value of X is never used (SA4006)"

staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.

Related: #280
2020-05-02 23:08:01 +02:00

206 lines
5.1 KiB
Go

package jira
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
)
func TestError_NewJiraError(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"errorMessages":["Issue does not exist or you do not have permission to see it."],"errors":{}}`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewJiraError(resp, errors.New("Original http error"))
if err, ok := err.(*Error); !ok {
t.Errorf("Expected jira Error. Got %s", err.Error())
}
if !strings.Contains(err.Error(), "Issue does not exist") {
t.Errorf("Expected issue message. Got: %s", err.Error())
}
}
func TestError_NoResponse(t *testing.T) {
err := NewJiraError(nil, errors.New("Original http error"))
msg := err.Error()
if !strings.Contains(msg, "Original http error") {
t.Errorf("Expected the original error message: Got\n%s\n", msg)
}
if !strings.Contains(msg, "No response") {
t.Errorf("Expected the 'No response' error message: Got\n%s\n", msg)
}
}
func TestError_NoJSON(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `Original message body`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewJiraError(resp, errors.New("Original http error"))
msg := err.Error()
if !strings.Contains(msg, "200 OK: Original message body: Original http error") {
t.Errorf("Expected the HTTP status: Got\n%s\n", msg)
}
}
func TestError_Unauthorized_NilError(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, `User is not authorized`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewJiraError(resp, nil)
msg := err.Error()
if !strings.Contains(msg, "401 Unauthorized:User is not authorized") {
t.Errorf("Expected Unauthorized HTTP status: Got\n%s\n", msg)
}
}
func TestError_BadJSON(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `<html>Not JSON</html>`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewJiraError(resp, errors.New("Original http error"))
msg := err.Error()
if !strings.Contains(msg, "could not parse JSON") {
t.Errorf("Expected the 'could not parse JSON' error message: Got\n%s\n", msg)
}
}
func TestError_NilOriginalMessage(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Expected an error message. Got a panic (%v)", r)
}
}()
msgErr := &Error{
HTTPError: nil,
ErrorMessages: []string{"Issue does not exist"},
Errors: map[string]string{
"issuetype": "issue type is required",
"title": "title is required",
},
}
_ = msgErr.Error()
}
func TestError_NilOriginalMessageLongError(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Expected an error message. Got a panic (%v)", r)
}
}()
msgErr := &Error{
HTTPError: nil,
ErrorMessages: []string{"Issue does not exist"},
Errors: map[string]string{
"issuetype": "issue type is required",
"title": "title is required",
},
}
_ = msgErr.LongError()
}
func TestError_ShortMessage(t *testing.T) {
msgErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: []string{"Issue does not exist"},
Errors: map[string]string{
"issuetype": "issue type is required",
"title": "title is required",
},
}
mapErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: nil,
Errors: map[string]string{
"issuetype": "issue type is required",
"title": "title is required",
},
}
noErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: nil,
Errors: nil,
}
err := msgErr.Error()
if err != "Issue does not exist: Original http error" {
t.Errorf("Expected short message. Got %s", err)
}
err = mapErr.Error()
if !(strings.Contains(err, "issue type is required") || strings.Contains(err, "title is required")) {
t.Errorf("Expected short message. Got %s", err)
}
err = noErr.Error()
if err != "Original http error" {
t.Errorf("Expected original error message. Got %s", err)
}
}
func TestError_LongMessage(t *testing.T) {
longError := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: []string{"Issue does not exist."},
Errors: map[string]string{
"issuetype": "issue type is required",
"title": "title is required",
},
}
msg := longError.LongError()
if !strings.Contains(msg, "Original http error") {
t.Errorf("Expected the error message: Got\n%s\n", msg)
}
if !strings.Contains(msg, "Issue does not exist") {
t.Errorf("Expected the error message: Got\n%s\n", msg)
}
if !strings.Contains(msg, "title - title is required") {
t.Errorf("Expected the error map: Got\n%s\n", msg)
}
}