github.com/TeaOSLab/EdgeNode@v1.3.8/internal/compressions/reader_deflate_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package compressions_test
     4  
     5  import (
     6  	"bytes"
     7  	"github.com/TeaOSLab/EdgeNode/internal/compressions"
     8  	"io"
     9  	"testing"
    10  )
    11  
    12  func TestDeflateReader(t *testing.T) {
    13  	for _, testString := range []string{"Hello", "World", "Ni", "Hao"} {
    14  		t.Log("===", testString, "===")
    15  		var buf = &bytes.Buffer{}
    16  		writer, err := compressions.NewDeflateWriter(buf, 5)
    17  		if err != nil {
    18  			t.Fatal(err)
    19  		}
    20  		_, err = writer.Write([]byte(testString))
    21  		if err != nil {
    22  			t.Fatal(err)
    23  		}
    24  		err = writer.Close()
    25  		if err != nil {
    26  			t.Fatal(err)
    27  		}
    28  
    29  		reader, err := compressions.NewDeflateReader(buf)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  		var data = make([]byte, 4096)
    34  		for {
    35  			n, err := reader.Read(data)
    36  			if n > 0 {
    37  				t.Log(string(data[:n]))
    38  			}
    39  			if err != nil {
    40  				if err == io.EOF {
    41  					break
    42  				}
    43  				t.Fatal(err)
    44  			}
    45  		}
    46  		err = reader.Close()
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  	}
    51  }