github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/compress/compress_test.go (about) 1 package compress 2 3 import ( 4 "compress/gzip" 5 "testing" 6 7 "github.com/andybalholm/brotli" 8 "github.com/stretchr/testify/assert" 9 "github.com/vicanso/pike/config" 10 ) 11 12 var compressTestData = []byte(`Brotli is a data format specification[2] for data streams compressed with a specific combination of the general-purpose LZ77 lossless compression algorithm, Huffman coding and 2nd order context modelling. Brotli is a compression algorithm developed by Google and works best for text compression. 13 14 Google employees Jyrki Alakuijala and Zoltán Szabadka initially developed Brotli to decrease the size of transmissions of WOFF2 web fonts, and in that context Brotli was a continuation of the development of zopfli, which is a zlib-compatible implementation of the standard gzip and deflate specifications. Brotli allows a denser packing than gzip and deflate because of several algorithmic and format level improvements: the use of context models for literals and copy distances, describing copy distances through past distances, use of move-to-front queue in entropy code selection, joint-entropy coding of literal and copy lengths, the use of graph algorithms in block splitting, and a larger backward reference window are example improvements. The Brotli specification was generalized in September 2015 for HTTP stream compression (content-encoding type 'br'). This generalized iteration also improved the compression ratio by using a pre-defined dictionary of frequently used words and phrases.`) 15 16 func TestConvertConfig(t *testing.T) { 17 assert := assert.New(t) 18 19 name := "compress-test" 20 levels := map[string]uint{ 21 "gzip": 9, 22 "br": 8, 23 } 24 configs := []config.CompressConfig{ 25 { 26 Name: name, 27 Levels: levels, 28 }, 29 } 30 opts := convertConfigs(configs) 31 assert.Equal(1, len(opts)) 32 assert.Equal(name, opts[0].Name) 33 assert.Equal(9, opts[0].Levels["gzip"]) 34 assert.Equal(8, opts[0].Levels["br"]) 35 } 36 37 func TestCompressLevel(t *testing.T) { 38 assert := assert.New(t) 39 srv := NewService() 40 assert.Equal(gzip.DefaultCompression, srv.GetLevel(EncodingGzip)) 41 assert.Equal(brotli.DefaultCompression, srv.GetLevel(EncodingBrotli)) 42 43 srv.SetLevels(map[string]int{ 44 EncodingGzip: 1, 45 EncodingBrotli: 2, 46 }) 47 assert.Equal(1, srv.GetLevel(EncodingGzip)) 48 assert.Equal(2, srv.GetLevel(EncodingBrotli)) 49 } 50 51 func TestCompressList(t *testing.T) { 52 assert := assert.New(t) 53 srvList := NewServices([]CompressOption{ 54 { 55 Name: "test", 56 Levels: map[string]int{ 57 "gzip": 1, 58 "br": 2, 59 }, 60 }, 61 }) 62 srv := srvList.Get("test") 63 assert.Equal(1, srv.GetLevel("gzip")) 64 assert.Equal(2, srv.GetLevel("br")) 65 66 srvList.Reset([]CompressOption{ 67 { 68 Name: "test1", 69 Levels: map[string]int{ 70 "gzip": 3, 71 "br": 4, 72 }, 73 }, 74 }) 75 // compress并不删除原有的srv 76 assert.Equal(srv, srvList.Get("test")) 77 78 srv = srvList.Get("test1") 79 assert.Equal(3, srv.GetLevel("gzip")) 80 assert.Equal(4, srv.GetLevel("br")) 81 82 // 从默认获取 83 assert.Equal(defaultCompressSrv, Get("test")) 84 Reset([]config.CompressConfig{ 85 { 86 Name: "test", 87 Levels: map[string]uint{ 88 "gzip": 3, 89 "br": 4, 90 }, 91 }, 92 }) 93 assert.Equal(3, Get("test").GetLevel("gzip")) 94 assert.Equal(4, Get("test").GetLevel("br")) 95 } 96 97 func TestDecompress(t *testing.T) { 98 assert := assert.New(t) 99 data := compressTestData 100 // 不同的压缩解压 101 tests := []struct { 102 fn func() ([]byte, error) 103 encoding string 104 }{ 105 { 106 fn: func() ([]byte, error) { 107 return Get("").Gzip(data) 108 }, 109 encoding: EncodingGzip, 110 }, 111 { 112 fn: func() ([]byte, error) { 113 return Get("").Brotli(data) 114 }, 115 encoding: EncodingBrotli, 116 }, 117 { 118 fn: func() ([]byte, error) { 119 return doLZ4Encode(data, 0) 120 }, 121 encoding: EncodingLZ4, 122 }, 123 { 124 fn: func() ([]byte, error) { 125 dst := doSnappyEncode(data) 126 return dst, nil 127 }, 128 encoding: EncodingSnappy, 129 }, 130 { 131 fn: func() ([]byte, error) { 132 return data, nil 133 }, 134 encoding: "", 135 }, 136 } 137 for _, tt := range tests { 138 result, err := tt.fn() 139 assert.Nil(err) 140 assert.NotEmpty(result) 141 result, err = Get("").Decompress(tt.encoding, result) 142 assert.Nil(err) 143 assert.Equal(data, result) 144 } 145 _, err := Get("").Decompress("a", nil) 146 assert.Equal(notSupportedEncoding, err) 147 }