From 599331394d543e20d628f120837b48e37b40eaba Mon Sep 17 00:00:00 2001 From: ^_^void Date: Tue, 25 Feb 2020 13:29:53 +0800 Subject: [PATCH] context: add AllParams() (#187) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add func AllParams() for Context It is usefull with for-each. * add unit test for AllParams() add unit test for AllParams() * Update context_test.go * Update context_test.go * Update context_test.go * Update context_test.go Co-authored-by: ᴜɴᴋɴᴡᴏɴ --- context.go | 5 +++++ context_test.go | 18 ++++++++++++++++++ 2 files changed, 23 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, ":") { diff --git a/context_test.go b/context_test.go index 051a93a..c1417d7 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" @@ -171,6 +172,23 @@ func Test_Context(t *testing.T) { So(resp.Body.String(), ShouldEqual, "user user 1 13 1.24 ") }) + Convey("Get all URL paramaters", func() { + m.Get("/:arg/:param/:flag", func(ctx *Context) string { + kvs := make([]string, 0, len(ctx.AllParams())) + for k, v := range ctx.AllParams() { + kvs = append(kvs, k + "=" + v) + } + 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,:flag=3,:param=2") + }) + Convey("Get file", func() { m.Post("/getfile", func(ctx *Context) { ctx.Query("")