diff --git a/.travis.yml b/.travis.yml index 4184d75..487b1b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,5 +8,5 @@ go: - master script: - - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0 + - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin latest - env GO111MODULE=on make lint && make test && make bench diff --git a/middleware/compress.go b/middleware/compress.go index ffa7dfb..15867af 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -79,7 +79,9 @@ func (conf *CompressConfig) AddCompressor(compressor Compressor) { // NewCompressConfig create a new compress config func NewCompressConfig(compressors ...Compressor) CompressConfig { cfg := CompressConfig{} - cfg.AddCompressor(new(GzipCompressor)) + for _, compressor := range compressors { + cfg.AddCompressor(compressor) + } return cfg } @@ -151,6 +153,8 @@ func NewCompress(config CompressConfig) elton.Handler { continue } if isReaderBody { + // 压缩时清除content length + c.Header().Del(elton.HeaderContentLength) fillHeader(encoding) err = compressor.Pipe(c) // 如果出错直接返回 diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 4fbc7ef..2f07423 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -74,6 +74,17 @@ func TestAcceptEncoding(t *testing.T) { assert.Equal(elton.Gzip, encoding) } +func TestNewCompressConfig(t *testing.T) { + assert := assert.New(t) + conf := NewCompressConfig() + assert.Empty(conf.Compressors) + + gzipCompressor := new(GzipCompressor) + conf = NewCompressConfig(gzipCompressor) + assert.Equal(1, len(conf.Compressors)) + assert.Equal(gzipCompressor, conf.Compressors[0]) +} + func TestCompress(t *testing.T) { t.Run("skip", func(t *testing.T) { assert := assert.New(t) @@ -289,6 +300,7 @@ func TestCompress(t *testing.T) { return nil } body := bytes.NewBufferString(randomString(4096)) + c.SetHeader(elton.HeaderContentLength, "4096") c.Body = body err := fn(c) assert.True(c.Committed)