Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context: add AllParams() #187

Merged
merged 6 commits into from Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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