-
Notifications
You must be signed in to change notification settings - Fork 0
/
reqlogd_test.go
160 lines (147 loc) · 3.88 KB
/
reqlogd_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
156
157
158
159
160
package main
import (
"context"
"database/sql"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
type mockDB struct {
t *testing.T
rr []*record
}
func newMockDB(t *testing.T) *mockDB {
return &mockDB{t: t}
}
func (md *mockDB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
md.rr = append(md.rr, &record{
rat: toTime(md.t, args[0]),
url: toString(md.t, args[1]),
method: toString(md.t, args[2]),
remote: toString(md.t, args[3]),
headers: toString(md.t, args[4]),
length: toInt(md.t, args[5]),
protocol: toString(md.t, args[6]),
})
return nil, nil
}
func (md *mockDB) reset() {
md.rr = nil
}
type record struct {
rat time.Time
url string
method string
remote string
headers string
length int
protocol string
}
func toTime(t *testing.T, v interface{}) time.Time {
tv, ok := v.(time.Time)
if !ok {
t.Fatalf("expected time value passed for field")
}
return tv
}
func toString(t *testing.T, v interface{}) string {
sv, ok := v.(string)
if !ok {
t.Fatalf("expected string value passed for field")
}
return sv
}
func toInt(t *testing.T, v interface{}) int {
iv, ok := v.(int)
if !ok {
t.Fatalf("expected int value passed for field")
}
return iv
}
func Test(t *testing.T) {
mockDB := newMockDB(t)
now := time.Now().UTC()
testServer := &server{
db: mockDB,
now: func() time.Time { return now },
}
ts := httptest.NewServer(http.HandlerFunc(testServer.serveReqLog))
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Fatal(err)
}
tests := map[string]struct {
req *http.Request
wantCode int
wantRecords []*record
}{
"request record is inserted into database": {
req: req,
wantCode: 200,
wantRecords: []*record{{
rat: now,
url: ts.URL + "/",
method: "GET",
remote: "127.0.0.1",
headers: "map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]",
length: 0,
protocol: "HTTP/1.1",
}},
},
"request record is inserted into database again": {
req: req,
wantCode: 200,
wantRecords: []*record{{
rat: now,
url: ts.URL + "/",
method: "GET",
remote: "127.0.0.1",
headers: "map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]",
length: 0,
protocol: "HTTP/1.1",
}},
},
}
for name, test := range tests {
mockDB.reset()
res, err := ts.Client().Do(test.req)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != test.wantCode {
t.Errorf("%q: want status code %v, got status code %v", name, test.wantCode, res.StatusCode)
}
if len(mockDB.rr) != len(test.wantRecords) {
t.Errorf("%q: expected one record in db, got %v", name, len(mockDB.rr))
continue
}
for i := range mockDB.rr {
wantr := test.wantRecords[i]
gotr := mockDB.rr[i]
if wantr.rat != gotr.rat {
t.Errorf("%q: at index %d expected record with rat %v, got %v", name, i, wantr.rat, gotr.rat)
}
if wantr.url != gotr.url {
t.Errorf("%q: at index %d expected record with url %v, got %v", name, i, wantr.url, gotr.url)
}
if wantr.method != gotr.method {
t.Errorf("%q: at index %d expected record with method %v, got %v", name, i, wantr.method, gotr.method)
}
if strings.Split(wantr.remote, ":")[0] != strings.Split(gotr.remote, ":")[0] {
t.Errorf("%q: at index %d expected record with remote %v, got %v", name, i, wantr.remote, gotr.remote)
}
if wantr.headers != gotr.headers {
t.Errorf("%q: at index %d expected record with headers %v, got %v", name, i, wantr.headers, gotr.headers)
}
if wantr.length != gotr.length {
t.Errorf("%q: at index %d expected record with length %v, got %v", name, i, wantr.length, gotr.length)
}
if wantr.protocol != gotr.protocol {
t.Errorf("%q: at index %d expected record with protocol %v, got %v", name, i, wantr.protocol, gotr.protocol)
}
}
}
}