mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
Merge branch 'master' into master
This commit is contained in:
commit
10564061ae
3
Makefile
3
Makefile
@ -19,6 +19,9 @@ test:
|
|||||||
if grep -q "^--- FAIL" tmp.out; then \
|
if grep -q "^--- FAIL" tmp.out; then \
|
||||||
rm tmp.out; \
|
rm tmp.out; \
|
||||||
exit 1; \
|
exit 1; \
|
||||||
|
elif grep -q "build failed" tmp.out; then \
|
||||||
|
rm tmp.out; \
|
||||||
|
exit; \
|
||||||
fi; \
|
fi; \
|
||||||
if [ -f profile.out ]; then \
|
if [ -f profile.out ]; then \
|
||||||
cat profile.out | grep -v "mode:" >> coverage.out; \
|
cat profile.out | grep -v "mode:" >> coverage.out; \
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -690,6 +691,28 @@ func TestUriBinding(t *testing.T) {
|
|||||||
assert.Equal(t, map[string]interface{}(nil), not.Name)
|
assert.Equal(t, map[string]interface{}(nil), not.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUriInnerBinding(t *testing.T) {
|
||||||
|
type Tag struct {
|
||||||
|
Name string `uri:"name"`
|
||||||
|
S struct {
|
||||||
|
Age int `uri:"age"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedName := "mike"
|
||||||
|
expectedAge := 25
|
||||||
|
|
||||||
|
m := map[string][]string{
|
||||||
|
"name": {expectedName},
|
||||||
|
"age": {strconv.Itoa(expectedAge)},
|
||||||
|
}
|
||||||
|
|
||||||
|
var tag Tag
|
||||||
|
assert.NoError(t, Uri.BindUri(m, &tag))
|
||||||
|
assert.Equal(t, tag.Name, expectedName)
|
||||||
|
assert.Equal(t, tag.S.Age, expectedAge)
|
||||||
|
}
|
||||||
|
|
||||||
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
|
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
|
||||||
b := Form
|
b := Form
|
||||||
assert.Equal(t, "form", b.Name())
|
assert.Equal(t, "form", b.Name())
|
||||||
|
@ -55,7 +55,7 @@ func mapFormByTag(ptr interface{}, form map[string][]string, tag string) error {
|
|||||||
structFieldKind = structField.Kind()
|
structFieldKind = structField.Kind()
|
||||||
}
|
}
|
||||||
if structFieldKind == reflect.Struct {
|
if structFieldKind == reflect.Struct {
|
||||||
err := mapForm(structField.Addr().Interface(), form)
|
err := mapFormByTag(structField.Addr().Interface(), form, tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1475,15 +1475,19 @@ func TestContextShouldBindWithQuery(t *testing.T) {
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
|
||||||
c.Request, _ = http.NewRequest("POST", "/?foo=bar&bar=foo", bytes.NewBufferString("foo=unused"))
|
c.Request, _ = http.NewRequest("POST", "/?foo=bar&bar=foo&Foo=bar1&Bar=foo1", bytes.NewBufferString("foo=unused"))
|
||||||
|
|
||||||
var obj struct {
|
var obj struct {
|
||||||
Foo string `form:"foo"`
|
Foo string `form:"foo"`
|
||||||
Bar string `form:"bar"`
|
Bar string `form:"bar"`
|
||||||
|
Foo1 string `form:"Foo"`
|
||||||
|
Bar1 string `form:"Bar"`
|
||||||
}
|
}
|
||||||
assert.NoError(t, c.ShouldBindQuery(&obj))
|
assert.NoError(t, c.ShouldBindQuery(&obj))
|
||||||
assert.Equal(t, "foo", obj.Bar)
|
assert.Equal(t, "foo", obj.Bar)
|
||||||
assert.Equal(t, "bar", obj.Foo)
|
assert.Equal(t, "bar", obj.Foo)
|
||||||
|
assert.Equal(t, "foo1", obj.Bar1)
|
||||||
|
assert.Equal(t, "bar1", obj.Foo1)
|
||||||
assert.Equal(t, 0, w.Body.Len())
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@ func TestBadUnixSocket(t *testing.T) {
|
|||||||
func TestFileDescriptor(t *testing.T) {
|
func TestFileDescriptor(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
|
|
||||||
addr, err := net.ResolveTCPAddr("tcp", ":8000")
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
listener, err := net.ListenTCP("tcp", addr)
|
listener, err := net.ListenTCP("tcp", addr)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@ -152,7 +152,7 @@ func TestFileDescriptor(t *testing.T) {
|
|||||||
// otherwise the main thread will complete
|
// otherwise the main thread will complete
|
||||||
time.Sleep(5 * time.Millisecond)
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
|
||||||
c, err := net.Dial("tcp", "localhost:8000")
|
c, err := net.Dial("tcp", listener.Addr().String())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
|
fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
|
||||||
|
17
logger.go
17
logger.go
@ -35,9 +35,9 @@ type LoggerConfig struct {
|
|||||||
// Optional. Default value is gin.DefaultWriter.
|
// Optional. Default value is gin.DefaultWriter.
|
||||||
Output io.Writer
|
Output io.Writer
|
||||||
|
|
||||||
// SkipPathes is a url path array which logs are not written.
|
// SkipPaths is a url path array which logs are not written.
|
||||||
// Optional.
|
// Optional.
|
||||||
SkipPathes []string
|
SkipPaths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
|
// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
|
||||||
@ -46,13 +46,22 @@ type LogFormatter func(params LogFormatterParams) string
|
|||||||
// LogFormatterParams is the structure any formatter will be handed when time to log comes
|
// LogFormatterParams is the structure any formatter will be handed when time to log comes
|
||||||
type LogFormatterParams struct {
|
type LogFormatterParams struct {
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
|
|
||||||
|
// TimeStamp shows the time after the server returns a response.
|
||||||
TimeStamp time.Time
|
TimeStamp time.Time
|
||||||
|
// StatusCode is HTTP response code.
|
||||||
StatusCode int
|
StatusCode int
|
||||||
|
// Latency is how much time the server cost to process a certain request.
|
||||||
Latency time.Duration
|
Latency time.Duration
|
||||||
|
// ClientIP equals Context's ClientIP method.
|
||||||
ClientIP string
|
ClientIP string
|
||||||
|
// Method is the HTTP method given to the request.
|
||||||
Method string
|
Method string
|
||||||
|
// Path is a path the client requests.
|
||||||
Path string
|
Path string
|
||||||
|
// ErrorMessage is set if error has occurred in processing the request.
|
||||||
ErrorMessage string
|
ErrorMessage string
|
||||||
|
// IsTerm shows whether does gin's output descriptor refers to a terminal.
|
||||||
IsTerm bool
|
IsTerm bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +124,7 @@ func LoggerWithFormatter(f LogFormatter) HandlerFunc {
|
|||||||
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||||
return LoggerWithConfig(LoggerConfig{
|
return LoggerWithConfig(LoggerConfig{
|
||||||
Output: out,
|
Output: out,
|
||||||
SkipPathes: notlogged,
|
SkipPaths: notlogged,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +140,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
out = DefaultWriter
|
out = DefaultWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
notlogged := conf.SkipPathes
|
notlogged := conf.SkipPaths
|
||||||
|
|
||||||
isTerm := true
|
isTerm := true
|
||||||
|
|
||||||
|
@ -321,7 +321,7 @@ func TestLoggerWithConfigSkippingPaths(t *testing.T) {
|
|||||||
router := New()
|
router := New()
|
||||||
router.Use(LoggerWithConfig(LoggerConfig{
|
router.Use(LoggerWithConfig(LoggerConfig{
|
||||||
Output: buffer,
|
Output: buffer,
|
||||||
SkipPathes: []string{"/skipped"},
|
SkipPaths: []string{"/skipped"},
|
||||||
}))
|
}))
|
||||||
router.GET("/logged", func(c *Context) {})
|
router.GET("/logged", func(c *Context) {})
|
||||||
router.GET("/skipped", func(c *Context) {})
|
router.GET("/skipped", func(c *Context) {})
|
||||||
|
@ -47,7 +47,7 @@ type RouterGroup struct {
|
|||||||
|
|
||||||
var _ IRouter = &RouterGroup{}
|
var _ IRouter = &RouterGroup{}
|
||||||
|
|
||||||
// Use adds middleware to the group, see example code in github.
|
// Use adds middleware to the group, see example code in GitHub.
|
||||||
func (group *RouterGroup) Use(middleware ...HandlerFunc) IRoutes {
|
func (group *RouterGroup) Use(middleware ...HandlerFunc) IRoutes {
|
||||||
group.Handlers = append(group.Handlers, middleware...)
|
group.Handlers = append(group.Handlers, middleware...)
|
||||||
return group.returnObj()
|
return group.returnObj()
|
||||||
@ -78,7 +78,7 @@ func (group *RouterGroup) handle(httpMethod, relativePath string, handlers Handl
|
|||||||
|
|
||||||
// Handle registers a new request handle and middleware with the given path and method.
|
// Handle registers a new request handle and middleware with the given path and method.
|
||||||
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
|
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
|
||||||
// See the example code in github.
|
// See the example code in GitHub.
|
||||||
//
|
//
|
||||||
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
|
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
|
||||||
// functions can be used.
|
// functions can be used.
|
||||||
|
@ -4,12 +4,12 @@
|
|||||||
|
|
||||||
// +build tools
|
// +build tools
|
||||||
|
|
||||||
// This file exists to cause `go mod` and `go get` to believe these tools
|
// This package exists to cause `go mod` and `go get` to believe these tools
|
||||||
// are dependencies, even though they are not runtime dependencies of any
|
// are dependencies, even though they are not runtime dependencies of any
|
||||||
// gin package. This means they will appear in `go.mod` file, but will not
|
// gin package. This means they will appear in `go.mod` file, but will not
|
||||||
// be a part of the build.
|
// be a part of the build.
|
||||||
|
|
||||||
package gin
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/campoy/embedmd"
|
_ "github.com/campoy/embedmd"
|
Loading…
x
Reference in New Issue
Block a user