package internal import ( "bytes" "io/ioutil" "testing" "github.com/stretchr/testify/require" ) func TestGzipEncodeDecode(t *testing.T) { enc, err := NewGzipEncoder() require.NoError(t, err) dec, err := NewGzipDecoder() require.NoError(t, err) payload, err := enc.Encode([]byte("howdy")) require.NoError(t, err) actual, err := dec.Decode(payload) require.NoError(t, err) require.Equal(t, "howdy", string(actual)) } func TestGzipReuse(t *testing.T) { enc, err := NewGzipEncoder() require.NoError(t, err) dec, err := NewGzipDecoder() require.NoError(t, err) payload, err := enc.Encode([]byte("howdy")) require.NoError(t, err) actual, err := dec.Decode(payload) require.NoError(t, err) require.Equal(t, "howdy", string(actual)) payload, err = enc.Encode([]byte("doody")) require.NoError(t, err) actual, err = dec.Decode(payload) require.NoError(t, err) require.Equal(t, "doody", string(actual)) } func TestIdentityEncodeDecode(t *testing.T) { enc := NewIdentityEncoder() dec := NewIdentityDecoder() payload, err := enc.Encode([]byte("howdy")) require.NoError(t, err) actual, err := dec.Decode(payload) require.NoError(t, err) require.Equal(t, "howdy", string(actual)) } func TestStreamIdentityDecode(t *testing.T) { var r bytes.Buffer n, err := r.Write([]byte("howdy")) require.NoError(t, err) require.Equal(t, 5, n) dec, err := NewStreamContentDecoder("identity", &r) require.NoError(t, err) data, err := ioutil.ReadAll(dec) require.NoError(t, err) require.Equal(t, []byte("howdy"), data) } func TestStreamGzipDecode(t *testing.T) { enc, err := NewGzipEncoder() require.NoError(t, err) written, err := enc.Encode([]byte("howdy")) require.NoError(t, err) w := bytes.NewBuffer(written) dec, err := NewStreamContentDecoder("gzip", w) require.NoError(t, err) b := make([]byte, 10) n, err := dec.Read(b) require.NoError(t, err) require.Equal(t, 5, n) require.Equal(t, []byte("howdy"), b[:n]) }