forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_errors_test.go
92 lines (78 loc) · 1.82 KB
/
api_errors_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package uadmin
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLogError(t *testing.T) {
request := &http.Request{
Method: "GET",
RequestURI: "/test/data",
}
msg := "message"
userErr := fmt.Errorf("original error")
expectedMessage := fmt.Sprintf("failed [%s] to [%s], msg: %s", "GET", "/test/data", msg)
stdOut := os.Stdout
defer func() {
os.Stdout = stdOut
}()
r, w, err := os.Pipe()
require.Nil(t, err)
os.Stdout = w
logError(request, msg, userErr)
err = w.Close()
require.Nil(t, err)
res, err := io.ReadAll(r)
require.Nil(t, err)
assert.Contains(t, string(res), expectedMessage)
assert.Contains(t, string(res), userErr.Error())
}
func TestRespondAndLogError(t *testing.T) {
type args struct {
code int
errMsg string
}
testCases := []struct {
name string
args args
exp []string
}{
{
name: "success. with err message",
args: args{
code: http.StatusInternalServerError,
errMsg: "internal",
},
exp: []string{`"status": "error"`, `"err_msg": "internal"`},
},
{
name: "success. without err message",
args: args{
code: http.StatusBadRequest,
},
exp: []string{`"status": "error"`, `"err_msg": "400. Bad Request"`},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w := httptest.NewRecorder()
r := &http.Request{}
RespondAndLogError(w, r, tc.args.code, tc.args.errMsg, nil)
res := w.Result()
res.Body.Close()
data, err := io.ReadAll(res.Body)
require.Nil(t, err)
header := res.Header.Get("Content-Type")
assert.Equal(t, res.StatusCode, tc.args.code)
assert.Equal(t, "application/json; charset=utf-8", header)
for _, mes := range tc.exp {
assert.Contains(t, string(data), mes)
}
})
}
}