go-zero/rest/handler/loghandler_test.go

154 lines
4.5 KiB
Go
Raw Normal View History

2020-07-29 18:00:04 +08:00
package handler
2020-07-26 17:09:05 +08:00
import (
2022-05-08 13:17:48 +08:00
"bytes"
"io"
2020-07-26 17:09:05 +08:00
"log"
"net/http"
"net/http/httptest"
"testing"
"time"
2020-07-29 18:00:04 +08:00
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/internal"
"github.com/zeromicro/go-zero/rest/internal/response"
2020-07-26 17:09:05 +08:00
)
func init() {
log.SetOutput(io.Discard)
2020-07-26 17:09:05 +08:00
}
func TestLogHandler(t *testing.T) {
handlers := []func(handler http.Handler) http.Handler{
LogHandler,
DetailedLogHandler,
}
for _, logHandler := range handlers {
2022-10-17 06:30:58 +08:00
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
2020-07-26 17:09:05 +08:00
handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2020-07-29 18:00:04 +08:00
r.Context().Value(internal.LogContext).(*internal.LogCollector).Append("anything")
2020-07-26 17:09:05 +08:00
w.Header().Set("X-Test", "test")
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte("content"))
assert.Nil(t, err)
flusher, ok := w.(http.Flusher)
assert.True(t, ok)
flusher.Flush()
2020-07-26 17:09:05 +08:00
}))
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
assert.Equal(t, "test", resp.Header().Get("X-Test"))
assert.Equal(t, "content", resp.Body.String())
}
}
2022-05-08 13:17:48 +08:00
func TestLogHandlerVeryLong(t *testing.T) {
var buf bytes.Buffer
for i := 0; i < limitBodyBytes<<1; i++ {
buf.WriteByte('a')
}
req := httptest.NewRequest(http.MethodPost, "http://localhost", &buf)
handler := LogHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Context().Value(internal.LogContext).(*internal.LogCollector).Append("anything")
_, _ = io.Copy(io.Discard, r.Body)
2022-05-08 13:17:48 +08:00
w.Header().Set("X-Test", "test")
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte("content"))
assert.Nil(t, err)
flusher, ok := w.(http.Flusher)
assert.True(t, ok)
flusher.Flush()
}))
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
assert.Equal(t, "test", resp.Header().Get("X-Test"))
assert.Equal(t, "content", resp.Body.String())
}
2020-07-26 17:09:05 +08:00
func TestLogHandlerSlow(t *testing.T) {
handlers := []func(handler http.Handler) http.Handler{
LogHandler,
DetailedLogHandler,
}
for _, logHandler := range handlers {
2022-10-17 06:30:58 +08:00
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
2020-07-26 17:09:05 +08:00
handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(defaultSlowThreshold + time.Millisecond*50)
2020-07-26 17:09:05 +08:00
}))
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
}
}
2021-03-15 20:11:09 +08:00
func TestDetailedLogHandler_Hijack(t *testing.T) {
resp := httptest.NewRecorder()
writer := &detailLoggedResponseWriter{
writer: &response.WithCodeResponseWriter{
Writer: resp,
2021-03-15 20:11:09 +08:00
},
}
assert.NotPanics(t, func() {
_, _, _ = writer.Hijack()
2021-03-15 20:11:09 +08:00
})
writer = &detailLoggedResponseWriter{
writer: &response.WithCodeResponseWriter{
Writer: mockedHijackable{resp},
2021-03-15 20:11:09 +08:00
},
}
assert.NotPanics(t, func() {
_, _, _ = writer.Hijack()
2021-03-15 20:11:09 +08:00
})
}
func TestSetSlowThreshold(t *testing.T) {
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
SetSlowThreshold(time.Second)
assert.Equal(t, time.Second, slowThreshold.Load())
}
2022-05-08 13:17:48 +08:00
func TestWrapMethodWithColor(t *testing.T) {
// no tty
assert.Equal(t, http.MethodGet, wrapMethod(http.MethodGet))
assert.Equal(t, http.MethodPost, wrapMethod(http.MethodPost))
assert.Equal(t, http.MethodPut, wrapMethod(http.MethodPut))
assert.Equal(t, http.MethodDelete, wrapMethod(http.MethodDelete))
assert.Equal(t, http.MethodPatch, wrapMethod(http.MethodPatch))
assert.Equal(t, http.MethodHead, wrapMethod(http.MethodHead))
assert.Equal(t, http.MethodOptions, wrapMethod(http.MethodOptions))
assert.Equal(t, http.MethodConnect, wrapMethod(http.MethodConnect))
assert.Equal(t, http.MethodTrace, wrapMethod(http.MethodTrace))
}
func TestWrapStatusCodeWithColor(t *testing.T) {
// no tty
assert.Equal(t, "200", wrapStatusCode(http.StatusOK))
assert.Equal(t, "302", wrapStatusCode(http.StatusFound))
assert.Equal(t, "404", wrapStatusCode(http.StatusNotFound))
assert.Equal(t, "500", wrapStatusCode(http.StatusInternalServerError))
assert.Equal(t, "503", wrapStatusCode(http.StatusServiceUnavailable))
}
2020-07-26 17:09:05 +08:00
func BenchmarkLogHandler(b *testing.B) {
b.ReportAllocs()
2022-10-17 06:30:58 +08:00
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
2020-07-26 17:09:05 +08:00
handler := LogHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
for i := 0; i < b.N; i++ {
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
}
}