-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhandling_test.go
155 lines (131 loc) · 3.68 KB
/
handling_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
package gimlet
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mongodb/grip"
"github.com/mongodb/grip/send"
"github.com/stretchr/testify/assert"
)
type writeResponseBaseFunc func(http.ResponseWriter, int, interface{})
type writeResponseFunc func(http.ResponseWriter, interface{})
func TestResponseWritingFunctions(t *testing.T) {
assert := assert.New(t)
baseCases := map[OutputFormat]writeResponseBaseFunc{
JSON: WriteJSONResponse,
BINARY: WriteBinaryResponse,
YAML: WriteYAMLResponse,
TEXT: WriteTextResponse,
HTML: WriteHTMLResponse,
}
for of, wf := range baseCases {
for _, code := range []int{200, 400, 500} {
r := httptest.NewRecorder()
wf(r, code, "")
assert.Equal(code, r.Code)
header := r.Header()
assert.Len(header, 1)
ct, ok := header["Content-Type"]
assert.True(ok)
assert.Equal(ct, []string{of.ContentType()})
body := r.Body.Bytes()
assert.True(len(body) < 4)
}
}
}
func TestSerializationErrors(t *testing.T) {
assert := assert.New(t)
baseCases := map[OutputFormat]writeResponseBaseFunc{
JSON: WriteJSONResponse,
YAML: WriteYAMLResponse,
CSV: WriteCSVResponse,
}
for _, wf := range baseCases {
r := httptest.NewRecorder()
wf(r, http.StatusOK, struct{ Foo chan struct{} }{Foo: make(chan struct{})})
assert.Equal(r.Code, http.StatusInternalServerError)
wf(r, http.StatusOK, errors.New("foo"))
assert.Equal(r.Code, http.StatusInternalServerError)
}
}
func TestResponsesWritingHelpers(t *testing.T) {
assert := assert.New(t)
testTable := map[int]map[OutputFormat]writeResponseFunc{
http.StatusOK: {
JSON: WriteJSON,
BINARY: WriteBinary,
YAML: WriteYAML,
TEXT: WriteText,
HTML: WriteHTML,
},
http.StatusBadRequest: {
JSON: WriteJSONError,
BINARY: WriteBinaryError,
YAML: WriteYAMLError,
TEXT: WriteTextError,
HTML: WriteHTMLError,
},
http.StatusInternalServerError: {
JSON: WriteJSONInternalError,
BINARY: WriteBinaryInternalError,
YAML: WriteYAMLInternalError,
TEXT: WriteTextInternalError,
HTML: WriteHTMLInternalError,
},
}
for status, cases := range testTable {
for of, wf := range cases {
r := httptest.NewRecorder()
wf(r, []struct{}{})
assert.Equal(status, r.Code)
ct, ok := r.Header()["Content-Type"]
assert.True(ok)
assert.Equal(ct, []string{of.ContentType()})
body := r.Body.Bytes()
assert.True(len(body) < 4)
}
}
}
func TestBytesConverter(t *testing.T) {
assert := assert.New(t)
cases := [][]interface{}{
{fmt.Sprintf("%v", http.DefaultClient), http.DefaultClient},
{"gimlet", "gimlet"},
{"gimlet", errors.New("gimlet")},
{"gimlet", []byte("gimlet")},
{"GET", get},
{"gimlet", bytes.NewBufferString("gimlet")},
}
for _, c := range cases {
if !assert.Len(c, 2) {
continue
}
out := c[0].(string)
in := c[1]
buf := &bytes.Buffer{}
_, err := writePayload(buf, in)
assert.NoError(err)
assert.Equal([]byte(out), buf.Bytes())
}
buf := &bytes.Buffer{}
_, err := writePayload(buf, []string{"gimlet", "gimlet"})
assert.NoError(err)
assert.Equal([]byte("gimlet\ngimlet"), buf.Bytes())
}
type mangledResponseWriter struct{ *httptest.ResponseRecorder }
func (r mangledResponseWriter) Write(b []byte) (int, error) { return 0, errors.New("always errors") }
func TestWriteResponseErrorLogs(t *testing.T) {
defer func(s send.Sender) {
assert.NoError(t, grip.SetSender(s))
}(grip.GetSender())
assert := assert.New(t)
sender := send.MakeInternalLogger()
rw := mangledResponseWriter{httptest.NewRecorder()}
assert.NoError(grip.SetSender(sender))
assert.False(sender.HasMessage())
writeResponse(JSON, rw, 200, []byte("foo"))
assert.True(sender.HasMessage())
}