+
+
+
+ Ray Mayemir
+
+ |
@@ -515,13 +521,13 @@ If you want to say **thank you** and/or support the active development of `Fiber
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_de.md b/.github/README_de.md
index 8a9b7c77bb..71d6d816e2 100644
--- a/.github/README_de.md
+++ b/.github/README_de.md
@@ -156,8 +156,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -504,6 +504,12 @@ Falls du **danke** sagen möchtest und/oder aktiv die Entwicklung von `fiber` f
+
+
+
+ Ray Mayemir
+
+ |
@@ -513,13 +519,13 @@ Falls du **danke** sagen möchtest und/oder aktiv die Entwicklung von `fiber` f
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_es.md b/.github/README_es.md
index 446f5ddc6d..32e7cb89bf 100644
--- a/.github/README_es.md
+++ b/.github/README_es.md
@@ -156,8 +156,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -485,6 +485,12 @@ Si quiere **agradecer** y/o apoyar el desarrollo activo de la `Fiber`:
+
+
+
+ Ray Mayemir
+
+ |
@@ -494,13 +500,13 @@ Si quiere **agradecer** y/o apoyar el desarrollo activo de la `Fiber`:
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_fr.md b/.github/README_fr.md
index 6ea42db25a..6784650eaf 100644
--- a/.github/README_fr.md
+++ b/.github/README_fr.md
@@ -156,8 +156,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -485,6 +485,12 @@ Si vous voulez nous remercier et/ou soutenir le développement actif de `Fiber`:
+
+
+
+ Ray Mayemir
+
+ |
@@ -494,13 +500,13 @@ Si vous voulez nous remercier et/ou soutenir le développement actif de `Fiber`:
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_id.md b/.github/README_id.md
index 502e7846ec..9a765adc5a 100644
--- a/.github/README_id.md
+++ b/.github/README_id.md
@@ -158,8 +158,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -488,6 +488,12 @@ Apabila anda ingin mengucapkan **terima kasih** dan/atau mendukung pengembangan
+
+
+
+ Ray Mayemir
+
+ |
@@ -497,13 +503,13 @@ Apabila anda ingin mengucapkan **terima kasih** dan/atau mendukung pengembangan
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_ja.md b/.github/README_ja.md
index 19bac17489..883b0e39ee 100644
--- a/.github/README_ja.md
+++ b/.github/README_ja.md
@@ -160,8 +160,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -491,6 +491,12 @@ func main() {
+
+
+
+ Ray Mayemir
+
+ |
@@ -500,13 +506,13 @@ func main() {
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_ko.md b/.github/README_ko.md
index dd1464c9d8..9addac3a69 100644
--- a/.github/README_ko.md
+++ b/.github/README_ko.md
@@ -160,8 +160,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -489,6 +489,12 @@ func main() {
+
+
+
+ Ray Mayemir
+
+ |
@@ -498,13 +504,13 @@ func main() {
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_pt.md b/.github/README_pt.md
index 19ee983f20..b3f5a67fea 100644
--- a/.github/README_pt.md
+++ b/.github/README_pt.md
@@ -156,8 +156,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -485,6 +485,12 @@ Se você quer **agradecer** e/ou apoiar o desenvolvimento ativo do `Fiber`:
+
+
+
+ Ray Mayemir
+
+ |
@@ -494,13 +500,13 @@ Se você quer **agradecer** e/ou apoiar o desenvolvimento ativo do `Fiber`:
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_ru.md b/.github/README_ru.md
index 994fc1d9a3..a3f6435faf 100644
--- a/.github/README_ru.md
+++ b/.github/README_ru.md
@@ -158,8 +158,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -506,6 +506,12 @@ func main() {
+
+
+
+ Ray Mayemir
+
+ |
@@ -515,13 +521,13 @@ func main() {
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_tr.md b/.github/README_tr.md
index b6930542b8..9567961c77 100644
--- a/.github/README_tr.md
+++ b/.github/README_tr.md
@@ -156,8 +156,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -486,6 +486,12 @@ Eğer **teşekkür etmek** ve/veya `Fiber` ın aktif geliştirilmesini destekle
+
+
+
+ Ray Mayemir
+
+ |
@@ -495,13 +501,13 @@ Eğer **teşekkür etmek** ve/veya `Fiber` ın aktif geliştirilmesini destekle
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
diff --git a/.github/README_zh-CN.md b/.github/README_zh-CN.md
index d150a86135..8b2a94a53d 100644
--- a/.github/README_zh-CN.md
+++ b/.github/README_zh-CN.md
@@ -156,8 +156,8 @@ func main() {
})
// GET /api/register
- app.Get("/api*", func(c *fiber.Ctx) {
- fmt.Printf("/api%s", c.Params("*"))
+ app.Get("/api/*", func(c *fiber.Ctx) {
+ fmt.Printf("/api/%s", c.Params("*"))
// => /api/register
})
@@ -486,6 +486,12 @@ func main() {
+
+
+
+ Ray Mayemir
+
+ |
@@ -495,13 +501,13 @@ func main() {
|
- ekaputra07
+ Eka Putra
|
- HenrikBinggl
+ Henrik Binggl
|
From 673bd61fa1dbfa99f06bb1b21a4c7f3fd03069d9 Mon Sep 17 00:00:00 2001
From: Fenny <25108519+Fenny@users.noreply.github.com>
Date: Mon, 13 Apr 2020 09:01:27 +0200
Subject: [PATCH 3/4] BodyParser supports Query params
---
app.go | 3 ++-
app_test.go | 63 ++++++++++++++++++++++++++++++++++++++++-------------
ctx.go | 19 +++++++++++-----
ctx_test.go | 24 ++++++++++++++++++--
go.sum | 7 ++++++
5 files changed, 93 insertions(+), 23 deletions(-)
diff --git a/app.go b/app.go
index 4a8d61e71c..f9bbd5a51c 100644
--- a/app.go
+++ b/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
diff --git a/app_test.go b/app_test.go
index 3a531ed1f1..537286fd0b 100644
--- a/app_test.go
+++ b/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)
+}
diff --git a/ctx.go b/ctx.go
index 0dea77a8fe..e768b9abab 100644
--- a/ctx.go
+++ b/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)
}
diff --git a/ctx_test.go b/ctx_test.go
index 51f2cc424b..0deab880db 100644
--- a/ctx_test.go
+++ b/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(`john`)))
+ // req.Header.Set("Content-Type", "application/xml")
+ // req.Header.Set("Content-Length", strconv.Itoa(len([]byte(`john`))))
+
+ // _, err = app.Test(req)
+ // if err != nil {
+ // t.Fatalf(`%s: %s`, t.Name(), err)
+ // }
}
func Test_Cookies(t *testing.T) {
app := New()
diff --git a/go.sum b/go.sum
index 276b6cc95c..1c40fec1d9 100644
--- a/go.sum
+++ b/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=
From 9e32e2e833983dfc7d7cf08b9f7d0c4a55814177 Mon Sep 17 00:00:00 2001
From: Fenny <25108519+Fenny@users.noreply.github.com>
Date: Mon, 13 Apr 2020 09:39:45 +0200
Subject: [PATCH 4/4] Update .travis.yml
---
.travis.yml | 1 -
1 file changed, 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index 1cf1ac741b..09c7dc26ff 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -23,4 +23,3 @@ script:
- GOARCH=386 go build
- go test -v
- - go test -race -v
| | | | | | | | | | |