From b267c6757c130ec886e53083d508357047ccbcdb Mon Sep 17 00:00:00 2001 From: ^_^void Date: Mon, 24 Feb 2020 18:24:35 +0800 Subject: [PATCH 1/6] add func AllParams() for Context It is usefull with for-each. --- context.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/context.go b/context.go index d99ad16..147e310 100644 --- a/context.go +++ b/context.go @@ -260,6 +260,11 @@ func (ctx *Context) Params(name string) string { return ctx.params[name] } +// AllParams returns all params. +func (ctx *Context) AllParams() Params { + return ctx.params +} + // SetParams sets value of param with given name. func (ctx *Context) SetParams(name, val string) { if name != "*" && !strings.HasPrefix(name, ":") { From 5ea44107d0814516b0922735dd52c08c4d77d3f1 Mon Sep 17 00:00:00 2001 From: ^_^void Date: Tue, 25 Feb 2020 09:42:14 +0800 Subject: [PATCH 2/6] add unit test for AllParams() add unit test for AllParams() --- context_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/context_test.go b/context_test.go index 051a93a..130e0f7 100644 --- a/context_test.go +++ b/context_test.go @@ -171,6 +171,18 @@ func Test_Context(t *testing.T) { So(resp.Body.String(), ShouldEqual, "user user 1 13 1.24 ") }) + Convey("All Parameter", func() { + m.Get("/:arg/:param/:flag", func(ctx *Context) string { + return com.ToStr(len(ctx.AllParams())) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/arg/param/flag", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp,req) + So(resp.Body.String(), ShouldEqual, "3") + }) + Convey("Get file", func() { m.Post("/getfile", func(ctx *Context) { ctx.Query("") From 3e125b6313948e4da6dc96f3eb85fbf99804fd1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Tue, 25 Feb 2020 13:14:14 +0800 Subject: [PATCH 3/6] Update context_test.go --- context_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/context_test.go b/context_test.go index 130e0f7..5b4a951 100644 --- a/context_test.go +++ b/context_test.go @@ -171,16 +171,20 @@ func Test_Context(t *testing.T) { So(resp.Body.String(), ShouldEqual, "user user 1 13 1.24 ") }) - Convey("All Parameter", func() { + Convey("Get all URL paramaters", func() { m.Get("/:arg/:param/:flag", func(ctx *Context) string { - return com.ToStr(len(ctx.AllParams())) + kvs := make([]string, len(ctx.AllParams())) + for k, v := range ctx.AllParams() { + kvs = append(kvs, k + "=" + v) + } + return strings.Join(kvs, ",") }) resp := httptest.NewRecorder() - req, err := http.NewRequest("GET", "/arg/param/flag", nil) + req, err := http.NewRequest("GET", "/1/2/3", nil) So(err, ShouldBeNil) m.ServeHTTP(resp,req) - So(resp.Body.String(), ShouldEqual, "3") + So(resp.Body.String(), ShouldEqual, ":arg=1,:param=2,:flag=3") }) Convey("Get file", func() { From f75d54ff2ed88b6ade1126f0339b5c75afd10d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Tue, 25 Feb 2020 13:18:49 +0800 Subject: [PATCH 4/6] Update context_test.go --- context_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 5b4a951..7484835 100644 --- a/context_test.go +++ b/context_test.go @@ -20,6 +20,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "sort" "strings" "testing" "time" @@ -177,7 +178,7 @@ func Test_Context(t *testing.T) { for k, v := range ctx.AllParams() { kvs = append(kvs, k + "=" + v) } - return strings.Join(kvs, ",") + return strings.Join(sort.Strings(kvs), ",") }) resp := httptest.NewRecorder() From 4834f7c7542a846876e7bd584085d31b3b10ae41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Tue, 25 Feb 2020 13:20:49 +0800 Subject: [PATCH 5/6] Update context_test.go --- context_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/context_test.go b/context_test.go index 7484835..96fbf79 100644 --- a/context_test.go +++ b/context_test.go @@ -178,14 +178,15 @@ func Test_Context(t *testing.T) { for k, v := range ctx.AllParams() { kvs = append(kvs, k + "=" + v) } - return strings.Join(sort.Strings(kvs), ",") + sort.Strings(kvs) + return strings.Join(kvs, ",") }) resp := httptest.NewRecorder() req, err := http.NewRequest("GET", "/1/2/3", nil) So(err, ShouldBeNil) m.ServeHTTP(resp,req) - So(resp.Body.String(), ShouldEqual, ":arg=1,:param=2,:flag=3") + So(resp.Body.String(), ShouldEqual, ":arg=1,:flag=3,:param=2") }) Convey("Get file", func() { From e399fba82ac8bfec08bb66337e21627ff7129e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Tue, 25 Feb 2020 13:25:43 +0800 Subject: [PATCH 6/6] Update context_test.go --- context_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 96fbf79..c1417d7 100644 --- a/context_test.go +++ b/context_test.go @@ -174,7 +174,7 @@ func Test_Context(t *testing.T) { Convey("Get all URL paramaters", func() { m.Get("/:arg/:param/:flag", func(ctx *Context) string { - kvs := make([]string, len(ctx.AllParams())) + kvs := make([]string, 0, len(ctx.AllParams())) for k, v := range ctx.AllParams() { kvs = append(kvs, k + "=" + v) }