-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_test.go
159 lines (129 loc) · 3.82 KB
/
http_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
package httplog
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/require"
)
//go:generate mockery -name FieldLogger -case underscore -inpkg
func testRequest(url string) (*http.Request, error) {
return http.NewRequest(
"GET",
url+`/some_endpoint?arg1="arg1value"&arg2="arg2value"`,
nil,
)
}
func TestLogger_HTTPHandler_EmptyConfig(t *testing.T) {
mLogger := new(MockFieldLogger)
l := &Logger{
logger: mLogger,
}
req, err := testRequest("")
require.NoError(t, err)
l.RequestHandler()(nil, req)
mLogger.AssertExpectations(t)
}
func TestLogger_HTTPHandler_RequestFieldsLogged(t *testing.T) {
mLogger := new(MockFieldLogger)
now := time.Now()
mLogger.On("WithFields", Fields{
"req_bytes_in": "0",
"req_uri_compact": `arg1="arg1value"&arg2="arg2value"`,
"req_path": "/some_endpoint",
"req_time": now.Format(time.RFC3339),
"req_method": "GET",
}).Return(mLogger)
mLogger.On("Log", []interface{}{"Received HTTP request"}).Once()
l := &Logger{
logger: mLogger,
cfg: DefaultReqResConfig(),
}
timeNow = func() time.Time {
return now
}
req, err := testRequest("")
require.NoError(t, err)
l.RequestHandler()(nil, req)
mLogger.AssertExpectations(t)
}
func TestLogger_HTTPHandler_ResponseFieldsLogged(t *testing.T) {
mLogger := new(MockFieldLogger)
now := time.Now()
mLogger.On("WithFields", Fields{
"res_bytes_out": "39",
"res_content_type": "application/json",
"res_time": now.Format(time.RFC3339),
"res_status": "200",
}).Return(mLogger)
mLogger.On("Log", []interface{}{"Responding to HTTP request"}).Once()
srv := httptest.NewServer(RegisterMiddleware(mLogger, Config{
ResponseFields: DefaultResponseFields,
})(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ioutil.ReadAll(r.Body)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
data := struct {
FieldA string `json:"field_a"`
FieldB bool `json:"field_b"`
}{
FieldA: "something",
FieldB: true,
}
encoder := json.NewEncoder(w)
err := encoder.Encode(data)
require.NoError(t, err)
}),
))
defer srv.Close()
timeNow = func() time.Time {
return now
}
req, err := testRequest(srv.URL)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
ioutil.ReadAll(res.Body)
mLogger.AssertExpectations(t)
}
func TestLogger_HTTPHandler_RedirectFieldsLogged(t *testing.T) {
mLogger := new(MockFieldLogger)
now := time.Now()
mLogger.On("WithFields", Fields{
"res_status": "302",
"res_bytes_out": "0",
"res_time": now.Format(time.RFC3339),
"res_location_compact": `arg1="arg1value"&arg2="arg2value"`,
"res_location_host": "http://localhost/wrong_endpoint",
}).Return(mLogger)
mLogger.On("Log", []interface{}{"Redirecting HTTP request"}).Once()
srv := httptest.NewServer(RegisterMiddleware(mLogger, Config{
ResponseFields: DefaultResponseFields,
})(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ioutil.ReadAll(r.Body)
u := `http://localhost/wrong_endpoint?arg1="arg1value"&arg2="arg2value"`
http.Redirect(w, r, u, 302)
}),
))
defer srv.Close()
timeNow = func() time.Time {
return now
}
req, err := testRequest(srv.URL)
require.NoError(t, err)
_, err = http.DefaultClient.Do(req)
require.Error(t, err)
mLogger.AssertExpectations(t)
}
func TestFormatComactArgs(t *testing.T) {
u := `http://localhost/wrong_endpoint?arg1="arg1value"&arg2="arg2valuelonglonglonglonglonglonglonglonglonglonglonglonglonglong"&arg3`
url, err := url.Parse(u)
require.NoError(t, err)
assert.Equal(t, `arg1="arg1value"&arg2="arg2valuelonglonglonglonglonglonglonglonglonglo...&arg3`, formatCompactArgs(url.RawQuery))
}