refactor: simplify the code (#2763)

* refactor: simplify the code

* fix: fix data race

* refactor: simplify the code

* refactor: simplify the code
This commit is contained in:
chen quan
2023-01-07 13:32:56 +08:00
committed by GitHub
parent 559726112c
commit dc260f196a
3 changed files with 24 additions and 75 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/internal"
"github.com/zeromicro/go-zero/rest/internal/response"
)
func init() {
@@ -54,7 +55,7 @@ func TestLogHandlerVeryLong(t *testing.T) {
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)
_, _ = io.Copy(io.Discard, r.Body)
w.Header().Set("X-Test", "test")
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte("content"))
@@ -89,45 +90,26 @@ func TestLogHandlerSlow(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.Code)
}
}
func TestLogHandler_Hijack(t *testing.T) {
resp := httptest.NewRecorder()
writer := &loggedResponseWriter{
w: resp,
}
assert.NotPanics(t, func() {
writer.Hijack()
})
writer = &loggedResponseWriter{
w: mockedHijackable{resp},
}
assert.NotPanics(t, func() {
writer.Hijack()
})
}
func TestDetailedLogHandler_Hijack(t *testing.T) {
resp := httptest.NewRecorder()
writer := &detailLoggedResponseWriter{
writer: &loggedResponseWriter{
w: resp,
writer: &response.WithCodeResponseWriter{
Writer: resp,
},
}
assert.NotPanics(t, func() {
writer.Hijack()
_, _, _ = writer.Hijack()
})
writer = &detailLoggedResponseWriter{
writer: &loggedResponseWriter{
w: mockedHijackable{resp},
writer: &response.WithCodeResponseWriter{
Writer: mockedHijackable{resp},
},
}
assert.NotPanics(t, func() {
writer.Hijack()
_, _, _ = writer.Hijack()
})
}
func TestSetSlowThreshold(t *testing.T) {
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
SetSlowThreshold(time.Second)