- More unit tests
- Improves HTML debug render - InputHolder removed - More debug logs
This commit is contained in:
		| @ -28,25 +28,22 @@ type Binding interface { | ||||
| var _validator = validator.NewValidator("binding", validator.BakedInValidators) | ||||
|  | ||||
| var ( | ||||
| 	JSON     = jsonBinding{} | ||||
| 	XML      = xmlBinding{} | ||||
| 	GETForm  = getFormBinding{} | ||||
| 	POSTForm = postFormBinding{} | ||||
| 	JSON = jsonBinding{} | ||||
| 	XML  = xmlBinding{} | ||||
| 	Form = formBinding{} | ||||
| ) | ||||
|  | ||||
| func Default(method, contentType string) Binding { | ||||
| 	if method == "GET" { | ||||
| 		return GETForm | ||||
| 		return Form | ||||
| 	} else { | ||||
| 		switch contentType { | ||||
| 		case MIMEPOSTForm: | ||||
| 			return POSTForm | ||||
| 		case MIMEJSON: | ||||
| 			return JSON | ||||
| 		case MIMEXML, MIMEXML2: | ||||
| 			return XML | ||||
| 		default: | ||||
| 			return GETForm | ||||
| 			return Form | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -17,8 +17,8 @@ type FooStruct struct { | ||||
| } | ||||
|  | ||||
| func TestBindingDefault(t *testing.T) { | ||||
| 	assert.Equal(t, Default("GET", ""), GETForm) | ||||
| 	assert.Equal(t, Default("GET", MIMEJSON), GETForm) | ||||
| 	assert.Equal(t, Default("GET", ""), Form) | ||||
| 	assert.Equal(t, Default("GET", MIMEJSON), Form) | ||||
|  | ||||
| 	assert.Equal(t, Default("POST", MIMEJSON), JSON) | ||||
| 	assert.Equal(t, Default("PUT", MIMEJSON), JSON) | ||||
| @ -26,54 +26,71 @@ func TestBindingDefault(t *testing.T) { | ||||
| 	assert.Equal(t, Default("POST", MIMEXML), XML) | ||||
| 	assert.Equal(t, Default("PUT", MIMEXML2), XML) | ||||
|  | ||||
| 	assert.Equal(t, Default("POST", MIMEPOSTForm), POSTForm) | ||||
| 	assert.Equal(t, Default("DELETE", MIMEPOSTForm), POSTForm) | ||||
| 	assert.Equal(t, Default("POST", MIMEPOSTForm), Form) | ||||
| 	assert.Equal(t, Default("DELETE", MIMEPOSTForm), Form) | ||||
| } | ||||
|  | ||||
| func TestBindingJSON(t *testing.T) { | ||||
| 	testBinding(t, | ||||
| 	testBodyBinding(t, | ||||
| 		JSON, "json", | ||||
| 		"/", "/", | ||||
| 		`{"foo": "bar"}`, `{"bar": "foo"}`) | ||||
| } | ||||
|  | ||||
| func TestBindingPOSTForm(t *testing.T) { | ||||
| 	testBinding(t, | ||||
| 		POSTForm, "post_form", | ||||
| func TestBindingForm(t *testing.T) { | ||||
| 	testFormBinding(t, "POST", | ||||
| 		"/", "/", | ||||
| 		"foo=bar", "bar=foo") | ||||
| } | ||||
|  | ||||
| func TestBindingGETForm(t *testing.T) { | ||||
| 	testBinding(t, | ||||
| 		GETForm, "get_form", | ||||
| func TestBindingForm2(t *testing.T) { | ||||
| 	testFormBinding(t, "GET", | ||||
| 		"/?foo=bar", "/?bar=foo", | ||||
| 		"", "") | ||||
| } | ||||
|  | ||||
| func TestBindingXML(t *testing.T) { | ||||
| 	testBinding(t, | ||||
| 	testBodyBinding(t, | ||||
| 		XML, "xml", | ||||
| 		"/", "/", | ||||
| 		"<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>") | ||||
| } | ||||
|  | ||||
| func testBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { | ||||
| 	assert.Equal(t, b.Name(), name) | ||||
| func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) { | ||||
| 	b := Form | ||||
| 	assert.Equal(t, b.Name(), "query") | ||||
|  | ||||
| 	obj := FooStruct{} | ||||
| 	req := requestWithBody(path, body) | ||||
| 	req := requestWithBody(method, path, body) | ||||
| 	if method == "POST" { | ||||
| 		req.Header.Add("Content-Type", MIMEPOSTForm) | ||||
| 	} | ||||
| 	err := b.Bind(req, &obj) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Equal(t, obj.Foo, "bar") | ||||
|  | ||||
| 	obj = FooStruct{} | ||||
| 	req = requestWithBody(badPath, badBody) | ||||
| 	req = requestWithBody(method, badPath, badBody) | ||||
| 	err = JSON.Bind(req, &obj) | ||||
| 	assert.Error(t, err) | ||||
| } | ||||
|  | ||||
| func requestWithBody(path, body string) (req *http.Request) { | ||||
| 	req, _ = http.NewRequest("POST", path, bytes.NewBufferString(body)) | ||||
| func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { | ||||
| 	assert.Equal(t, b.Name(), name) | ||||
|  | ||||
| 	obj := FooStruct{} | ||||
| 	req := requestWithBody("POST", path, body) | ||||
| 	err := b.Bind(req, &obj) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Equal(t, obj.Foo, "bar") | ||||
|  | ||||
| 	obj = FooStruct{} | ||||
| 	req = requestWithBody("POST", badPath, badBody) | ||||
| 	err = JSON.Bind(req, &obj) | ||||
| 	assert.Error(t, err) | ||||
| } | ||||
|  | ||||
| func requestWithBody(method, path, body string) (req *http.Request) { | ||||
| 	req, _ = http.NewRequest(method, path, bytes.NewBufferString(body)) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| @ -6,13 +6,13 @@ package binding | ||||
| 
 | ||||
| import "net/http" | ||||
| 
 | ||||
| type getFormBinding struct{} | ||||
| type formBinding struct{} | ||||
| 
 | ||||
| func (_ getFormBinding) Name() string { | ||||
| 	return "get_form" | ||||
| func (_ formBinding) Name() string { | ||||
| 	return "query" | ||||
| } | ||||
| 
 | ||||
| func (_ getFormBinding) Bind(req *http.Request, obj interface{}) error { | ||||
| func (_ formBinding) Bind(req *http.Request, obj interface{}) error { | ||||
| 	if err := req.ParseForm(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @ -1,23 +0,0 @@ | ||||
| // Copyright 2014 Manu Martinez-Almeida.  All rights reserved. | ||||
| // Use of this source code is governed by a MIT style | ||||
| // license that can be found in the LICENSE file. | ||||
|  | ||||
| package binding | ||||
|  | ||||
| import "net/http" | ||||
|  | ||||
| type postFormBinding struct{} | ||||
|  | ||||
| func (_ postFormBinding) Name() string { | ||||
| 	return "post_form" | ||||
| } | ||||
|  | ||||
| func (_ postFormBinding) 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) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user