MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestLog

Function TestLog

pkg/webmiddleware/log_test.go:79–206  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

77}
78
79func TestLog(t *testing.T) {
80 a := assertions.New(t)
81 ch := make(logEntryChannel, 10)
82 m := Log(&log.Logger{
83 Handler: ch,
84 }, []string{"/path", "/ignorepath"})
85
86 for _, tc := range []struct {
87 name string
88 path string
89 setReq func(r *http.Request)
90 validate func(t *testing.T)
91 handler func(w http.ResponseWriter, r *http.Request)
92 }{
93 {
94 name: "NormalSuccess",
95 handler: func(w http.ResponseWriter, r *http.Request) {
96 w.WriteHeader(http.StatusOK)
97 },
98 path: "/",
99 validate: func(t *testing.T) {
100 ch.Expect(t, func(t *testing.T, e log.Entry) {
101 verifySuccess(t, e)
102 assertions.New(t).So(e.Fields().Fields()["peer.address"], should.Equal, "192.0.2.1:1234")
103 })
104 },
105 },
106 {
107 name: "RealIP",
108 setReq: func(r *http.Request) {
109 r.Header.Set("X-Real-Ip", "12.34.56.78")
110 },
111 handler: func(w http.ResponseWriter, r *http.Request) {
112 w.WriteHeader(http.StatusOK)
113 },
114 path: "/",
115 validate: func(t *testing.T) {
116 ch.Expect(t, func(t *testing.T, e log.Entry) {
117 verifySuccess(t, e)
118 assertions.New(t).So(e.Fields().Fields()["peer.real_ip"], should.Equal, "12.34.56.78")
119 })
120 },
121 },
122 {
123 name: "IgnoreSuccess",
124 handler: func(w http.ResponseWriter, r *http.Request) {
125 w.WriteHeader(http.StatusOK)
126 },
127 path: "/path",
128 validate: ch.ExpectNoLog,
129 },
130 {
131 name: "IgnoreSuccess",
132 handler: func(w http.ResponseWriter, r *http.Request) {
133 w.WriteHeader(http.StatusOK)
134 },
135 path: "/ignorepath",
136 validate: ch.ExpectNoLog,

Callers

nothing calls this directly

Calls 12

NotFoundFunction · 0.92
FromContextFunction · 0.92
verifySuccessFunction · 0.85
ExpectMethod · 0.80
LogFunction · 0.70
NewMethod · 0.65
FieldsMethod · 0.65
SetMethod · 0.65
RunMethod · 0.65
ContextMethod · 0.65
ServeHTTPMethod · 0.45
validateMethod · 0.45

Tested by

no test coverage detected