github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/transformer/z_tsfm_json_test.go (about)

     1  package transformer_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  
    10  	. "github.com/onsi/gomega"
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
    14  	. "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/transformer"
    15  	vldterr "github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
    16  	"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
    17  )
    18  
    19  type S string
    20  
    21  func (s *S) UnmarshalText(data []byte) error {
    22  	return errors.Errorf("err")
    23  }
    24  
    25  func TestJSON(t *testing.T) {
    26  	data := struct {
    27  		Data struct {
    28  			S           S    `json:"s,omitempty"`
    29  			Bool        bool `json:"bool"`
    30  			StructSlice []struct {
    31  				Name string `json:"name"`
    32  			} `json:"structSlice"`
    33  			StringSlice []string `json:"stringSlice"`
    34  			NestedSlice []struct {
    35  				Names []string `json:"names"`
    36  			} `json:"nestedSlice"`
    37  		} `json:"data"`
    38  	}{}
    39  
    40  	ct, _ := DefaultFactory.NewTransformer(
    41  		bgctx,
    42  		typesx.FromReflectType(reflect.TypeOf(data)), Option{},
    43  	)
    44  
    45  	t.Run("EncodeTo", func(t *testing.T) {
    46  		b := bytes.NewBuffer(nil)
    47  		h := http.Header{}
    48  
    49  		err := ct.EncodeTo(context.Background(), WriterWithHeader(b, h), data)
    50  		NewWithT(t).Expect(err).To(BeNil())
    51  		NewWithT(t).Expect(h.Get(httpx.HeaderContentType)).
    52  			To(Equal("application/json; charset=utf-8"))
    53  	})
    54  
    55  	t.Run("EncodeWithReflectValue", func(t *testing.T) {
    56  		b := bytes.NewBuffer(nil)
    57  		h := http.Header{}
    58  
    59  		err := ct.EncodeTo(context.Background(), WriterWithHeader(b, h), reflect.ValueOf(data))
    60  		NewWithT(t).Expect(err).To(BeNil())
    61  		NewWithT(t).Expect(h.Get(httpx.HeaderContentType)).
    62  			To(Equal("application/json; charset=utf-8"))
    63  	})
    64  
    65  	t.Run("DecodeAndValidate failed", func(t *testing.T) {
    66  		b := bytes.NewBufferString(`{`)
    67  		err := ct.DecodeFrom(context.Background(), b, &data)
    68  		NewWithT(t).Expect(err).NotTo(BeNil())
    69  	})
    70  
    71  	t.Run("DecodeAndValidate success", func(t *testing.T) {
    72  		b := bytes.NewBufferString(`{}`)
    73  		err := ct.DecodeFrom(context.Background(), b, reflect.ValueOf(&data))
    74  		NewWithT(t).Expect(err).To(BeNil())
    75  	})
    76  
    77  	t.Run("DecodeAndValidateFailedWithLocation", func(t *testing.T) {
    78  		cases := []struct {
    79  			json     string
    80  			location string
    81  		}{{
    82  			`{
    83  	"data": {
    84  		"s": "111",
    85  		"bool": true
    86  	}
    87  }`, "data.s",
    88  		},
    89  			{
    90  				`
    91  {
    92   	"data": {
    93  		"bool": ""
    94  	}
    95  }
    96  `, "data.bool",
    97  			},
    98  			{
    99  				`
   100  {
   101  		"data": {
   102  			"structSlice": [
   103  				{"name":"{"},
   104  				{"name":"1"},
   105  				{"name": { "test": 1 }},
   106  				{"name":"1"}
   107  			]
   108  		}
   109  }`,
   110  				"data.structSlice[2].name",
   111  			},
   112  			{
   113  				`
   114  		{
   115  			"data": {
   116  				"stringSlice":["1","2",3]
   117  			}
   118  		}`,
   119  				"data.stringSlice[2]",
   120  			},
   121  			{
   122  				`
   123  		{
   124  			"data": {
   125  				"stringSlice":["1","2",3]
   126  			}
   127  		}`,
   128  				"data.stringSlice[2]",
   129  			},
   130  			{
   131  				`
   132  		{
   133  			"data": {
   134  				"bool": true,
   135  				"nestedSlice": [
   136  					{ "names": ["1","2","3"] },
   137  			        { "names": ["1","\"2", 3] }
   138  				]
   139  			}
   140  		}
   141  		`, "data.nestedSlice[1].names[2]",
   142  			},
   143  		}
   144  
   145  		for _, c := range cases {
   146  			b := bytes.NewBufferString(c.json)
   147  			err := ct.DecodeFrom(context.Background(), b, &data)
   148  
   149  			err.(*vldterr.ErrorSet).Each(func(fe *vldterr.FieldError) {
   150  				NewWithT(t).Expect(fe.Field.String()).To(Equal(c.location))
   151  			})
   152  		}
   153  	})
   154  }