Navigation Menu

Skip to content

Commit

Permalink
update #83
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Nov 11, 2019
1 parent 4318e30 commit 63d1b67
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 17 deletions.
5 changes: 3 additions & 2 deletions debug.go
Expand Up @@ -16,9 +16,9 @@ func ToBodyType(s string) color.BodyType {
case "json":
return color.JsonType
case "xml":
return color.XmlType
//return color.XmlType //TODO open
case "yaml":
return color.YamlType
//return color.YamlType //TODO open
}

return color.TxtType
Expand Down Expand Up @@ -113,6 +113,7 @@ func (do *DebugOption) debugPrint(req *http.Request, rsp *http.Response) error {
if _, err := io.Copy(w, r); err != nil {
return err
}

fmt.Fprintf(w, "\r\n\r\n")
}

Expand Down
21 changes: 18 additions & 3 deletions encode/form.go
Expand Up @@ -21,14 +21,29 @@ func NewFormEncode(b *bytes.Buffer) *FormEncode {
return &FormEncode{Writer: multipart.NewWriter(b)}
}

func toBytes(v reflect.Value) (all []byte, err error) {
switch v := v.Interface().(type) {
func toBytes(val reflect.Value) (all []byte, err error) {
switch v := val.Interface().(type) {
case string:
all = core.StringToBytes(v)
case []byte:
all = v
default:
return nil, fmt.Errorf("unknown type toBytes:%T", v)

if val.Kind() == reflect.Interface {
val = reflect.ValueOf(val.Interface())
}

switch t := val.Kind(); t {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
case reflect.Float32, reflect.Float64:
case reflect.String:
default:
return nil, fmt.Errorf("unknown type toBytes:%T, kind:%v", v, val.Kind())
}

s := valToStr(val, emptyField)
all = core.StringToBytes(s)
}

return all, nil
Expand Down
77 changes: 65 additions & 12 deletions group_test.go
Expand Up @@ -253,8 +253,20 @@ func TestBindHeader(t *testing.T) {
}

type testForm struct {
Mode string `form:"mode"`
Text string `form:"text"`
Mode string `form:"mode"`
Text string `form:"text"`
Int int `form:"int"`
Int8 int8 `form:"int8"`
Int16 int16 `form:"int16"`
Int32 int32 `form:"int32"`
Int64 int64 `form:"int64"`
Uint uint `form:"uint"`
Uint8 uint8 `form:"uint8"`
Uint16 uint16 `form:"uint16"`
Uint32 uint32 `form:"uint32"`
Uint64 uint64 `form:"uint64"`
Float32 float32 `form:"float32"`
Float64 float64 `form:"float64"`
//Voice []byte `form:"voice" form-mem:"true"` //todo open
}

Expand All @@ -265,16 +277,23 @@ func setupForm(t *testing.T, reqTestForm testForm) *gin.Engine {
t2 := testForm{}
err := c.Bind(&t2)
assert.NoError(t, err)
//assert.Equal(t, reqTestForm, t2)
assert.Equal(t, reqTestForm.Mode, t2.Mode)
assert.Equal(t, reqTestForm.Text, t2.Text)
assert.Equal(t, reqTestForm, t2)
/*
assert.Equal(t, reqTestForm.Mode, t2.Mode)
assert.Equal(t, reqTestForm.Text, t2.Text)
*/
})
return router
}

type testForm2 struct {
Mode string `form:"mode"`
Text string `form:"text"`
Mode string `form:"mode"`
Text string `form:"text"`
Int int `form:"int"`
Uint uint `form:"uint"`
Float32 float32 `form:"float32"`
Float64 float64 `form:"float64"`

Voice *multipart.FileHeader `form:"voice"`
Voice2 *multipart.FileHeader `form:"voice2"`
ReqVoice []byte
Expand All @@ -291,6 +310,10 @@ func setupForm2(t *testing.T, reqTestForm testForm2) *gin.Engine {
//assert.Equal(t, reqTestForm, t2)
assert.Equal(t, reqTestForm.Mode, t2.Mode)
assert.Equal(t, reqTestForm.Text, t2.Text)
assert.Equal(t, reqTestForm.Int, t2.Int)
assert.Equal(t, reqTestForm.Uint, t2.Uint)
assert.Equal(t, reqTestForm.Float32, t2.Float32)
//assert.Equal(t, reqTestForm.Float64, t2.Float64)

assert.NotNil(t, t2.Voice)
fd, err := t2.Voice.Open()
Expand Down Expand Up @@ -321,6 +344,10 @@ func TestSetFormMap(t *testing.T) {
Mode: "A",
Text: "good morning",
ReqVoice2: []byte("pcm pcm"),
Int: 1,
Uint: 2,
Float32: 1.12,
Float64: 3.14,
}

var err error
Expand All @@ -334,16 +361,38 @@ func TestSetFormMap(t *testing.T) {

g := New(nil)
code := 0
err = g.POST(ts.URL + "/test.form").SetForm(H{"mode": "A", "text": "good morning",
"voice": core.FormFile("testdata/voice.pcm"), "voice2": core.FormMem("pcm pcm")}).Code(&code).Do()
err = g.POST(ts.URL + "/test.form").
SetForm(H{"mode": "A",
"text": "good morning",
"voice": core.FormFile("testdata/voice.pcm"),
"voice2": core.FormMem("pcm pcm"),
"int": 1,
"uint": 2,
"float32": 1.12,
"float64": 3.14,
}).
Code(&code).
Do()

assert.NoError(t, err)
}

func TestSetFormStruct(t *testing.T) {
reqTestForm := testForm{
Mode: "A",
Text: "good morning",
Mode: "A",
Text: "good morning",
Int: 1,
Int8: 2,
Int16: 3,
Int32: 4,
Int64: 5,
Uint: 6,
Uint8: 7,
Uint16: 8,
Uint32: 9,
Uint64: 10,
Float32: 1.23,
Float64: 3.14,
//Voice: []byte("pcm data"),
}

Expand All @@ -354,7 +403,11 @@ func TestSetFormStruct(t *testing.T) {

g := New(nil)
code := 0
err := g.POST(ts.URL + "/test.form").SetForm(&reqTestForm).Code(&code).Do()
err := g.POST(ts.URL + "/test.form").
//Debug(true).
SetForm(&reqTestForm).
Code(&code).
Do()

assert.NoError(t, err)
}
Expand Down

0 comments on commit 63d1b67

Please sign in to comment.