2017-11-02 19:09:17 +02:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-11-03 01:53:36 +02:00
|
|
|
"fmt"
|
2017-11-02 19:09:17 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestError_NewJiraError(t *testing.T) {
|
2017-11-03 01:53:36 +02:00
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2019-01-01 01:55:54 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2017-11-03 01:53:36 +02:00
|
|
|
fmt.Fprint(w, `{"errorMessages":["Issue does not exist or you do not have permission to see it."],"errors":{}}`)
|
|
|
|
})
|
2017-11-02 19:09:17 +02:00
|
|
|
|
2017-11-03 01:53:36 +02:00
|
|
|
req, _ := testClient.NewRequest("GET", "/", nil)
|
|
|
|
resp, _ := testClient.Do(req, nil)
|
2017-11-02 19:09:17 +02:00
|
|
|
|
|
|
|
err := NewJiraError(resp, errors.New("Original http error"))
|
2017-11-02 23:27:16 +02:00
|
|
|
if err, ok := err.(*Error); !ok {
|
2017-11-02 19:09:17 +02:00
|
|
|
t.Errorf("Expected jira Error. Got %s", err.Error())
|
|
|
|
}
|
2017-11-03 01:53:36 +02:00
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "Issue does not exist") {
|
|
|
|
t.Errorf("Expected issue message. Got: %s", err.Error())
|
|
|
|
}
|
2017-11-02 19:09:17 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 03:57:26 +02:00
|
|
|
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) {
|
2019-01-01 01:55:54 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-03-19 20:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestError_Unauthorized_NilError(t *testing.T) {
|
|
|
|
setup()
|
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2020-04-29 09:12:34 +02:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2019-03-19 20:27:47 +02:00
|
|
|
fmt.Fprint(w, `User is not authorized`)
|
|
|
|
})
|
|
|
|
|
|
|
|
req, _ := testClient.NewRequest("GET", "/", nil)
|
|
|
|
resp, _ := testClient.Do(req, nil)
|
|
|
|
|
|
|
|
err := NewJiraError(resp, nil)
|
|
|
|
msg := err.Error()
|
2020-04-29 09:12:34 +02:00
|
|
|
if !strings.Contains(msg, "401 Unauthorized:User is not authorized") {
|
2019-03-19 20:27:47 +02:00
|
|
|
t.Errorf("Expected Unauthorized HTTP status: Got\n%s\n", msg)
|
2020-04-29 09:12:34 +02:00
|
|
|
}
|
2019-01-01 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2017-11-15 03:57:26 +02:00
|
|
|
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()
|
|
|
|
|
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
|
|
|
if !strings.Contains(msg, "could not parse JSON") {
|
|
|
|
t.Errorf("Expected the 'could not parse JSON' error message: Got\n%s\n", msg)
|
2017-11-15 03:57:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:09:17 +02:00
|
|
|
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()
|
2017-11-02 23:40:30 +02:00
|
|
|
if !(strings.Contains(err, "issue type is required") || strings.Contains(err, "title is required")) {
|
2017-11-02 19:09:17 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|