Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for supporting file binding #1264

Merged
merged 10 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func Default(method, contentType string) Binding {
return ProtoBuf
case MIMEMSGPACK, MIMEMSGPACK2:
return MsgPack
case MIMEMultipartPOSTForm:
return FormMultipart
default: //case MIMEPOSTForm, MIMEMultipartPOSTForm:
thinkerou marked this conversation as resolved.
Show resolved Hide resolved
return Form
}
Expand Down
88 changes: 86 additions & 2 deletions binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"testing"
"time"

Expand All @@ -29,6 +31,18 @@ type FooBarStruct struct {
Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`
}

type FooBarFileStruct struct {
FooBarStruct
File *multipart.FileHeader `form:"file" binding:"required"`
}

type FooBarFileFailStruct struct {
FooBarStruct
File *multipart.FileHeader `invalid_name:"file" binding:"required"`
// for unexport test
data *multipart.FileHeader `form:"data" binding:"required"`
}

type FooStructUseNumber struct {
Foo interface{} `json:"foo" binding:"required"`
}
Expand Down Expand Up @@ -152,8 +166,8 @@ func TestBindingDefault(t *testing.T) {
assert.Equal(t, Default("POST", MIMEPOSTForm), Form)
assert.Equal(t, Default("PUT", MIMEPOSTForm), Form)

assert.Equal(t, Default("POST", MIMEMultipartPOSTForm), Form)
assert.Equal(t, Default("PUT", MIMEMultipartPOSTForm), Form)
assert.Equal(t, Default("POST", MIMEMultipartPOSTForm), FormMultipart)
assert.Equal(t, Default("PUT", MIMEMultipartPOSTForm), FormMultipart)

assert.Equal(t, Default("POST", MIMEPROTOBUF), ProtoBuf)
assert.Equal(t, Default("PUT", MIMEPROTOBUF), ProtoBuf)
Expand Down Expand Up @@ -413,6 +427,48 @@ func createFormPostRequestFail() *http.Request {
return req
}

func createFormFilesMultipartRequest() *http.Request {
boundary := "--testboundary"
body := new(bytes.Buffer)
mw := multipart.NewWriter(body)
defer mw.Close()

mw.SetBoundary(boundary)
mw.WriteField("foo", "bar")
mw.WriteField("bar", "foo")

f, _ := os.Open("form.go")
defer f.Close()
fw, _ := mw.CreateFormFile("file", "form.go")
io.Copy(fw, f)

req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)

return req
}

func createFormFilesMultipartRequestFail() *http.Request {
boundary := "--testboundary"
body := new(bytes.Buffer)
mw := multipart.NewWriter(body)
defer mw.Close()

mw.SetBoundary(boundary)
mw.WriteField("foo", "bar")
mw.WriteField("bar", "foo")

f, _ := os.Open("form.go")
defer f.Close()
fw, _ := mw.CreateFormFile("file_foo", "form_foo.go")
io.Copy(fw, f)

req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)

return req
}

func createFormMultipartRequest() *http.Request {
boundary := "--testboundary"
body := new(bytes.Buffer)
Expand Down Expand Up @@ -457,6 +513,34 @@ func TestBindingFormPostFail(t *testing.T) {
assert.Error(t, err)
}

func TestBindingFormFilesMultipart(t *testing.T) {
req := createFormFilesMultipartRequest()
var obj FooBarFileStruct
FormMultipart.Bind(req, &obj)

// file from os
f, _ := os.Open("form.go")
defer f.Close()
fileActual, _ := ioutil.ReadAll(f)

// file from multipart
mf, _ := obj.File.Open()
defer mf.Close()
fileExpect, _ := ioutil.ReadAll(mf)

assert.Equal(t, FormMultipart.Name(), "multipart/form-data")
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.Bar, "foo")
assert.Equal(t, fileExpect, fileActual)
}

func TestBindingFormFilesMultipartFail(t *testing.T) {
req := createFormFilesMultipartRequestFail()
var obj FooBarFileFailStruct
err := FormMultipart.Bind(req, &obj)
assert.Error(t, err)
}

func TestBindingFormMultipart(t *testing.T) {
req := createFormMultipartRequest()
var obj FooBarStruct
Expand Down
5 changes: 5 additions & 0 deletions binding/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
if err := mapForm(obj, req.MultipartForm.Value); err != nil {
return err
}

if err := mapFiles(obj, req); err != nil {
thinkerou marked this conversation as resolved.
Show resolved Hide resolved
return err
}

return validate(obj)
}
30 changes: 30 additions & 0 deletions binding/form_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,41 @@ package binding

import (
"errors"
"fmt"
"net/http"
"reflect"
"strconv"
"time"
)

func mapFiles(ptr interface{}, req *http.Request) error {
typ := reflect.TypeOf(ptr).Elem()
val := reflect.ValueOf(ptr).Elem()
for i := 0; i < typ.NumField(); i++ {
typeField := typ.Field(i)
structField := val.Field(i)

t := fmt.Sprintf("%s", typeField.Type)
if string(t) != "*multipart.FileHeader" {
continue
}

inputFieldName := typeField.Tag.Get("form")
if inputFieldName == "" {
inputFieldName = typeField.Name
}

_, fileHeader, err := req.FormFile(inputFieldName)
if err != nil {
return err
}

structField.Set(reflect.ValueOf(fileHeader))

}
return nil
}

func mapForm(ptr interface{}, form map[string][]string) error {
typ := reflect.TypeOf(ptr).Elem()
val := reflect.ValueOf(ptr).Elem()
Expand Down