github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/readers/gzip_test.go (about) 1 package readers 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "io" 7 "testing" 8 9 "github.com/rclone/rclone/lib/random" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 type checkClose struct { 15 io.Reader 16 closed bool 17 } 18 19 func (cc *checkClose) Close() error { 20 cc.closed = true 21 return nil 22 } 23 24 func TestGzipReader(t *testing.T) { 25 // Create some compressed data 26 data := random.String(1000) 27 var out bytes.Buffer 28 zw := gzip.NewWriter(&out) 29 _, err := io.Copy(zw, bytes.NewBufferString(data)) 30 require.NoError(t, err) 31 require.NoError(t, zw.Close()) 32 gzData := out.Bytes() 33 34 // Check we can decompress it 35 cc := &checkClose{Reader: bytes.NewBuffer(gzData)} 36 var decompressed bytes.Buffer 37 zr, err := NewGzipReader(cc) 38 require.NoError(t, err) 39 _, err = io.Copy(&decompressed, zr) 40 require.NoError(t, err) 41 assert.Equal(t, data, decompressed.String()) 42 43 // Check the underlying close gets called 44 assert.False(t, cc.closed) 45 require.NoError(t, zr.Close()) 46 assert.True(t, cc.closed) 47 }