-
Notifications
You must be signed in to change notification settings - Fork 6
/
jsend_test.go
88 lines (81 loc) · 2.16 KB
/
jsend_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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package jsend
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWrite(t *testing.T) {
tests := []struct {
status int
body Body
}{
{
body: Body{Status: StatusError, Message: "error"},
},
{
body: Body{Status: StatusError, Message: "error", Code: 10000},
},
{
body: Body{Status: StatusError, Message: "error", Code: 10001, Data: "error data"},
},
{
status: http.StatusInternalServerError,
body: Body{Status: StatusError, Message: "error"},
},
{
body: Body{Status: StatusFail, Data: "fail"},
},
{
status: http.StatusForbidden,
body: Body{Status: StatusFail, Data: "fail"},
},
{
body: Body{Status: StatusSuccess, Data: "success"},
},
}
contentType := "application/json"
for _, test := range tests {
response := httptest.NewRecorder()
var err error
var statuses []int
if test.status != 0 {
statuses = append(statuses, test.status)
}
if test.body.Status == StatusSuccess {
err = Success(response, test.body.Data, statuses...)
} else if test.body.Status == StatusFail {
err = Fail(response, test.body.Data, statuses...)
} else {
if test.body.Data != nil {
err = ErrorCodeData(response, test.body.Message, test.body.Code, test.body.Data, statuses...)
} else if test.body.Code != 0 {
err = ErrorCode(response, test.body.Message, test.body.Code, statuses...)
} else {
err = Error(response, test.body.Message, statuses...)
}
}
if err != nil {
t.Fatal(err)
}
actualContentType := response.Header().Get("Content-Type")
assert.Equal(t, contentType, actualContentType)
if test.status != 0 {
assert.Equal(t, test.status, response.Result().StatusCode)
}
var actualBody Body
if err = json.Unmarshal(response.Body.Bytes(), &actualBody); err != nil {
t.Fatal(err)
}
assert.Equal(t, test.body, actualBody)
}
}
func TestWriteError(t *testing.T) {
w := httptest.NewRecorder()
err := Write(w, NewError("msg", 0, make(chan int)))
assert.NotNil(t, err)
}