-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
192 lines (184 loc) · 4.98 KB
/
json_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package sender
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/mig-elgt/sender/codes"
)
func TestJsonSender_NewJSON(t *testing.T) {
type args struct {
w http.ResponseWriter
code int
}
testCases := []struct {
name string
args args
want *jsonSender
}{
{
name: "base-case",
args: args{
w: httptest.NewRecorder(),
code: http.StatusOK,
},
want: &jsonSender{
w: httptest.NewRecorder(),
statusCode: http.StatusOK,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
js := NewJSON(tc.args.w, tc.args.code)
if !reflect.DeepEqual(js, tc.want) {
t.Errorf("NewJSON(w,code) got %v; want %v", js, tc.want)
}
})
}
}
func TestJsonSender_WithError(t *testing.T) {
type args struct {
w *httptest.ResponseRecorder
code int
errCode codes.Code
errDesc string
}
testCases := []struct {
name string
args args
want []byte
}{
{
name: "already exists",
args: args{
w: httptest.NewRecorder(),
code: http.StatusConflict,
errCode: codes.AlreadyExists,
errDesc: "Email already exists",
},
want: []byte("{\"error\":{\"status\":409,\"error\":\"ALREADY_EXISTS\",\"description\":\"Email already exists\"}}\n"),
},
{
name: "internal server error",
args: args{
w: httptest.NewRecorder(),
code: http.StatusInternalServerError,
errCode: codes.Internal,
errDesc: "Something went wrong...",
},
want: []byte("{\"error\":{\"status\":500,\"error\":\"INTERNAL\",\"description\":\"Something went wrong...\"}}\n"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
NewJSON(tc.args.w, tc.args.code).WithError(tc.args.errCode, tc.args.errDesc).Send()
if got, want := tc.args.w.Body.Bytes(), tc.want; !reflect.DeepEqual(got, want) {
t.Errorf("WithError(code) got \n%+v; want \n%+v", string(got), string(want))
}
})
}
}
func TestJsonSender_WithFieldError(t *testing.T) {
type args struct {
w *httptest.ResponseRecorder
code int
errCode codes.Code
fieldName string
fieldValue string
}
testCases := []struct {
name string
args args
want []byte
}{
{
name: "email invalid",
args: args{
w: httptest.NewRecorder(),
code: http.StatusUnprocessableEntity,
errCode: codes.InvalidArgument,
fieldName: "email",
fieldValue: "should be a string",
},
want: []byte("{\"error\":{\"status\":422,\"error\":\"INVALID_ARGUMENT\",\"description\":\"One or more fields raised validation errors.\",\"fields\":{\"email\":\"should be a string\"}}}\n"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
NewJSON(tc.args.w, tc.args.code).WithFieldError(tc.args.errCode, tc.args.fieldName, tc.args.fieldValue).Send()
if got, want := tc.args.w.Body.Bytes(), tc.want; !reflect.DeepEqual(got, want) {
t.Errorf("WithFieldError(code) got \n%+v; want \n%+v", string(got), string(want))
}
})
}
}
func TestJsonSender_WithFieldsError(t *testing.T) {
type args struct {
w *httptest.ResponseRecorder
code int
errCode codes.Code
fields map[string]string
}
cases := map[string]struct {
args args
want []byte
}{
"fields missing": {
args: args{
w: httptest.NewRecorder(),
code: http.StatusUnprocessableEntity,
errCode: codes.InvalidArgument,
fields: map[string]string{"username": "The user name is blank.", "email": "The email is required."},
},
want: []byte("{\"error\":{\"status\":422,\"error\":\"INVALID_ARGUMENT\",\"description\":\"One or more fields raised validation errors.\",\"fields\":{\"email\":\"The email is required.\",\"username\":\"The user name is blank.\"}}}\n"),
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
NewJSON(tc.args.w, tc.args.code).WithFieldsError(tc.args.errCode, tc.args.fields).Send()
if got, want := tc.args.w.Body.Bytes(), tc.want; !reflect.DeepEqual(got, want) {
t.Errorf("WithFieldsError(code, fields) got \n%v; want \n%v", string(got), string(want))
}
})
}
}
func TestJsonSender_Send(t *testing.T) {
type newAccount struct {
Email string `json:"email"`
Activated bool `json:"activated"`
}
type args struct {
w *httptest.ResponseRecorder
code int
reg newAccount
}
testCases := []struct {
name string
args args
want []byte
}{
{
name: "base-case",
args: args{
w: httptest.NewRecorder(),
code: http.StatusOK,
reg: newAccount{"[email protected]", false},
},
want: []byte("{\"email\":\"[email protected]\",\"activated\":false}\n"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
NewJSON(tc.args.w, tc.args.code).Send(tc.args.reg)
if got, want := tc.args.w.Body.Bytes(), tc.want; !reflect.DeepEqual(got, want) {
t.Errorf("Send(req) got \n%+v; want \n%+v", string(got), string(want))
}
})
}
}
func createWriter(content string) http.ResponseWriter {
w := httptest.NewRecorder()
w.Body.WriteString(content)
return w
}