Skip to content

Commit

Permalink
context: add AllParams() (#187)
Browse files Browse the repository at this point in the history
* 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: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
  • Loading branch information
xmh19936688 and unknwon committed Feb 25, 2020
1 parent 993bb1c commit 5993313
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions context.go
Expand Up @@ -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, ":") {
Expand Down
18 changes: 18 additions & 0 deletions context_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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("")
Expand Down

0 comments on commit 5993313

Please sign in to comment.