github.com/ezoic/ws@v1.0.4-0.20220713205711-5c1d69e074c5/write_test.go (about)

     1  package ws
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"testing"
     8  )
     9  
    10  func TestWriteHeader(t *testing.T) {
    11  	for i, test := range RWTestCases {
    12  		t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
    13  			buf := &bytes.Buffer{}
    14  			err := WriteHeader(buf, test.Header)
    15  			if test.Err && err == nil {
    16  				t.Errorf("expected error, got nil")
    17  			}
    18  			if !test.Err && err != nil {
    19  				t.Errorf("unexpected error: %s", err)
    20  			}
    21  			if test.Err {
    22  				return
    23  			}
    24  			if bts := buf.Bytes(); !bytes.Equal(bts, test.Data) {
    25  				t.Errorf("WriteHeader()\nwrote:\n\t%08b\nwant:\n\t%08b", bts, test.Data)
    26  			}
    27  		})
    28  	}
    29  }
    30  
    31  func BenchmarkWriteHeader(b *testing.B) {
    32  	for _, bench := range RWBenchCases {
    33  		b.Run(bench.label, func(b *testing.B) {
    34  			for i := 0; i < b.N; i++ {
    35  				if err := WriteHeader(ioutil.Discard, bench.header); err != nil {
    36  					b.Fatal(err)
    37  				}
    38  			}
    39  		})
    40  	}
    41  }