-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror_test.go
73 lines (61 loc) · 1.72 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package biteship
import (
"encoding/json"
"errors"
"github.com/stretchr/testify/assert"
"log"
"net/http"
"testing"
)
func TestErrorGo(t *testing.T) {
err := errors.New("myerror")
errorGo := ErrorGo(err)
assert.Equal(t, errorGo.Status, http.StatusInternalServerError)
assert.Equal(t, errorGo.ErrorCode, "Error Go")
assert.Equal(t, errorGo.Message, err.Error())
}
func TestErrorGo_Panics(t *testing.T) {
assert.Panics(t, func() {
_ = ErrorGo(nil)
})
}
func TestErrorRequestParam(t *testing.T) {
err := errors.New("testerr")
errRequestParam := ErrorRequestParam(err)
assert.Equal(t, errRequestParam.Status, http.StatusBadRequest)
assert.Equal(t, errRequestParam.ErrorCode, "Bad Request")
assert.Equal(t, errRequestParam.Message, err.Error())
}
func TestErrorRequestParams_Panics(t *testing.T) {
assert.Panics(t, func() {
_ = ErrorRequestParam(nil)
})
}
func TestErrorHttp(t *testing.T) {
mockError := Error{
Status: 0,
ErrorCode: "",
Message: "",
RawError: "",
Code: 0,
}
validJson, err := json.Marshal(mockError)
if err != nil {
log.Println("Failure :")
log.Fatalln(err)
} else {
err := ErrorHttp(http.StatusBadRequest, validJson)
assert.Equal(t, err.Status, http.StatusBadRequest)
assert.Equal(t, err.Code, mockError.Code)
assert.Equal(t, err.Message, mockError.Message)
assert.Equal(t, err.RawError, mockError.RawError)
assert.Equal(t, err.ErrorCode, mockError.ErrorCode)
}
}
func TestErrorHttp_InvalidJson(t *testing.T) {
invalidJson := `{"some", "data", "invalid_property"}`
err := ErrorHttp(http.StatusBadRequest, []byte(invalidJson))
assert.Equal(t, err.Status, http.StatusInternalServerError)
assert.Equal(t, err.ErrorCode, "Error Go")
assert.NotEqual(t, err.Message, "")
}