forked from blendle/zapdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
123 lines (103 loc) · 2.59 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
package zapdriver_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/blendle/zapdriver"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
func TestHTTP(t *testing.T) {
t.Parallel()
req := &zapdriver.HTTPPayload{}
field := zapdriver.HTTP(req)
assert.Equal(t, zap.Object("httpRequest", req), field)
}
func TestNewHTTP(t *testing.T) {
t.Parallel()
var tests = map[string]struct {
req *http.Request
res *http.Response
want *zapdriver.HTTPPayload
}{
"empty": {
nil,
nil,
&zapdriver.HTTPPayload{},
},
"RequestMethod": {
&http.Request{Method: "GET"},
nil,
&zapdriver.HTTPPayload{RequestMethod: "GET"},
},
"Status": {
nil,
&http.Response{StatusCode: 404},
&zapdriver.HTTPPayload{Status: 404},
},
"UserAgent": {
&http.Request{Header: http.Header{"User-Agent": []string{"hello world"}}},
nil,
&zapdriver.HTTPPayload{UserAgent: "hello world"},
},
"RemoteIP": {
&http.Request{RemoteAddr: "127.0.0.1"},
nil,
&zapdriver.HTTPPayload{RemoteIP: "127.0.0.1"},
},
"Referrer": {
&http.Request{Header: http.Header{"Referer": []string{"hello universe"}}},
nil,
&zapdriver.HTTPPayload{Referer: "hello universe"},
},
"Protocol": {
&http.Request{Proto: "HTTP/1.1"},
nil,
&zapdriver.HTTPPayload{Protocol: "HTTP/1.1"},
},
"RequestURL": {
&http.Request{URL: &url.URL{Host: "example.com", Scheme: "https"}},
nil,
&zapdriver.HTTPPayload{RequestURL: "https://example.com"},
},
"RequestSize": {
&http.Request{Body: ioutil.NopCloser(strings.NewReader("12345"))},
nil,
&zapdriver.HTTPPayload{RequestSize: "5"},
},
"ResponseSize": {
nil,
&http.Response{Body: ioutil.NopCloser(strings.NewReader("12345"))},
&zapdriver.HTTPPayload{ResponseSize: "5"},
},
"simple request": {
httptest.NewRequest("POST", "/", strings.NewReader("12345")),
nil,
&zapdriver.HTTPPayload{
RequestSize: "5",
RequestMethod: "POST",
RemoteIP: "192.0.2.1:1234",
Protocol: "HTTP/1.1",
RequestURL: "/",
},
},
"simple response": {
nil,
&http.Response{Body: ioutil.NopCloser(strings.NewReader("12345")), StatusCode: 404},
&zapdriver.HTTPPayload{ResponseSize: "5", Status: 404},
},
"request & response": {
&http.Request{Method: "POST", Proto: "HTTP/1.1"},
&http.Response{StatusCode: 200},
&zapdriver.HTTPPayload{RequestMethod: "POST", Protocol: "HTTP/1.1", Status: 200},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tt.want, zapdriver.NewHTTP(tt.req, tt.res))
})
}
}