-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse_test.go
153 lines (112 loc) · 3.44 KB
/
response_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
package rest_test
import (
"encoding/json"
"errors"
"fmt"
"github.com/edermanoel94/rest-go"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type customError struct {
Description string `json:"description"`
}
type customErrorWithoutJson struct {
Description string `json:"description"`
}
func (c customError) Error() string {
bytes, _ := json.Marshal(&c)
return string(bytes)
}
func (c customErrorWithoutJson) Error() string {
return fmt.Sprintf("description: %s", c.Description)
}
// TESTS
func TestResponse(t *testing.T) {
testCases := []struct {
description string
payload []byte
statusCode int
err error
}{
{"should serialize message and send statusCode",
[]byte("{\"name\": \"cale\"}"), http.StatusOK, nil},
{"should send a nil and get an ErrNotValidJson", nil, http.StatusInternalServerError, rest.ErrNotValidJson},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
recorder := httptest.NewRecorder()
rest.Response(recorder, tc.payload, tc.statusCode)
result := recorder.Result()
defer result.Body.Close()
payloadReceived, err := ioutil.ReadAll(result.Body)
if err != nil {
t.Fatalf("cannot read recorder: %v", err)
}
// TODO: is the better way to do this?
if tc.err != nil {
assert.Equal(t, string(payloadReceived), fmt.Sprintf(`{"message":"%s"}`, tc.err.Error()))
} else {
assert.Equal(t, len(tc.payload), len(payloadReceived))
}
assert.Equal(t, tc.statusCode, result.StatusCode)
assert.Equal(t, "application/json", result.Header.Get("Content-Type"))
})
}
}
func TestError(t *testing.T) {
testCases := []struct {
description string
err error
errStringGiven string
}{
{"should given a message of error", errors.New("not found"), "not found"},
{"should ignore extra quotes", errors.New("\"not f\"ound"), "not found"},
{"should send a custom struct error message which implements error interface",
customError{Description: "not found"}, "{\"description\":\"not found\"}"},
{"should send a custom struct which implements error interface but not use json.Marshal",
customErrorWithoutJson{Description: "cannot found"}, "{\"message\":\"not a valid json\"}"},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
recorder := httptest.NewRecorder()
rest.Error(recorder, tc.err, http.StatusNotFound)
result := recorder.Result()
defer result.Body.Close()
payloadReceived, err := ioutil.ReadAll(result.Body)
if err != nil {
t.Fatalf("cannot read recorder: %v", err)
}
assert.Contains(t, string(payloadReceived), tc.errStringGiven)
})
}
// TODO: add more tests with custom error on pointer
}
func TestMarshalled(t *testing.T) {
testCases := []struct {
description string
actual interface{}
contains string
}{
{"should marshal struct correctly", struct {
Name string `json:"name"`
}{"Eder"}, "Eder"},
{"should marshal to a 0", 0, "0"},
{"should marshal to a nil", nil, "null"},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
recorder := httptest.NewRecorder()
rest.Marshalled(recorder, &tc.actual, http.StatusInternalServerError)
result := recorder.Result()
defer result.Body.Close()
payloadReceived, err := ioutil.ReadAll(result.Body)
if err != nil {
t.Fatal(err)
}
assert.Contains(t, string(payloadReceived), tc.contains)
})
}
}