-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
268 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// +build !nomsgpack | ||
|
||
package binding | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
func TestBindingMsgPackBody(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
binding BindingBody | ||
body string | ||
want string | ||
}{ | ||
{ | ||
name: "MsgPack binding", | ||
binding: MsgPack, | ||
body: msgPackBody(t), | ||
}, | ||
} { | ||
t.Logf("testing: %s", tt.name) | ||
req := requestWithBody("POST", "/", tt.body) | ||
form := FooStruct{} | ||
body, _ := ioutil.ReadAll(req.Body) | ||
assert.NoError(t, tt.binding.BindBody(body, &form)) | ||
assert.Equal(t, FooStruct{"FOO"}, form) | ||
} | ||
} | ||
|
||
func msgPackBody(t *testing.T) string { | ||
test := FooStruct{"FOO"} | ||
h := new(codec.MsgpackHandle) | ||
buf := bytes.NewBuffer(nil) | ||
assert.NoError(t, codec.NewEncoder(buf, h).Encode(test)) | ||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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. | ||
|
||
// +build !nomsgpack | ||
|
||
package binding | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
func TestBindingMsgPack(t *testing.T) { | ||
test := FooStruct{ | ||
Foo: "bar", | ||
} | ||
|
||
h := new(codec.MsgpackHandle) | ||
assert.NotNil(t, h) | ||
buf := bytes.NewBuffer([]byte{}) | ||
assert.NotNil(t, buf) | ||
err := codec.NewEncoder(buf, h).Encode(test) | ||
assert.NoError(t, err) | ||
|
||
data := buf.Bytes() | ||
|
||
testMsgPackBodyBinding(t, | ||
MsgPack, "msgpack", | ||
"/", "/", | ||
string(data), string(data[1:])) | ||
} | ||
|
||
func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { | ||
assert.Equal(t, name, b.Name()) | ||
|
||
obj := FooStruct{} | ||
req := requestWithBody("POST", path, body) | ||
req.Header.Add("Content-Type", MIMEMSGPACK) | ||
err := b.Bind(req, &obj) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "bar", obj.Foo) | ||
|
||
obj = FooStruct{} | ||
req = requestWithBody("POST", badPath, badBody) | ||
req.Header.Add("Content-Type", MIMEMSGPACK) | ||
err = MsgPack.Bind(req, &obj) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestBindingDefaultMsgPack(t *testing.T) { | ||
assert.Equal(t, MsgPack, Default("POST", MIMEMSGPACK)) | ||
assert.Equal(t, MsgPack, Default("PUT", MIMEMSGPACK2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// 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. | ||
|
||
// +build nomsgpack | ||
|
||
package binding | ||
|
||
import "net/http" | ||
|
||
// Content-Type MIME of the most common data formats. | ||
const ( | ||
MIMEJSON = "application/json" | ||
MIMEHTML = "text/html" | ||
MIMEXML = "application/xml" | ||
MIMEXML2 = "text/xml" | ||
MIMEPlain = "text/plain" | ||
MIMEPOSTForm = "application/x-www-form-urlencoded" | ||
MIMEMultipartPOSTForm = "multipart/form-data" | ||
MIMEPROTOBUF = "application/x-protobuf" | ||
MIMEYAML = "application/x-yaml" | ||
) | ||
|
||
// Binding describes the interface which needs to be implemented for binding the | ||
// data present in the request such as JSON request body, query parameters or | ||
// the form POST. | ||
type Binding interface { | ||
Name() string | ||
Bind(*http.Request, interface{}) error | ||
} | ||
|
||
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind, | ||
// but it reads the body from supplied bytes instead of req.Body. | ||
type BindingBody interface { | ||
Binding | ||
BindBody([]byte, interface{}) error | ||
} | ||
|
||
// BindingUri adds BindUri method to Binding. BindUri is similar with Bind, | ||
// but it read the Params. | ||
type BindingUri interface { | ||
Name() string | ||
BindUri(map[string][]string, interface{}) error | ||
} | ||
|
||
// StructValidator is the minimal interface which needs to be implemented in | ||
// order for it to be used as the validator engine for ensuring the correctness | ||
// of the request. Gin provides a default implementation for this using | ||
// https://github.com/go-playground/validator/tree/v8.18.2. | ||
type StructValidator interface { | ||
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right. | ||
// If the received type is not a struct, any validation should be skipped and nil must be returned. | ||
// If the received type is a struct or pointer to a struct, the validation should be performed. | ||
// If the struct is not valid or the validation itself fails, a descriptive error should be returned. | ||
// Otherwise nil must be returned. | ||
ValidateStruct(interface{}) error | ||
|
||
// Engine returns the underlying validator engine which powers the | ||
// StructValidator implementation. | ||
Engine() interface{} | ||
} | ||
|
||
// Validator is the default validator which implements the StructValidator | ||
// interface. It uses https://github.com/go-playground/validator/tree/v8.18.2 | ||
// under the hood. | ||
var Validator StructValidator = &defaultValidator{} | ||
|
||
// These implement the Binding interface and can be used to bind the data | ||
// present in the request to struct instances. | ||
var ( | ||
JSON = jsonBinding{} | ||
XML = xmlBinding{} | ||
Form = formBinding{} | ||
Query = queryBinding{} | ||
FormPost = formPostBinding{} | ||
FormMultipart = formMultipartBinding{} | ||
ProtoBuf = protobufBinding{} | ||
YAML = yamlBinding{} | ||
Uri = uriBinding{} | ||
) | ||
|
||
// Default returns the appropriate Binding instance based on the HTTP method | ||
// and the content type. | ||
func Default(method, contentType string) Binding { | ||
if method == "GET" { | ||
return Form | ||
} | ||
|
||
switch contentType { | ||
case MIMEJSON: | ||
return JSON | ||
case MIMEXML, MIMEXML2: | ||
return XML | ||
case MIMEPROTOBUF: | ||
return ProtoBuf | ||
case MIMEYAML: | ||
return YAML | ||
case MIMEMultipartPOSTForm: | ||
return FormMultipart | ||
default: // case MIMEPOSTForm: | ||
return Form | ||
} | ||
} | ||
|
||
func validate(obj interface{}) error { | ||
if Validator == nil { | ||
return nil | ||
} | ||
return Validator.ValidateStruct(obj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.