Adds additional bindings for multipart and form
This commit is contained in:
		| @ -36,6 +36,8 @@ var ( | ||||
| 	JSON          = jsonBinding{} | ||||
| 	XML           = xmlBinding{} | ||||
| 	Form          = formBinding{} | ||||
| 	FormPost      = formPostBinding{} | ||||
| 	FormMultipart = formMultipartBinding{} | ||||
| ) | ||||
|  | ||||
| func Default(method, contentType string) Binding { | ||||
|  | ||||
| @ -6,6 +6,7 @@ package binding | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"mime/multipart" | ||||
| 	"net/http" | ||||
| 	"testing" | ||||
|  | ||||
| @ -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"}`) | ||||
|  | ||||
| @ -7,6 +7,8 @@ package binding | ||||
| import "net/http" | ||||
|  | ||||
| type formBinding struct{} | ||||
| type formPostBinding struct{} | ||||
| type formMultipartBinding struct{} | ||||
|  | ||||
| func (_ formBinding) Name() string { | ||||
| 	return "form" | ||||
| @ -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) | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user