gitee.com/h79/goutils@v1.22.10/common/http/http_test.go (about)

     1  package http
     2  
     3  import (
     4  	"bytes"
     5  	"mime/multipart"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  type DDDStruct struct {
    11  }
    12  
    13  func (d *DDDStruct) CreateForm(w *multipart.Writer) error {
    14  	return nil
    15  }
    16  
    17  type DDD struct {
    18  	D1 string
    19  	D2 int     `json:"d2,omitempty"`
    20  	D3 float32 `url:"d3,omitempty"`
    21  	D4 string  `url:"d4"`
    22  }
    23  
    24  func TestHttp_ConvertTo(t *testing.T) {
    25  	v := ConvertTo(&DDD{})
    26  	t.Log(v.Encode())
    27  }
    28  
    29  func TestForm(t *testing.T) {
    30  	var b bytes.Buffer
    31  	w := multipart.NewWriter(&b)
    32  	defer w.Close()
    33  
    34  	d := DDDStruct{}
    35  	val := reflect.ValueOf(&d)
    36  	if val.CanInterface() && val.Type().Implements(FormType) {
    37  		fm := val.Interface().(Form)
    38  		fm.CreateForm(w)
    39  	}
    40  }