Adds additional bindings for multipart and form

This commit is contained in:
Manu Mtz-Almeida 2015-07-03 04:20:00 +02:00
parent 8f3047814e
commit 4194adce4c
3 changed files with 74 additions and 3 deletions

View File

@ -36,6 +36,8 @@ var (
JSON = jsonBinding{} JSON = jsonBinding{}
XML = xmlBinding{} XML = xmlBinding{}
Form = formBinding{} Form = formBinding{}
FormPost = formPostBinding{}
FormMultipart = formMultipartBinding{}
) )
func Default(method, contentType string) Binding { func Default(method, contentType string) Binding {

View File

@ -6,6 +6,7 @@ package binding
import ( import (
"bytes" "bytes"
"mime/multipart"
"net/http" "net/http"
"testing" "testing"
@ -64,6 +65,44 @@ func TestBindingXML(t *testing.T) {
"<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>") "<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
} }
func createFormPostRequest() *http.Request {
req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar&bar=foo"))
req.Header.Set("Content-Type", MIMEPOSTForm)
return req
}
func createFormMultipartRequest() *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")
req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
return req
}
func TestBindingFormPost(t *testing.T) {
req := createFormPostRequest()
var obj FooBarStruct
FormPost.Bind(req, &obj)
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.Bar, "foo")
}
func TestBindingFormMultipart(t *testing.T) {
req := createFormMultipartRequest()
var obj FooBarStruct
FormMultipart.Bind(req, &obj)
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.Bar, "foo")
}
func TestValidationFails(t *testing.T) { func TestValidationFails(t *testing.T) {
var obj FooStruct var obj FooStruct
req := requestWithBody("POST", "/", `{"bar": "foo"}`) req := requestWithBody("POST", "/", `{"bar": "foo"}`)

View File

@ -7,6 +7,8 @@ package binding
import "net/http" import "net/http"
type formBinding struct{} type formBinding struct{}
type formPostBinding struct{}
type formMultipartBinding struct{}
func (_ formBinding) Name() string { func (_ formBinding) Name() string {
return "form" return "form"
@ -22,3 +24,31 @@ func (_ formBinding) Bind(req *http.Request, obj interface{}) error {
} }
return validate(obj) return validate(obj)
} }
func (_ formPostBinding) Name() string {
return "form-urlencoded"
}
func (_ formPostBinding) Bind(req *http.Request, obj interface{}) error {
if err := req.ParseForm(); err != nil {
return err
}
if err := mapForm(obj, req.PostForm); err != nil {
return err
}
return validate(obj)
}
func (_ formMultipartBinding) Name() string {
return "multipart/form-data"
}
func (_ formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
if err := req.ParseMultipartForm(32 << 10); err != nil {
return err
}
if err := mapForm(obj, req.MultipartForm.Value); err != nil {
return err
}
return validate(obj)
}