mirror of https://github.com/gofiber/fiber.git
BodyParser supports Query params
parent
995c9d71c3
commit
673bd61fa1
3
app.go
3
app.go
|
@ -73,7 +73,8 @@ type Group struct {
|
|||
// New creates a new Fiber named instance.
|
||||
// You can pass optional settings when creating a new instance.
|
||||
func New(settings ...*Settings) *App {
|
||||
|
||||
schemaDecoderForm.SetAliasTag("form")
|
||||
schemaDecoderQuery.SetAliasTag("query")
|
||||
// Create app
|
||||
app := new(App)
|
||||
// Create settings
|
||||
|
|
63
app_test.go
63
app_test.go
|
@ -2,11 +2,17 @@
|
|||
// 📌 API Documentation: https://fiber.wiki
|
||||
// 📝 Github Repository: https://github.com/gofiber/fiber
|
||||
|
||||
// go test -v -coverprofile cover.out .
|
||||
// go tool cover -html=cover.out -o cover.html
|
||||
// open cover.html
|
||||
|
||||
package fiber
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var handler = func(c *Ctx) {}
|
||||
|
@ -67,6 +73,20 @@ func Test_Methods(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func Test_New(t *testing.T) {
|
||||
app := New(&Settings{
|
||||
Immutable: true,
|
||||
})
|
||||
app.Get("/", func(*Ctx) {
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Shutdown(t *testing.T) {
|
||||
app := New()
|
||||
_ = app.Shutdown()
|
||||
}
|
||||
|
||||
func Test_Static(t *testing.T) {
|
||||
app := New()
|
||||
grp := app.Group("/v1")
|
||||
|
@ -167,18 +187,31 @@ func Test_Group(t *testing.T) {
|
|||
is200(t, app, "/test/v1/users", "GET")
|
||||
}
|
||||
|
||||
// func Test_Listen(t *testing.T) {
|
||||
// t.Parallel()
|
||||
// app := New()
|
||||
// app.Banner = false
|
||||
// go func() {
|
||||
// time.Sleep(1 * time.Second)
|
||||
// _ = app.Shutdown()
|
||||
// }()
|
||||
// app.Listen(3002)
|
||||
// go func() {
|
||||
// time.Sleep(1 * time.Second)
|
||||
// _ = app.Shutdown()
|
||||
// }()
|
||||
// app.Listen("3002")
|
||||
// }
|
||||
func Test_Listen(t *testing.T) {
|
||||
app := New()
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
_ = app.Shutdown()
|
||||
}()
|
||||
app.Listen(3002)
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
_ = app.Shutdown()
|
||||
}()
|
||||
app.Listen("3003")
|
||||
}
|
||||
|
||||
func Test_Serve(t *testing.T) {
|
||||
app := New(&Settings{
|
||||
Prefork: true,
|
||||
})
|
||||
ln, err := net.Listen("tcp4", ":3004")
|
||||
if err != nil {
|
||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
_ = app.Shutdown()
|
||||
}()
|
||||
app.Serve(ln)
|
||||
}
|
||||
|
|
19
ctx.go
19
ctx.go
|
@ -61,7 +61,8 @@ type Cookie struct {
|
|||
|
||||
// Global variables
|
||||
var jsonParser = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
var schemaDecoder = schema.NewDecoder()
|
||||
var schemaDecoderForm = schema.NewDecoder()
|
||||
var schemaDecoderQuery = schema.NewDecoder()
|
||||
|
||||
// Ctx pool
|
||||
var poolCtx = sync.Pool{
|
||||
|
@ -256,7 +257,7 @@ func (ctx *Ctx) Body(key ...string) string {
|
|||
// It supports decoding the following content types based on the Content-Type header:
|
||||
// application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data
|
||||
func (ctx *Ctx) BodyParser(out interface{}) error {
|
||||
// TODO : Query Params
|
||||
// get content type
|
||||
ctype := getString(ctx.Fasthttp.Request.Header.ContentType())
|
||||
// application/json
|
||||
if strings.HasPrefix(ctype, MIMEApplicationJSON) {
|
||||
|
@ -272,7 +273,7 @@ func (ctx *Ctx) BodyParser(out interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return schemaDecoder.Decode(out, data)
|
||||
return schemaDecoderForm.Decode(out, data)
|
||||
}
|
||||
// multipart/form-data
|
||||
if strings.HasPrefix(ctype, MIMEMultipartForm) {
|
||||
|
@ -280,9 +281,17 @@ func (ctx *Ctx) BodyParser(out interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return schemaDecoder.Decode(out, data.Value)
|
||||
|
||||
return schemaDecoderForm.Decode(out, data.Value)
|
||||
}
|
||||
// query Params
|
||||
if ctx.Fasthttp.QueryArgs().Len() > 0 {
|
||||
data := make(map[string][]string)
|
||||
ctx.Fasthttp.QueryArgs().VisitAll(func(key []byte, val []byte) {
|
||||
data[getString(key)] = []string{getString(val)}
|
||||
})
|
||||
return schemaDecoderQuery.Decode(out, data)
|
||||
}
|
||||
|
||||
return fmt.Errorf("BodyParser: cannot parse content-type: %v", ctype)
|
||||
}
|
||||
|
||||
|
|
24
ctx_test.go
24
ctx_test.go
|
@ -156,11 +156,11 @@ func Test_Body(t *testing.T) {
|
|||
func Test_BodyParser(t *testing.T) {
|
||||
app := New()
|
||||
type Demo struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name" xml:"name" form:"name" query:"name"`
|
||||
}
|
||||
app.Post("/test", func(c *Ctx) {
|
||||
d := new(Demo)
|
||||
err := c.BodyParser(&d)
|
||||
err := c.BodyParser(d)
|
||||
if err != nil {
|
||||
t.Fatalf(`%s: BodyParser %v`, t.Name(), err)
|
||||
}
|
||||
|
@ -176,6 +176,26 @@ func Test_BodyParser(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||
}
|
||||
|
||||
// data := url.Values{}
|
||||
// data.Set("name", "john")
|
||||
// req = httptest.NewRequest("POST", "/test", strings.NewReader(data.Encode()))
|
||||
// req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
// req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
||||
|
||||
// _, err = app.Test(req)
|
||||
// if err != nil {
|
||||
// t.Fatalf(`%s: %s`, t.Name(), err)
|
||||
// }
|
||||
|
||||
// req = httptest.NewRequest("POST", "/test", bytes.NewBuffer([]byte(`<name>john</name>`)))
|
||||
// req.Header.Set("Content-Type", "application/xml")
|
||||
// req.Header.Set("Content-Length", strconv.Itoa(len([]byte(`<name>john</name>`))))
|
||||
|
||||
// _, err = app.Test(req)
|
||||
// if err != nil {
|
||||
// t.Fatalf(`%s: %s`, t.Name(), err)
|
||||
// }
|
||||
}
|
||||
func Test_Cookies(t *testing.T) {
|
||||
app := New()
|
||||
|
|
7
go.sum
7
go.sum
|
@ -7,12 +7,19 @@ github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGn
|
|||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
|
||||
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
|
||||
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
|
||||
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||
github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
|
|
Loading…
Reference in New Issue