Skip to content
Myr

Implement QueryArray and PostArray methods

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andrey Nering · committed by Andrey Nering · parent 4a6bc4a · Browse files · 2 files+73 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+73 added−0 removed

  • other:+73 added−0 removed

2 files+73 added−0 removed

modified:context_test.go+32 added
249 unmodified lines
250 assert.Equal(t, obj.ID, "main")251 assert.Equal(t, obj.Page, 11)252 assert.Equal(t, obj.Both, "")253 assert.Equal(t, obj.Array, []string{"first", "second"})254255 values, ok := c.GetQueryArray("array[]")256 assert.True(t, ok)257 assert.Equal(t, "first", values[0])258 assert.Equal(t, "second", values[1])259260 values = c.QueryArray("array[]")261 assert.Equal(t, "first", values[0])262 assert.Equal(t, "second", values[1])263264 values = c.QueryArray("nokey")265 assert.Equal(t, 0, len(values))266267 values = c.QueryArray("both")268 assert.Equal(t, 1, len(values))269 assert.Equal(t, "GET", values[0])270}271272func TestContextPostFormMultipart(t *testing.T) {273 c, _, _ := CreateTestContext()
40 unmodified lines
314 value, ok = c.GetPostForm("nokey")315 assert.False(t, ok)316 assert.Empty(t, value)317 assert.Equal(t, c.DefaultPostForm("nokey", "nothing"), "nothing")318319 values, ok := c.GetPostFormArray("array")320 assert.True(t, ok)321 assert.Equal(t, "first", values[0])322 assert.Equal(t, "second", values[1])323324 values = c.PostFormArray("array")325 assert.Equal(t, "first", values[0])326 assert.Equal(t, "second", values[1])327328 values = c.PostFormArray("nokey")329 assert.Equal(t, 0, len(values))330331 values = c.PostFormArray("foo")332 assert.Equal(t, 1, len(values))333 assert.Equal(t, "bar", values[0])334}335336func TestContextSetCookie(t *testing.T) {337 c, _, _ := CreateTestContext()
392 unmodified lines
modified:context.go+41 added
234 unmodified lines
235 }236 return "", false237}238239// QueryArray returns a slice of strings for a given query key.240// The length of the slice depends on the number of params with the given key.241func (c *Context) QueryArray(key string) []string {242 values, _ := c.GetQueryArray(key)243 return values244}245246// GetQueryArray returns a slice of strings for a given query key, plus247// a boolean value whether at least one value exists for the given key.248func (c *Context) GetQueryArray(key string) ([]string, bool) {249 req := c.Request250 if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {251 return values, true252 }253 return []string{}, false254}255256// PostForm returns the specified key from a POST urlencoded form or multipart form257// when it exists, otherwise it returns an empty string `("")`.258func (c *Context) PostForm(key string) string {259 value, _ := c.GetPostForm(key)
30 unmodified lines
290 }291 return "", false292}293294// PostFormArray returns a slice of strings for a given form key.295// The length of the slice depends on the number of params with the given key.296func (c *Context) PostFormArray(key string) []string {297 values, _ := c.GetPostFormArray(key)298 return values299}300301// GetPostFormArray returns a slice of strings for a given form key, plus302// a boolean value whether at least one value exists for the given key.303func (c *Context) GetPostFormArray(key string) ([]string, bool) {304 req := c.Request305 req.ParseForm()306 req.ParseMultipartForm(32 << 20) // 32 MB307 if values := req.PostForm[key]; len(values) > 0 {308 return values, true309 }310 if req.MultipartForm != nil && req.MultipartForm.File != nil {311 if values := req.MultipartForm.Value[key]; len(values) > 0 {312 return values, true313 }314 }315 return []string{}, false316}317318// Bind checks the Content-Type to select a binding engine automatically,319// Depending the "Content-Type" header different bindings are used:320// "application/json" --> JSON binding321// "application/xml" --> XML binding
282 unmodified lines