github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/net/http/httpjson/io_test.go (about)

     1  package httpjson
     2  
     3  import (
     4  	"context"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestWriteArray(t *testing.T) {
    11  	examples := []struct {
    12  		in   []int
    13  		want string
    14  	}{
    15  		{nil, "[]"},
    16  		{[]int{}, "[]"},
    17  		{make([]int, 0), "[]"},
    18  	}
    19  
    20  	for _, ex := range examples {
    21  		rec := httptest.NewRecorder()
    22  		Write(context.Background(), rec, 200, ex.in)
    23  		got := strings.TrimSpace(rec.Body.String())
    24  		if got != ex.want {
    25  			t.Errorf("Write(%v) = %v want %v", ex.in, got, ex.want)
    26  		}
    27  	}
    28  }
    29  
    30  type errResponse struct {
    31  	*httptest.ResponseRecorder
    32  	err error
    33  }
    34  
    35  func (r *errResponse) Write([]byte) (int, error) {
    36  	return 0, r.err
    37  }