-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go.orig
60 lines (55 loc) · 1.33 KB
/
http_test.go.orig
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
package identity
import (
"bytes"
"errors"
"net/http"
"strings"
"testing"
)
type testResponseWriter struct {
buf bytes.Buffer
header http.Header
status int
}
func (w *testResponseWriter) Write(b []byte) (int, error) { return w.buf.Write(b) }
func (w *testResponseWriter) WriteHeader(status int) { w.status = status }
func (w *testResponseWriter) Header() http.Header { return w.header }
type testStruct struct {
A string `json:"a"`
B int `json:"b"`
}
func TestRenderJSON(t *testing.T) {
candidates := []struct {
t interface{}
status int
exBody string
}{
{
t: testStruct{A: "value", B: 123},
status: http.StatusOK,
exBody: `{"a":"value","b":123}`,
},
{
t: errors.New("something error"),
status: http.StatusInternalServerError,
exBody: `{"error":"Internal Server Error","message":"something error"}`,
},
}
for _, c := range candidates {
w := &testResponseWriter{header: http.Header{}}
if err := renderJSON(w, c.status, c.t); err != nil {
t.Error("err should not occured")
t.Logf("error: %s\n", err)
return
}
if w.status != c.status {
t.Errorf("status does not match: %d != %d\n", w.status, c.status)
return
}
s := strings.Trim(w.buf.String(), "\r\n")
if s != c.exBody {
t.Errorf("body does not match: %s != %s\n", s, c.exBody)
return
}
}
}