Skip to content

Commit

Permalink
Adds additional bindings for multipart and form
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jul 3, 2015
1 parent 8f30478 commit 4194adc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
8 changes: 5 additions & 3 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ type StructValidator interface {
var Validator StructValidator = &defaultValidator{}

var (
JSON = jsonBinding{}
XML = xmlBinding{}
Form = formBinding{}
JSON = jsonBinding{}
XML = xmlBinding{}
Form = formBinding{}
FormPost = formPostBinding{}
FormMultipart = formMultipartBinding{}
)

func Default(method, contentType string) Binding {
Expand Down
39 changes: 39 additions & 0 deletions binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package binding

import (
"bytes"
"mime/multipart"
"net/http"
"testing"

Expand Down Expand Up @@ -64,6 +65,44 @@ func TestBindingXML(t *testing.T) {
"<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) {
var obj FooStruct
req := requestWithBody("POST", "/", `{"bar": "foo"}`)
Expand Down
30 changes: 30 additions & 0 deletions binding/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package binding
import "net/http"

type formBinding struct{}
type formPostBinding struct{}
type formMultipartBinding struct{}

func (_ formBinding) Name() string {
return "form"
Expand All @@ -22,3 +24,31 @@ func (_ formBinding) Bind(req *http.Request, obj interface{}) error {
}
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)
}

0 comments on commit 4194adc

Please sign in to comment.