-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender_test.go
101 lines (86 loc) · 2.49 KB
/
render_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
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package chix
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
// testJSONMarshalEqual is a helper function to test if the resulting http body equals
// the input data when marshalled and unmarshalled.
func testJSONMarshalEqual[T any](t *testing.T, input T, body io.ReadCloser, shouldEqual bool) (ok bool) {
var in, out any
inBytes, err := json.Marshal(input)
if err != nil {
t.Errorf("error marshaling input data: %v", err)
return false
}
if err = json.Unmarshal(inBytes, &in); err != nil {
t.Errorf("error unmarshaling input data: %v", err)
return false
}
dec := json.NewDecoder(body)
if err = dec.Decode(&out); err != nil {
t.Errorf("error decoding response body: %v", err)
return false
}
if reflect.DeepEqual(in, out) && !shouldEqual {
t.Errorf("expected %#v to not equal %#v", in, out)
return false
} else if !reflect.DeepEqual(in, out) && shouldEqual {
t.Errorf("expected %#v to equal %#v", in, out)
return false
}
return true
}
func TestJSON(t *testing.T) {
tests := []struct {
name string
data any
headers map[string]string
statusCode int
}{
{
name: "empty",
data: M{},
headers: map[string]string{"Content-Type": "application/json"},
statusCode: http.StatusOK,
},
{
name: "base-object",
data: M{"foo": "bar"},
headers: map[string]string{"Content-Type": "application/json"},
statusCode: http.StatusOK,
},
{
name: "base-array",
data: []string{"foo", "bar"},
headers: map[string]string{"Content-Type": "application/json"},
statusCode: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://example.com/?pretty=true", http.NoBody)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
JSON(w, r, tt.statusCode, tt.data)
})
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
resp := rec.Result()
if resp.StatusCode != tt.statusCode {
t.Errorf("expected status code %d, got %d", tt.statusCode, resp.StatusCode)
}
for k, v := range tt.headers {
if resp.Header.Get(k) != v {
t.Errorf("expected header %s to be %s, got %s", k, v, resp.Header.Get(k))
}
}
_ = testJSONMarshalEqual(t, tt.data, resp.Body, true)
})
}
}