Skip to content

Commit

Permalink
fix: new compress config with compressor
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 23, 2020
1 parent 6cb84e4 commit 48ff7a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
// 如果出错直接返回
Expand Down
12 changes: 12 additions & 0 deletions middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 48ff7a2

Please sign in to comment.