| 1 |
/* |
| 2 |
Copyright 2014 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package httplog |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http" |
| 21 |
"net/http/httptest" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"k8s.io/apiserver/pkg/endpoints/responsewriter" |
| 26 |
) |
| 27 |
|
| 28 |
func TestDefaultStacktracePred(t *testing.T) { |
| 29 |
for _, x := range []int{101, 200, 204, 302, 400, 404} { |
| 30 |
if DefaultStacktracePred(x) { |
| 31 |
t.Fatalf("should not log on %v by default", x) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
for _, x := range []int{500, 100} { |
| 36 |
if !DefaultStacktracePred(x) { |
| 37 |
t.Fatalf("should log on %v by default", x) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestStatusIsNot(t *testing.T) { |
| 43 |
statusTestTable := []struct { |
| 44 |
status int |
| 45 |
statuses []int |
| 46 |
want bool |
| 47 |
}{ |
| 48 |
{http.StatusOK, []int{}, true}, |
| 49 |
{http.StatusOK, []int{http.StatusOK}, false}, |
| 50 |
{http.StatusCreated, []int{http.StatusOK, http.StatusAccepted}, true}, |
| 51 |
} |
| 52 |
for _, tt := range statusTestTable { |
| 53 |
sp := StatusIsNot(tt.statuses...) |
| 54 |
got := sp(tt.status) |
| 55 |
if got != tt.want { |
| 56 |
t.Errorf("Expected %v, got %v", tt.want, got) |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestWithLogging(t *testing.T) { |
| 62 |
req, err := http.NewRequest("GET", "http://example.com", nil) |
| 63 |
if err != nil { |
| 64 |
t.Errorf("Unexpected error: %v", err) |
| 65 |
} |
| 66 |
|
| 67 |
shouldLogRequest := func() bool { return true } |
| 68 |
var handler http.Handler |
| 69 |
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 70 |
handler = withLogging(withLogging(handler, DefaultStacktracePred, shouldLogRequest), DefaultStacktracePred, shouldLogRequest) |
| 71 |
|
| 72 |
func() { |
| 73 |
defer func() { |
| 74 |
if r := recover(); r == nil { |
| 75 |
t.Errorf("Expected newLogged to panic") |
| 76 |
} |
| 77 |
}() |
| 78 |
w := httptest.NewRecorder() |
| 79 |
handler.ServeHTTP(w, req) |
| 80 |
}() |
| 81 |
} |
| 82 |
|
| 83 |
func TestLogOf(t *testing.T) { |
| 84 |
tests := []struct { |
| 85 |
name string |
| 86 |
shouldLogRequest bool |
| 87 |
want string |
| 88 |
}{ |
| 89 |
{ |
| 90 |
name: "request is being logged", |
| 91 |
shouldLogRequest: true, |
| 92 |
want: "*httplog.respLogger", |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "request is not being logged", |
| 96 |
shouldLogRequest: false, |
| 97 |
want: "*httplog.passthroughLogger", |
| 98 |
}, |
| 99 |
} |
| 100 |
|
| 101 |
for _, test := range tests { |
| 102 |
t.Run(test.name, func(t *testing.T) { |
| 103 |
req, err := http.NewRequest("GET", "http://example.com", nil) |
| 104 |
if err != nil { |
| 105 |
t.Errorf("Unexpected error: %v", err) |
| 106 |
} |
| 107 |
var handler http.Handler |
| 108 |
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 109 |
got := reflect.TypeOf(LogOf(r, w)).String() |
| 110 |
if test.want != got { |
| 111 |
t.Errorf("Expected %v, got %v", test.want, got) |
| 112 |
} |
| 113 |
}) |
| 114 |
handler = withLogging(handler, DefaultStacktracePred, func() bool { return test.shouldLogRequest }) |
| 115 |
w := httptest.NewRecorder() |
| 116 |
handler.ServeHTTP(w, req) |
| 117 |
}) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestUnlogged(t *testing.T) { |
| 122 |
unloggedTests := []bool{true, false} |
| 123 |
for _, makeLogger := range unloggedTests { |
| 124 |
req, err := http.NewRequest("GET", "http://example.com", nil) |
| 125 |
if err != nil { |
| 126 |
t.Errorf("Unexpected error: %v", err) |
| 127 |
} |
| 128 |
|
| 129 |
origWriter := httptest.NewRecorder() |
| 130 |
var handler http.Handler |
| 131 |
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 132 |
got := Unlogged(r, w) |
| 133 |
if origWriter != got { |
| 134 |
t.Errorf("Expected origin writer, got %#v", got) |
| 135 |
} |
| 136 |
}) |
| 137 |
if makeLogger { |
| 138 |
handler = WithLogging(handler, DefaultStacktracePred) |
| 139 |
} |
| 140 |
|
| 141 |
handler.ServeHTTP(origWriter, req) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestLoggedStatus(t *testing.T) { |
| 146 |
req, err := http.NewRequest("GET", "http://example.com", nil) |
| 147 |
if err != nil { |
| 148 |
t.Errorf("unexpected error: %v", err) |
| 149 |
} |
| 150 |
|
| 151 |
var tw http.ResponseWriter = new(responsewriter.FakeResponseWriter) |
| 152 |
logger := newLogged(req, tw) |
| 153 |
logger.Write(nil) |
| 154 |
|
| 155 |
if logger.status != http.StatusOK { |
| 156 |
t.Errorf("expected status after write to be %v, got %v", http.StatusOK, logger.status) |
| 157 |
} |
| 158 |
|
| 159 |
tw = new(responsewriter.FakeResponseWriter) |
| 160 |
logger = newLogged(req, tw) |
| 161 |
logger.WriteHeader(http.StatusForbidden) |
| 162 |
logger.Write(nil) |
| 163 |
|
| 164 |
if logger.status != http.StatusForbidden { |
| 165 |
t.Errorf("expected status after write to remain %v, got %v", http.StatusForbidden, logger.status) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestRespLoggerWithDecoratedResponseWriter(t *testing.T) { |
| 170 |
tests := []struct { |
| 171 |
name string |
| 172 |
r func() http.ResponseWriter |
| 173 |
hijackable bool |
| 174 |
}{ |
| 175 |
{ |
| 176 |
name: "http2", |
| 177 |
r: func() http.ResponseWriter { |
| 178 |
return &responsewriter.FakeResponseWriterFlusherCloseNotifier{} |
| 179 |
}, |
| 180 |
hijackable: false, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "http/1.x", |
| 184 |
r: func() http.ResponseWriter { |
| 185 |
return &responsewriter.FakeResponseWriterFlusherCloseNotifierHijacker{} |
| 186 |
}, |
| 187 |
hijackable: true, |
| 188 |
}, |
| 189 |
} |
| 190 |
|
| 191 |
for _, test := range tests { |
| 192 |
t.Run(test.name, func(t *testing.T) { |
| 193 |
req, err := http.NewRequest("GET", "http://example.com", nil) |
| 194 |
if err != nil { |
| 195 |
t.Errorf("Unexpected error: %v", err) |
| 196 |
} |
| 197 |
|
| 198 |
var handler http.Handler |
| 199 |
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 200 |
switch v := w.(type) { |
| 201 |
case *respLogger: |
| 202 |
t.Errorf("Did not expect %v", reflect.TypeOf(v)) |
| 203 |
return |
| 204 |
default: |
| 205 |
} |
| 206 |
|
| 207 |
//lint:file-ignore SA1019 Keep supporting deprecated http.CloseNotifier |
| 208 |
if _, ok := w.(http.CloseNotifier); !ok { |
| 209 |
t.Errorf("Expected the ResponseWriter object to implement http.CloseNotifier") |
| 210 |
} |
| 211 |
if _, ok := w.(http.Flusher); !ok { |
| 212 |
t.Errorf("Expected the ResponseWriter object to implement http.Flusher") |
| 213 |
} |
| 214 |
if _, ok := w.(http.Hijacker); test.hijackable != ok { |
| 215 |
t.Errorf("http.Hijacker does not match, want: %t, got: %t", test.hijackable, ok) |
| 216 |
} |
| 217 |
}) |
| 218 |
|
| 219 |
handler = withLogging(handler, DefaultStacktracePred, func() bool { return true }) |
| 220 |
handler.ServeHTTP(test.r(), req) |
| 221 |
}) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
func TestResponseWriterDecorator(t *testing.T) { |
| 226 |
decorator := &respLogger{ |
| 227 |
w: &responsewriter.FakeResponseWriter{}, |
| 228 |
} |
| 229 |
var w http.ResponseWriter = decorator |
| 230 |
|
| 231 |
if inner := w.(responsewriter.UserProvidedDecorator).Unwrap(); inner != decorator.w { |
| 232 |
t.Errorf("Expected the decorator to return the inner http.ResponseWriter object") |
| 233 |
} |
| 234 |
} |