-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
90 lines (74 loc) · 1.59 KB
/
middleware_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
package cameljson
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestMiddleware(t *testing.T) {
cases := []struct {
in []byte
want []byte
}{
{
[]byte(`{"NamE":"Ivan","AGE":26,"_sex":"male"}`),
[]byte(`{"namE":"Ivan","age":26,"_sex":"male"}`),
},
{
[]byte(`[{"FIRST_NAME":"Ivan"},{"age":26}]`),
[]byte(`[{"first_name":"Ivan"},{"age":26}]`),
},
{
[]byte(`{"MaP":{"PaM":"amp"},array:[1,2,3]}`),
[]byte(`{"maP":{"paM":"amp"},array:[1,2,3]}`),
},
}
for _, c := range cases {
rec := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/", nil)
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(c.in)
})
Middleware(h).ServeHTTP(rec, req)
got := rec.Body.Bytes()
if !jsonEqual(got, c.want) {
t.Errorf("expected %s", c.want)
t.Errorf(" got %s", got)
}
}
}
func TestMiddlewareWithBasicString(t *testing.T) {
cases := []struct {
in string
want string
}{
{
"invalid_json",
"invalid_json",
},
{
"",
"",
},
}
for _, c := range cases {
rec := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/", nil)
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(c.in))
})
Middleware(h).ServeHTTP(rec, req)
got := rec.Body.String()
if got != c.want {
t.Errorf("expected '%s'", c.want)
t.Errorf(" got '%s'", got)
}
}
}
func jsonEqual(a, b []byte) bool {
var i1, i2 interface{}
json.Unmarshal(a, &i1)
json.Unmarshal(b, &i2)
return reflect.DeepEqual(i1, i2)
}