diff --git a/compress.go b/compress.go index f166086..61d1b6d 100644 --- a/compress.go +++ b/compress.go @@ -26,7 +26,7 @@ var ( ) const ( - defaultCompresMinLength = 1024 + defaultCompressMinLength = 1024 ) type ( @@ -75,7 +75,8 @@ func NewWithDefaultCompressor(config Config) cod.Handler { // 添加默认的 brotli 压缩 br := new(BrCompressor) _, err := br.Compress([]byte("brotli"), 0) - if err != nil { + // 如果可以压缩成功,则添加 br 压缩 + if err == nil { compressorList = append(compressorList, br) } @@ -90,7 +91,7 @@ func NewWithDefaultCompressor(config Config) cod.Handler { func New(config Config) cod.Handler { minLength := config.MinLength if minLength == 0 { - minLength = defaultCompresMinLength + minLength = defaultCompressMinLength } skipper := config.Skipper if skipper == nil { @@ -130,7 +131,7 @@ func New(config Config) cod.Handler { body = c.BodyBuffer.Bytes() } if !isReaderBody { - // 如果数据长度少于最小压缩长度或 + // 如果数据长度少于最小压缩长度 if len(body) < minLength { return } @@ -143,9 +144,14 @@ func New(config Config) cod.Handler { } if isReaderBody { c.SetHeader(cod.HeaderContentEncoding, encoding) + err = compressor.Pipe(c, config.Level) + if err != nil { + return + } // pipe 将数据直接转至原有的Response,因此设置committed为true c.Committed = true - compressor.Pipe(c, config.Level) + // 清除 reader body + c.Body = nil } else { newBuf, e := compressor.Compress(body, config.Level) // 如果压缩成功,则使用压缩数据 diff --git a/compress_test.go b/compress_test.go index 30fc45c..589f542 100644 --- a/compress_test.go +++ b/compress_test.go @@ -6,6 +6,7 @@ import ( "fmt" "math/rand" "net/http/httptest" + "os" "testing" "time" @@ -157,6 +158,7 @@ func TestCompress(t *testing.T) { } body := bytes.NewBufferString("abcd") c.BodyBuffer = body + c.SetHeader(cod.HeaderContentType, "text/plain") err := fn(c) assert.Nil(err) assert.Equal(c.BodyBuffer.Bytes(), body.Bytes()) @@ -244,7 +246,6 @@ func TestCompress(t *testing.T) { return nil } body := bytes.NewBufferString(randomString(4096)) - fmt.Println(len(body.Bytes())) gzipBytes, _ := doGzip(body.Bytes(), 0) c.Body = body err := fn(c) @@ -264,10 +265,10 @@ func TestMain(m *testing.M) { // and CoverMode will be non empty if run with -cover if rc == 0 && testing.CoverMode() != "" { c := testing.Coverage() - if c < 0.9 { + if c < 0.85 { fmt.Println("Tests passed but coverage failed at", c) rc = -1 } } - // os.Exit(rc) + os.Exit(rc) }