Skip to content

Commit

Permalink
Support set query string.
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Jul 23, 2016
1 parent 94499e3 commit e1f9223
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions gofight.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
// "X-Version": version,
// })
//
// Set query string: Using SetQUERY to generate query string data.
//
// SetQUERY(gofight.H{
// "a": "1",
// "b": "2",
// })
//
// POST FORM Data: Using SetFORM to generate form data.
//
// SetFORM(gofight.H{
Expand Down Expand Up @@ -212,6 +219,23 @@ func (rc *RequestConfig) SetFORM(body H) *RequestConfig {
return rc
}

// SetQUERY supply query string.
func (rc *RequestConfig) SetQUERY(query H) *RequestConfig {
f := make(url.Values)

for k, v := range query {
f.Set(k, v)
}

if strings.Contains(rc.Path, "?") {
rc.Path = rc.Path + "&" + f.Encode()
} else {
rc.Path = rc.Path + "?" + f.Encode()
}

return rc
}

// SetBody supply raw body.
func (rc *RequestConfig) SetBody(body string) *RequestConfig {
if len(body) > 0 {
Expand Down
20 changes: 20 additions & 0 deletions gofight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,23 @@ func TestEchoOptions(t *testing.T) {
assert.Equal(t, http.StatusOK, r.Status())
})
}

func TestSetQueryString(t *testing.T) {
r := New()

r.GET("/hello").
SetQUERY(H{
"a": "1",
"b": "2",
})

assert.Equal(t, "/hello?a=1&b=2", r.Path)

r.GET("/hello?foo=bar").
SetQUERY(H{
"a": "1",
"b": "2",
})

assert.Equal(t, "/hello?foo=bar&a=1&b=2", r.Path)
}

0 comments on commit e1f9223

Please sign in to comment.