chore: simplify tests with logtest (#3184)
This commit is contained in:
@@ -7,12 +7,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stat"
|
||||
)
|
||||
|
||||
func init() {
|
||||
logx.Disable()
|
||||
stat.SetReporter(nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -62,10 +62,6 @@ type requestSettings struct {
|
||||
signature string
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func TestContentSecurityHandler(t *testing.T) {
|
||||
tests := []struct {
|
||||
method string
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -21,10 +20,6 @@ const (
|
||||
|
||||
var aesKey = []byte(`PdSgVkYp3s6v9y$B&E)H+MbQeThWmZq4`)
|
||||
|
||||
func init() {
|
||||
log.SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func TestCryptionHandlerGet(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
|
||||
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -3,7 +3,6 @@ package handler
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -14,10 +13,6 @@ import (
|
||||
"github.com/zeromicro/go-zero/rest/internal/response"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func TestLogHandler(t *testing.T) {
|
||||
handlers := []func(handler http.Handler) http.Handler{
|
||||
LogHandler,
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
@@ -14,10 +12,6 @@ import (
|
||||
|
||||
const conns = 4
|
||||
|
||||
func init() {
|
||||
log.SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func TestMaxConnsHandler(t *testing.T) {
|
||||
var waitGroup sync.WaitGroup
|
||||
waitGroup.Add(conns)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -10,10 +8,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func TestWithPanic(t *testing.T) {
|
||||
handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
panic("whatever")
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -12,10 +10,6 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/stat"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func TestSheddingHandlerAccept(t *testing.T) {
|
||||
metrics := stat.NewMetrics("unit-test")
|
||||
shedder := mockShedder{
|
||||
|
||||
@@ -31,14 +31,14 @@ const (
|
||||
// Notice: even if canceled in server side, 499 will be logged as well.
|
||||
func TimeoutHandler(duration time.Duration) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
if duration > 0 {
|
||||
return &timeoutHandler{
|
||||
handler: next,
|
||||
dt: duration,
|
||||
}
|
||||
if duration <= 0 {
|
||||
return next
|
||||
}
|
||||
|
||||
return next
|
||||
return &timeoutHandler{
|
||||
handler: next,
|
||||
dt: duration,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,9 +207,11 @@ func relevantCaller() runtime.Frame {
|
||||
if !strings.HasPrefix(frame.Function, "net/http.") {
|
||||
return frame
|
||||
}
|
||||
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return frame
|
||||
}
|
||||
|
||||
@@ -2,21 +2,16 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/logx/logtest"
|
||||
"github.com/zeromicro/go-zero/rest/internal/response"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func TestTimeout(t *testing.T) {
|
||||
timeoutHandler := TimeoutHandler(time.Millisecond)
|
||||
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -45,7 +40,12 @@ func TestWithTimeoutTimedout(t *testing.T) {
|
||||
timeoutHandler := TimeoutHandler(time.Millisecond)
|
||||
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
w.Write([]byte(`foo`))
|
||||
_, err := w.Write([]byte(`foo`))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
@@ -96,7 +96,12 @@ func TestTimeoutWebsocket(t *testing.T) {
|
||||
func TestTimeoutWroteHeaderTwice(t *testing.T) {
|
||||
timeoutHandler := TimeoutHandler(time.Minute)
|
||||
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`hello`))
|
||||
_, err := w.Write([]byte(`hello`))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("foo", "bar")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
@@ -145,7 +150,7 @@ func TestTimeoutHijack(t *testing.T) {
|
||||
}
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
_, _, _ = writer.Hijack()
|
||||
})
|
||||
|
||||
writer = &timeoutWriter{
|
||||
@@ -155,7 +160,7 @@ func TestTimeoutHijack(t *testing.T) {
|
||||
}
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
_, _, _ = writer.Hijack()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -165,7 +170,7 @@ func TestTimeoutPusher(t *testing.T) {
|
||||
}
|
||||
|
||||
assert.Panics(t, func() {
|
||||
handler.Push("any", nil)
|
||||
_ = handler.Push("any", nil)
|
||||
})
|
||||
|
||||
handler = &timeoutWriter{
|
||||
@@ -174,20 +179,44 @@ func TestTimeoutPusher(t *testing.T) {
|
||||
assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil))
|
||||
}
|
||||
|
||||
func TestTimeoutWriter_Hijack(t *testing.T) {
|
||||
writer := &timeoutWriter{
|
||||
w: httptest.NewRecorder(),
|
||||
h: make(http.Header),
|
||||
req: httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody),
|
||||
}
|
||||
_, _, err := writer.Hijack()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestTimeoutWroteTwice(t *testing.T) {
|
||||
c := logtest.NewCollector(t)
|
||||
writer := &timeoutWriter{
|
||||
w: &response.WithCodeResponseWriter{
|
||||
Writer: httptest.NewRecorder(),
|
||||
},
|
||||
h: make(http.Header),
|
||||
req: httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody),
|
||||
}
|
||||
writer.writeHeaderLocked(http.StatusOK)
|
||||
writer.writeHeaderLocked(http.StatusOK)
|
||||
assert.Contains(t, c.String(), "superfluous response.WriteHeader call")
|
||||
}
|
||||
|
||||
type mockedPusher struct{}
|
||||
|
||||
func (m mockedPusher) Header() http.Header {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (m mockedPusher) Write(bytes []byte) (int, error) {
|
||||
func (m mockedPusher) Write(_ []byte) (int, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (m mockedPusher) WriteHeader(statusCode int) {
|
||||
func (m mockedPusher) WriteHeader(_ int) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (m mockedPusher) Push(target string, opts *http.PushOptions) error {
|
||||
func (m mockedPusher) Push(_ string, _ *http.PushOptions) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user