github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/ezhttp/utils_test.go (about)

     1  package ezhttp
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  type testObject struct {
    12  	A int    `xml:"a" json:"a"`
    13  	B string `xml:"b" json:"b"`
    14  }
    15  
    16  func TestDecodeJSON(t *testing.T) {
    17  	var data testObject
    18  	r := bytes.NewBufferString(`{"a":1,"b":"2"}`)
    19  	err := DecodeJSON(r, &data)
    20  	require.Nil(t, err)
    21  
    22  	want := testObject{A: 1, B: "2"}
    23  	assert.Equal(t, want, data)
    24  }
    25  
    26  func TestDecodeXML(t *testing.T) {
    27  	var data testObject
    28  	r := bytes.NewBufferString(`<testObject><a>123</a><b>456</b></testObject>`)
    29  	err := DecodeXML(r, &data)
    30  	require.Nil(t, err)
    31  
    32  	want := testObject{A: 123, B: "456"}
    33  	assert.Equal(t, want, data)
    34  }